Class: ActiveAgents::Telemetry::Trace
- Inherits:
-
Object
- Object
- ActiveAgents::Telemetry::Trace
- Defined in:
- lib/activeagents/telemetry/trace.rb
Overview
A collection of spans sharing a trace_id, plus the service metadata the ingest endpoint keys on. Adapters build one per unit of work — for chat adapters, one per conversation turn.
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#resource_attributes ⇒ Object
Returns the value of attribute resource_attributes.
-
#service_name ⇒ Object
Returns the value of attribute service_name.
-
#spans ⇒ Object
readonly
Returns the value of attribute spans.
-
#trace_id ⇒ Object
readonly
Returns the value of attribute trace_id.
Instance Method Summary collapse
-
#add_span(span) ⇒ Object
Adopts the span into this trace, stamping the trace_id so adapters cannot emit a span that belongs to no trace.
- #empty? ⇒ Boolean
-
#initialize(trace_id: nil, service_name: nil, environment: nil, resource_attributes: {}) ⇒ Trace
constructor
A new instance of Trace.
- #root ⇒ Object
-
#span(name, type:, parent: nil, **options) ⇒ Object
Builds and adopts a span in one step.
- #to_h ⇒ Object
Constructor Details
#initialize(trace_id: nil, service_name: nil, environment: nil, resource_attributes: {}) ⇒ Trace
Returns a new instance of Trace.
14 15 16 17 18 19 20 |
# File 'lib/activeagents/telemetry/trace.rb', line 14 def initialize(trace_id: nil, service_name: nil, environment: nil, resource_attributes: {}) @trace_id = trace_id || SecureRandom.hex(16) @service_name = service_name @environment = environment @resource_attributes = resource_attributes || {} @spans = [] end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
12 13 14 |
# File 'lib/activeagents/telemetry/trace.rb', line 12 def environment @environment end |
#resource_attributes ⇒ Object
Returns the value of attribute resource_attributes.
12 13 14 |
# File 'lib/activeagents/telemetry/trace.rb', line 12 def resource_attributes @resource_attributes end |
#service_name ⇒ Object
Returns the value of attribute service_name.
12 13 14 |
# File 'lib/activeagents/telemetry/trace.rb', line 12 def service_name @service_name end |
#spans ⇒ Object (readonly)
Returns the value of attribute spans.
11 12 13 |
# File 'lib/activeagents/telemetry/trace.rb', line 11 def spans @spans end |
#trace_id ⇒ Object (readonly)
Returns the value of attribute trace_id.
11 12 13 |
# File 'lib/activeagents/telemetry/trace.rb', line 11 def trace_id @trace_id end |
Instance Method Details
#add_span(span) ⇒ Object
Adopts the span into this trace, stamping the trace_id so adapters cannot emit a span that belongs to no trace.
24 25 26 27 28 |
# File 'lib/activeagents/telemetry/trace.rb', line 24 def add_span(span) span.trace_id = trace_id @spans << span span end |
#empty? ⇒ Boolean
39 40 41 |
# File 'lib/activeagents/telemetry/trace.rb', line 39 def empty? @spans.empty? end |
#root ⇒ Object
35 36 37 |
# File 'lib/activeagents/telemetry/trace.rb', line 35 def root @spans.find { |span| span.type == "root" } end |
#span(name, type:, parent: nil, **options) ⇒ Object
Builds and adopts a span in one step.
31 32 33 |
# File 'lib/activeagents/telemetry/trace.rb', line 31 def span(name, type:, parent: nil, **) add_span(Span.new(name, type: type, parent_span_id: parent&.span_id, **)) end |
#to_h ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/activeagents/telemetry/trace.rb', line 43 def to_h { "trace_id" => trace_id, "service_name" => service_name, "environment" => environment, "timestamp" => , "resource_attributes" => resource_attributes, "spans" => spans.flat_map { |span| flatten(span) }.map(&:to_h) } end |