lib/opentelemetry/trace.rb in opentelemetry-api-0.12.0 vs lib/opentelemetry/trace.rb in opentelemetry-api-0.12.1

- old
+ new

@@ -11,12 +11,22 @@ module Trace extend self CURRENT_SPAN_KEY = Context.create_key('current-span') - private_constant :CURRENT_SPAN_KEY + # 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`. + # + # @return [#bytes] + RANDOM = Random.respond_to?(:bytes) ? Random : Random::DEFAULT + private_constant :CURRENT_SPAN_KEY, :RANDOM + # An invalid trace identifier, a 16-byte string with all zero bytes. INVALID_TRACE_ID = ("\0" * 16).b # An invalid span identifier, an 8-byte string with all zero bytes. INVALID_SPAN_ID = ("\0" * 8).b @@ -25,21 +35,21 @@ # non-zero byte. # # @return [String] a valid trace ID. def generate_trace_id loop do - id = Random::DEFAULT.bytes(16) + id = RANDOM.bytes(16) return id unless id == INVALID_TRACE_ID end end # Generates a valid span identifier, an 8-byte string with at least one # non-zero byte. # # @return [String] a valid span ID. def generate_span_id loop do - id = Random::DEFAULT.bytes(8) + id = RANDOM.bytes(8) return id unless id == INVALID_SPAN_ID end end # Returns the current span from the current or provided context