Sha256: 51a7cd19a72e627f1deb2aecd5446b28220939899768b02e0835ea3f294fcd87

Contents?: true

Size: 1.48 KB

Versions: 12

Compression:

Stored size: 1.48 KB

Contents

# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2017

module Instana
  class SpanContext
    attr_accessor :trace_id
    attr_accessor :span_id
    attr_accessor :baggage
    attr_reader :level

    # Create a new SpanContext
    #
    # @param tid [Integer] the trace ID
    # @param sid [Integer] the span ID
    # @param level [Integer] default 1
    # @param baggage [Hash] baggage applied to this trace
    #
    def initialize(tid, sid, level = 1, baggage = {})
      @trace_id = tid
      @span_id = sid
      @level = Integer(level || 1)
      @baggage = baggage || {}
    end

    def trace_id_header
      ::Instana::Util.id_to_header(@trace_id)
    end

    def span_id_header
      ::Instana::Util.id_to_header(@span_id)
    end

    def trace_parent_header
      trace = (@baggage[:external_trace_id] || trace_id_header).rjust(32, '0')
      parent = span_id_header.rjust(16, '0')
      flags = @level == 1 ? "01" : "00"

      "00-#{trace}-#{parent}-#{flags}"
    end

    def trace_state_header
      external_state = @baggage[:external_state] || ''
      state = external_state.split(/,/)

      if @level == 1
        state = state.reject { |s| s.start_with?('in=') }
        state.unshift("in=#{trace_id_header};#{span_id_header}")
      end

      state.compact.join(',')
    end

    def to_hash
      { :trace_id => @trace_id, :span_id => @span_id }
    end

    def valid?
      @baggage && @trace_id && !@trace_id.emtpy?
    end

    def active?
      @level == 1
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
instana-1.200.0.pre1 lib/instana/tracing/span_context.rb
instana-1.199.6 lib/instana/tracing/span_context.rb
instana-1.199.5 lib/instana/tracing/span_context.rb
instana-1.199.4 lib/instana/tracing/span_context.rb
instana-1.199.3 lib/instana/tracing/span_context.rb
instana-1.199.2 lib/instana/tracing/span_context.rb
instana-1.199.1 lib/instana/tracing/span_context.rb
instana-1.199.0 lib/instana/tracing/span_context.rb
instana-1.198.0 lib/instana/tracing/span_context.rb
instana-1.198.0.pre1 lib/instana/tracing/span_context.rb
instana-1.197.0 lib/instana/tracing/span_context.rb
instana-1.197.0.pre2 lib/instana/tracing/span_context.rb