Module: Uptrace::IdGenerator
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 theRandom.bytes
class method) and with Ruby 3.0+ (which deprecatesRandom::DEFAULT
). When we drop support for Ruby 2.5, this can simply be replaced with the classRandom
. Random.respond_to?(:bytes) ? Random : Random.new
Instance Method Summary collapse
-
#generate_span_id ⇒ String
Generates a valid span identifier, an 8-byte string with at least one non-zero byte.
-
#generate_trace_id ⇒ String
Generates a valid trace identifier, a 16-byte string with at least one non-zero byte.
Instance Method Details
#generate_span_id ⇒ String
Generates a valid span identifier, an 8-byte string with at least one non-zero byte.
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_id ⇒ String
Generates a valid trace identifier, a 16-byte string with at least one non-zero byte.
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 |