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[:session_id] when calling runner.run().
TracingCallbacks automatically reads it per-request and sets langfuse.session.id.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/agents/instrumentation.rb', line 75 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.
120 121 122 123 124 125 |
# File 'lib/agents/instrumentation.rb', line 120 def self.otel_available? require "opentelemetry-api" true rescue LoadError false end |