Module: Agents::Instrumentation
- Defined in:
- lib/agents/instrumentation.rb,
lib/agents/instrumentation/constants.rb,
lib/agents/instrumentation/tracing_callbacks.rb
Overview
Optional OpenTelemetry instrumentation for the ai-agents gem. Emits OTel spans for LLM calls, tool executions, and agent handoffs that render correctly in Langfuse and other OTel-compatible backends.
The gem only emits spans — the consumer configures the OTel exporter and provides a tracer. The opentelemetry-api gem is NOT declared as a dependency; consumers must include it in their own bundle.
Defined Under Namespace
Modules: Constants Classes: TracingCallbacks
Class Method Summary collapse
-
.install(runner, tracer:, trace_name: Constants::SPAN_RUN, span_attributes: {}, attribute_provider: nil) ⇒ Agents::AgentRunner?
Install OTel tracing on a runner via callbacks.
-
.otel_available? ⇒ Boolean
Check if the opentelemetry-api gem is available.
Class Method Details
.install(runner, tracer:, trace_name: Constants::SPAN_RUN, span_attributes: {}, attribute_provider: nil) ⇒ Agents::AgentRunner?
Install OTel tracing on a runner via callbacks. No-op if opentelemetry-api is not available. Idempotent per runner instance: first install wins.
Session grouping: set ‘context` when calling `runner.run()`. TracingCallbacks automatically reads it per-request and sets `langfuse.session.id`.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/agents/instrumentation.rb', line 57 def self.install(runner, tracer:, trace_name: Constants::SPAN_RUN, span_attributes: {}, attribute_provider: nil) return unless otel_available? INSTALL_MUTEX.synchronize do return runner if instrumentation_installed?(runner) callbacks = TracingCallbacks.new( tracer: tracer, trace_name: trace_name, span_attributes: span_attributes, attribute_provider: attribute_provider ) register_callbacks(runner, callbacks) mark_instrumentation_installed(runner) end runner end |
.otel_available? ⇒ Boolean
Check if the opentelemetry-api gem is available.
102 103 104 105 106 107 |
# File 'lib/agents/instrumentation.rb', line 102 def self.otel_available? require "opentelemetry-api" true rescue LoadError false end |