Class: ActiveAgents::Telemetry::Trace

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#environmentObject

Returns the value of attribute environment.



12
13
14
# File 'lib/activeagents/telemetry/trace.rb', line 12

def environment
  @environment
end

#resource_attributesObject

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_nameObject

Returns the value of attribute service_name.



12
13
14
# File 'lib/activeagents/telemetry/trace.rb', line 12

def service_name
  @service_name
end

#spansObject (readonly)

Returns the value of attribute spans.



11
12
13
# File 'lib/activeagents/telemetry/trace.rb', line 11

def spans
  @spans
end

#trace_idObject (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

Returns:

  • (Boolean)


39
40
41
# File 'lib/activeagents/telemetry/trace.rb', line 39

def empty?
  @spans.empty?
end

#rootObject



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, **options)
  add_span(Span.new(name, type: type, parent_span_id: parent&.span_id, **options))
end

#to_hObject



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" => timestamp,
    "resource_attributes" => resource_attributes,
    "spans" => spans.flat_map { |span| flatten(span) }.map(&:to_h)
  }
end