Class: ActiveAgent::Telemetry::Tracer
- Inherits:
-
Object
- Object
- ActiveAgent::Telemetry::Tracer
- Defined in:
- lib/active_agent/telemetry/tracer.rb
Overview
Manages trace creation and lifecycle on the activeagents-telemetry core: builds an ActiveAgents::Telemetry::Trace per unit of work, tracks the current span in thread-local storage, and hands finished traces to the Reporter.
Constant Summary collapse
- CURRENT_SPAN_KEY =
Thread-local storage for current trace context
:active_agent_telemetry_current_span
Instance Attribute Summary collapse
-
#configuration ⇒ Configuration
readonly
Telemetry configuration.
-
#reporter ⇒ Reporter
readonly
The reporter for sending traces.
Instance Method Summary collapse
-
#current_span ⇒ Span?
Returns the current span from thread-local storage.
-
#flush ⇒ void
Flushes buffered traces and waits for delivery to complete, so callers (tests, rails runner, job shutdown) can rely on the traces having been delivered/stored when this returns.
-
#initialize(configuration) ⇒ Tracer
constructor
A new instance of Tracer.
-
#shutdown ⇒ void
Shuts down the tracer and reporter.
-
#span(name, **attributes) ⇒ Span
Creates a standalone span (not within a trace block).
-
#trace(name, **attributes) {|span| ... } ⇒ Object
Creates and executes a new trace.
Constructor Details
Instance Attribute Details
#configuration ⇒ Configuration (readonly)
Returns Telemetry configuration.
18 19 20 |
# File 'lib/active_agent/telemetry/tracer.rb', line 18 def configuration @configuration end |
#reporter ⇒ Reporter (readonly)
Returns The reporter for sending traces.
21 22 23 |
# File 'lib/active_agent/telemetry/tracer.rb', line 21 def reporter @reporter end |
Instance Method Details
#current_span ⇒ Span?
Returns the current span from thread-local storage.
93 94 95 |
# File 'lib/active_agent/telemetry/tracer.rb', line 93 def current_span Thread.current[CURRENT_SPAN_KEY] end |
#flush ⇒ void
This method returns an undefined value.
Flushes buffered traces and waits for delivery to complete, so callers (tests, rails runner, job shutdown) can rely on the traces having been delivered/stored when this returns.
102 103 104 |
# File 'lib/active_agent/telemetry/tracer.rb', line 102 def flush reporter.flush end |
#shutdown ⇒ void
This method returns an undefined value.
Shuts down the tracer and reporter.
109 110 111 |
# File 'lib/active_agent/telemetry/tracer.rb', line 109 def shutdown reporter.shutdown end |
#span(name, **attributes) ⇒ Span
Creates a standalone span (not within a trace block).
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_agent/telemetry/tracer.rb', line 78 def span(name, **attributes) return Telemetry::NullSpan.new unless should_trace? span_type = attributes.delete(:span_type) || :root current = current_span if current current.add_span(name, span_type: span_type, **attributes) else Span.new(name, trace_id: SecureRandom.hex(16), span_type: span_type, **default_attributes.merge(attributes)) end end |
#trace(name, **attributes) {|span| ... } ⇒ Object
Creates and executes a new trace.
Callers may supply the trace id (instrumentation passes the generation's prompt_options) so external records — e.g. solid_agent's persisted generations — can correlate with this trace; otherwise one is generated.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_agent/telemetry/tracer.rb', line 43 def trace(name, **attributes) return yield(Telemetry::NullSpan.new) unless should_trace? trace_id = attributes.delete(:trace_id) || SecureRandom.hex(16) span_type = attributes.delete(:span_type) || :root trace = build_trace(trace_id) root_span = Span.new( name, trace_id: trace_id, span_type: span_type, **default_attributes.merge(attributes) ) trace.add_span(root_span) with_span(root_span) do result = yield(root_span) root_span.finish reporter.report(redact_trace!(trace)) result end rescue StandardError => e if root_span && !root_span.finished? root_span.record_error(e) root_span.finish reporter.report(redact_trace!(trace)) end raise end |