Class: A2A::Monitoring::DistributedTracing::TraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/monitoring/distributed_tracing.rb

Overview

Trace context for distributed tracing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trace_id:, span_id:, trace_flags: 1, tracestate: nil) ⇒ TraceContext

Initialize trace context

Parameters:

  • trace_id (String)

    Trace ID (32 hex characters)

  • span_id (String)

    Span ID (16 hex characters)

  • trace_flags (Integer) (defaults to: 1)

    Trace flags

  • tracestate (String, nil) (defaults to: nil)

    Trace state



148
149
150
151
152
153
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 148

def initialize(trace_id:, span_id:, trace_flags: 1, tracestate: nil)
  @trace_id = trace_id
  @span_id = span_id
  @trace_flags = trace_flags
  @tracestate = tracestate
end

Instance Attribute Details

#span_idObject (readonly)

Returns the value of attribute span_id.



139
140
141
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 139

def span_id
  @span_id
end

#trace_flagsObject (readonly)

Returns the value of attribute trace_flags.



139
140
141
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 139

def trace_flags
  @trace_flags
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



139
140
141
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 139

def trace_id
  @trace_id
end

#tracestateObject (readonly)

Returns the value of attribute tracestate.



139
140
141
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 139

def tracestate
  @tracestate
end

Class Method Details

.parse(traceparent, tracestate = nil) ⇒ TraceContext?

Parse trace context from traceparent header

Parameters:

  • traceparent (String)

    Traceparent header value

  • tracestate (String, nil) (defaults to: nil)

    Tracestate header value

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 161

def self.parse(traceparent, tracestate = nil)
  # Format: 00-{trace_id}-{span_id}-{trace_flags}
  parts = traceparent.split("-")
  return nil unless parts.size == 4 && parts[0] == "00"

  new(
    trace_id: parts[1],
    span_id: parts[2],
    trace_flags: parts[3].to_i(16),
    tracestate: tracestate
  )
rescue StandardError
  nil
end

Instance Method Details

#create_childTraceContext

Create child context with new span ID

Returns:



188
189
190
191
192
193
194
195
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 188

def create_child
  self.class.new(
    trace_id: @trace_id,
    span_id: generate_span_id,
    trace_flags: @trace_flags,
    tracestate: @tracestate
  )
end

#generate_span_idString (private)

Generate a new span ID

Returns:

  • (String)

    16-character hex span ID



203
204
205
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 203

def generate_span_id
  SecureRandom.hex(8)
end

#to_traceparentString

Convert to traceparent header format

Returns:

  • (String)

    Traceparent header value



180
181
182
# File 'lib/a2a/monitoring/distributed_tracing.rb', line 180

def to_traceparent
  "00-#{@trace_id}-#{@span_id}-#{@trace_flags.to_s(16).rjust(2, '0')}"
end