Class: Instana::SpanContext

Inherits:
OpenTelemetry::Trace::SpanContext
  • Object
show all
Defined in:
lib/instana/trace/span_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trace_id: Trace.generate_trace_id, span_id: Trace.generate_span_id, trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT, tracestate: OpenTelemetry::Trace::Tracestate::DEFAULT, remote: false, level: 1, baggage: {}) ⇒ SpanContext

Create a new SpanContext

Parameters:

  • tid (Integer)

    the trace ID

  • sid (Integer)

    the span ID

  • level (Integer) (defaults to: 1)

    default 1

  • baggage (Hash) (defaults to: {})

    baggage applied to this trace



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/instana/trace/span_context.rb', line 16

def initialize( # rubocop:disable Lint/MissingSuper, Metrics/ParameterLists
  trace_id: Trace.generate_trace_id,
  span_id: Trace.generate_span_id,
  trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT, # Todo - implement traceflags
  tracestate: OpenTelemetry::Trace::Tracestate::DEFAULT,  # Todo - implement tracestates
  remote: false,
  level: 1,
  baggage: {}
)
  @trace_id = trace_id
  @span_id = span_id
  @trace_flags = trace_flags
  @tracestate = tracestate
  @remote = remote
  @level = Integer(level || 1)
  @baggage = baggage || {}
end

Instance Attribute Details

#baggageObject

Returns the value of attribute baggage.



6
7
8
# File 'lib/instana/trace/span_context.rb', line 6

def baggage
  @baggage
end

#levelObject (readonly)

Returns the value of attribute level.



7
8
9
# File 'lib/instana/trace/span_context.rb', line 7

def level
  @level
end

#span_idObject

Returns the value of attribute span_id.



6
7
8
# File 'lib/instana/trace/span_context.rb', line 6

def span_id
  @span_id
end

#trace_idObject

Returns the value of attribute trace_id.



6
7
8
# File 'lib/instana/trace/span_context.rb', line 6

def trace_id
  @trace_id
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/instana/trace/span_context.rb', line 78

def active?
  @level == 1
end

#span_id_headerObject



38
39
40
# File 'lib/instana/trace/span_context.rb', line 38

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

#to_hashObject



70
71
72
# File 'lib/instana/trace/span_context.rb', line 70

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

#trace_id_headerObject



34
35
36
# File 'lib/instana/trace/span_context.rb', line 34

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

#trace_parent_headerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/instana/trace/span_context.rb', line 42

def trace_parent_header
  trace = (@baggage[:external_trace_id] || trace_id_header).rjust(32, '0')
  parent = span_id_header.rjust(16, '0')
  flags = if @baggage[:external_trace_flags]
            # Parse external flags as 8-bit hex, clear LSB, then set LSB based on level
            external_flags = @baggage[:external_trace_flags].to_i(16) & 0xFE # Clear LSB
            combined_flags = external_flags | (@level == 1 ? 1 : 0) # Set LSB based on level
            combined_flags = [combined_flags, 0xFF].min # Cap at 8-bit max
            format('%02x', combined_flags)
          else
            @level == 1 ? "03" : "02"
          end
  flags = "03" if flags > "03"
  "00-#{trace}-#{parent}-#{flags}"
end

#trace_state_headerObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/instana/trace/span_context.rb', line 58

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.take(32).reject { |v| v.nil? }.join(',')
end

#valid?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/instana/trace/span_context.rb', line 74

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