Module: Jaeger::TraceId

Defined in:
lib/jaeger/trace_id.rb

Constant Summary collapse

MAX_64BIT_SIGNED_INT =
(1 << 63) - 1
MAX_64BIT_UNSIGNED_INT =
(1 << 64) - 1
MAX_128BIT_UNSIGNED_INT =
(1 << 128) - 1
TRACE_ID_UPPER_BOUND =
MAX_64BIT_UNSIGNED_INT + 1

Class Method Summary collapse

Class Method Details

.base16_hex_id_to_uint128(id) ⇒ Object



21
22
23
24
25
26
# File 'lib/jaeger/trace_id.rb', line 21

def self.base16_hex_id_to_uint128(id)
  return nil unless id

  value = id.to_i(16)
  value > MAX_128BIT_UNSIGNED_INT || value.negative? ? 0 : value
end

.base16_hex_id_to_uint64(id) ⇒ Object



14
15
16
17
18
19
# File 'lib/jaeger/trace_id.rb', line 14

def self.base16_hex_id_to_uint64(id)
  return nil unless id

  value = id.to_i(16)
  value > MAX_64BIT_UNSIGNED_INT || value.negative? ? 0 : value
end

.generateObject



10
11
12
# File 'lib/jaeger/trace_id.rb', line 10

def self.generate
  rand(TRACE_ID_UPPER_BOUND)
end

.to_hex(id) ⇒ Object

Convert an integer id into a 0 padded hex string. If the string is shorter than 16 characters, it will be padded to 16. If it is longer than 16 characters, it is padded to 32.



37
38
39
40
41
42
43
44
45
46
# File 'lib/jaeger/trace_id.rb', line 37

def self.to_hex(id)
  hex_str = id.to_s(16)

  # pad the string with '0's to 16 or 32 characters
  if hex_str.length > 16
    hex_str.rjust(32, '0')
  else
    hex_str.rjust(16, '0')
  end
end

.uint64_id_to_int64(id) ⇒ Object

Thrift defines ID fields as i64, which is signed, therefore we convert large IDs (> 2^63) to negative longs



30
31
32
# File 'lib/jaeger/trace_id.rb', line 30

def self.uint64_id_to_int64(id)
  id > MAX_64BIT_SIGNED_INT ? id - MAX_64BIT_UNSIGNED_INT - 1 : id
end