Sha256: d79d3b8444806406346f530e756fa7995391b6d37625f463a1f3a1b0676a166a

Contents?: true

Size: 1002 Bytes

Versions: 2

Compression:

Stored size: 1002 Bytes

Contents

# frozen_string_literal: true

module Zipkin
  module TraceId
    TRACE_ID_UPPER_BOUND = 2**64

    # 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

    # An invalid trace identifier, an 8-byte string with all zero bytes.
    INVALID_TRACE_ID = ("\0" * 8).b

    # Generates a valid trace identifier, an 8-byte string with at least one
    # non-zero byte.
    #
    # @return [String] a valid trace ID.
    def self.generate
      loop do
        id = RANDOM.bytes(8)
        return id.unpack1('H*') if id != INVALID_TRACE_ID
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zipkin-1.6.3 lib/zipkin/trace_id.rb
zipkin-1.6.2 lib/zipkin/trace_id.rb