Sha256: fe03b998210d719b43886d0e7fe47aba58d9778abcdb1d92a25cc70af9b5105d

Contents?: true

Size: 1.15 KB

Versions: 18

Compression:

Stored size: 1.15 KB

Contents

module Hermes
  class TraceContext
    attr_reader :origin_event_headers
    private     :origin_event_headers

    DELIMITER = ";".freeze
    SPAN_LENGTH = 64
    private_constant :DELIMITER, :SPAN_LENGTH

    def initialize(origin_event_headers = {})
      @origin_event_headers = origin_event_headers.to_h
    end

    def trace
      @trace ||= origin_event_headers.fetch(Hermes::B3PropagationModelHeaders.trace_id_key, SecureRandom.hex(32))
    end

    def span
      @span ||= [trace[0..last_trace_index_for_span], DELIMITER, service_seed_for_span, DELIMITER, uuid].join
    end

    def parent_span
      origin_event_headers.fetch(Hermes::B3PropagationModelHeaders.span_id_key, nil)
    end

    def service
      Hermes.configuration.application_prefix or raise "missing application prefix!"
    end

    private

    def service_seed_for_span
      service[0..14]
    end

    def uuid
      @uuid ||= SecureRandom.uuid
    end

    # expected length is 64, so the maximum index will be 63
    # - 1 due to the semicolon
    # - 1 due to the semicolon
    def last_trace_index_for_span
      (64 - 1) - 1 - service_seed_for_span.size - 1 - uuid.size
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
hermes-rb-0.10.1 lib/hermes/trace_context.rb
hermes-rb-0.10.0 lib/hermes/trace_context.rb
hermes-rb-0.9.1 lib/hermes/trace_context.rb
hermes-rb-0.9.0 lib/hermes/trace_context.rb
hermes-rb-0.8.0 lib/hermes/trace_context.rb
hermes-rb-0.7.5 lib/hermes/trace_context.rb
hermes-rb-0.7.4 lib/hermes/trace_context.rb
hermes-rb-0.7.3 lib/hermes/trace_context.rb
hermes-rb-0.7.2 lib/hermes/trace_context.rb
hermes-rb-0.7.1 lib/hermes/trace_context.rb
hermes-rb-0.7.0 lib/hermes/trace_context.rb
hermes-rb-0.6.1 lib/hermes/trace_context.rb
hermes-rb-0.6.0 lib/hermes/trace_context.rb
hermes-rb-0.5.0 lib/hermes/trace_context.rb
hermes-rb-0.4.0 lib/hermes/trace_context.rb
hermes-rb-0.3.1 lib/hermes/trace_context.rb
hermes-rb-0.3.0 lib/hermes/trace_context.rb
hermes-rb-0.2.0 lib/hermes/trace_context.rb