Class: Trace::SpanId

Inherits:
Object
  • Object
show all
Defined in:
lib/zipkin-tracer/trace.rb

Direct Known Subclasses

TraceId128Bit

Constant Summary collapse

HEX_REGEX =
/^[a-f0-9]{16,32}$/i
MAX_SIGNED_I64 =
9223372036854775807
MASK =
(2 ** 64) - 1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ SpanId

Returns a new instance of SpanId.



91
92
93
94
95
96
97
98
# File 'lib/zipkin-tracer/trace.rb', line 91

def initialize(value)
  @value = value
  @i64 = if @value > MAX_SIGNED_I64
    -1 * ((@value ^ MASK) + 1)
  else
    @value
  end
end

Class Method Details

.from_value(v) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/zipkin-tracer/trace.rb', line 80

def self.from_value(v)
  if v.is_a?(String) && v =~ HEX_REGEX
    # drops any bits higher than 64 by selecting right-most 16 characters
    new(v.length > 16 ? v[v.length - 16, 16].hex : v.hex)
  elsif v.is_a?(Numeric)
    new(v)
  elsif v.is_a?(SpanId)
    v
  end
end

Instance Method Details

#to_iObject



101
# File 'lib/zipkin-tracer/trace.rb', line 101

def to_i; @i64; end

#to_sObject



100
# File 'lib/zipkin-tracer/trace.rb', line 100

def to_s; "%016x" % @value; end