Class: Instana::SpanContext
- Inherits:
-
OpenTelemetry::Trace::SpanContext
- Object
- OpenTelemetry::Trace::SpanContext
- Instana::SpanContext
- Defined in:
- lib/instana/trace/span_context.rb
Instance Attribute Summary collapse
-
#baggage ⇒ Object
Returns the value of attribute baggage.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#span_id ⇒ Object
Returns the value of attribute span_id.
-
#trace_id ⇒ Object
Returns the value of attribute trace_id.
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#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
constructor
Create a new SpanContext.
- #span_id_header ⇒ Object
- #to_hash ⇒ Object
- #trace_id_header ⇒ Object
- #trace_parent_header ⇒ Object
- #trace_state_header ⇒ Object
- #valid? ⇒ Boolean
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
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
#baggage ⇒ Object
Returns the value of attribute baggage.
6 7 8 |
# File 'lib/instana/trace/span_context.rb', line 6 def baggage @baggage end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
7 8 9 |
# File 'lib/instana/trace/span_context.rb', line 7 def level @level end |
#span_id ⇒ Object
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_id ⇒ Object
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
78 79 80 |
# File 'lib/instana/trace/span_context.rb', line 78 def active? @level == 1 end |
#span_id_header ⇒ Object
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_hash ⇒ Object
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_header ⇒ Object
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_header ⇒ Object
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_header ⇒ Object
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
74 75 76 |
# File 'lib/instana/trace/span_context.rb', line 74 def valid? @baggage && @trace_id && !@trace_id.empty? end |