Module: Uptrace::IdGenerator

Extended by:
IdGenerator
Included in:
IdGenerator
Defined in:
lib/uptrace/id_generator.rb

Overview

Uptrace client that configures OpenTelemetry SDK to use Uptrace exporter.

Constant Summary collapse

INVALID_SPAN_ID =

An invalid span identifier, an 8-byte string with all zero bytes.

("\0" * 8).b
RANDOM =

Random number generator for generating IDs. This is an object that can respond to #bytes and uses the system PRNG. The current logic is compatible with Ruby 2.5 (which does not implement the Random.bytes class method) and with Ruby 3.0+ (which deprecates Random::DEFAULT). When we drop support for Ruby 2.5, this can simply be replaced with the class Random.

Returns:

  • (#bytes)
Random.respond_to?(:bytes) ? Random : Random.new

Instance Method Summary collapse

Instance Method Details

#generate_span_idString

Generates a valid span identifier, an 8-byte string with at least one non-zero byte.

Returns:

  • (String)

    a valid span ID.



36
37
38
39
40
41
# File 'lib/uptrace/id_generator.rb', line 36

def generate_span_id
  loop do
    id = Random.bytes(8)
    return id unless id == INVALID_SPAN_ID
  end
end

#generate_trace_idString

Generates a valid trace identifier, a 16-byte string with at least one non-zero byte.

Returns:

  • (String)

    a valid trace ID.



25
26
27
28
29
30
# File 'lib/uptrace/id_generator.rb', line 25

def generate_trace_id
  time = (Time.now.to_f * 1_000_000_000).to_i
  high = [time >> 32, time & 0xFFFFFFFF].pack('NN')
  low = RANDOM.bytes(8)
  high << low
end