class LittleGhost::Tracing::OpenTelemetry
OpenTelemetry turns LittleGhost lifecycle notifications into GenAI spans and events. It brings agents, model calls, tools, workflows, and token usage into the same traces as the rest of an application.
LittleGhost.configure do |config| config.instrument LittleGhost::Tracing::OpenTelemetry.new end
LittleGhost depends only on opentelemetry-api. Install and configure the desired SDK, processors, and exporters before registering this subscriber.
Content and trust
Prompts, responses, messages, tool arguments, and exception content are omitted by default. They appear only when Instrumentation has an explicit, scrubbed Support::ContentCapture policy.
Adding attributes and limiting payloads
enrich adds application attributes, such as a span kind your tracing backend understands, to every lifecycle notification. payload_limiter shortens captured content that a backend would otherwise reject.
tracing = LittleGhost::Tracing::OpenTelemetry.new( enrich: ->(phase, name, attributes) { {"openinference.span.kind" => (name == :model) ? "LLM" : "CHAIN"} }, payload_limiter: LittleGhost::Tracing::PayloadLimiter.new(max_bytes: 64_000) )
Public Class Methods
# File lib/little_ghost/tracing/open_telemetry.rb, line 118 def initialize(tracer: nil, enrich: nil, payload_limiter: nil) raise ArgumentError, "enrich must respond to call" if enrich && !enrich.respond_to?(:call) if payload_limiter && !payload_limiter.respond_to?(:call) raise ArgumentError, "payload_limiter must respond to call" end @tracer = tracer @enrich = enrich @payload_limiter = payload_limiter @entries = {} @mutex = Mutex.new end
Uses tracer or the global tracer provider.
enrich is an optional callable that receives the phase (:start, :finish, or :emit), the notification name as a Symbol, and its attributes, and returns a Hash of attributes to add, or nil. A key containing a dot, such as "session.id", is used as the attribute name; other keys follow the same mapping as LittleGhost’s own attributes. Returned values are normalized like other attributes, so strings are capped at 1,024 characters. If enrich raises or returns anything other than a Hash or nil, a little_ghost.tracing.enrich_failed warning names the error class and the notification is recorded without the additions, so spans still start and finish.
payload_limiter is an optional callable, usually a PayloadLimiter, that receives each span’s or event’s OpenTelemetry attributes as a Hash and returns the Hash to record. It runs on the attributes set when a span starts, on those set when it finishes, on exception events, and on the attributes passed to with_span. If it raises or returns something other than a Hash, String attributes longer than 1,024 characters are omitted, a little_ghost.tracing.payload_limit_failed warning is emitted through Events, and the span still finishes.
Public Instance Methods
Source
# File lib/little_ghost/tracing/open_telemetry.rb, line 142 def emit(name, attributes) add_event(name.to_sym, prepare_attributes(:emit, name.to_sym, attributes)) end
Adds a structured event to the correlated active span.
Source
# File lib/little_ghost/tracing/open_telemetry.rb, line 137 def finish(name, attributes) finish_span(name.to_sym, prepare_attributes(:finish, name.to_sym, attributes)) end
Finishes the span correlated by operation ID.
Source
# File lib/little_ghost/tracing/open_telemetry.rb, line 179 def flush(timeout: nil) provider = ::OpenTelemetry.tracer_provider provider.force_flush(timeout:) if provider.respond_to?(:force_flush) end
Flushes through the configured tracer provider when supported.
Source
# File lib/little_ghost/tracing/open_telemetry.rb, line 185 def shutdown(timeout: nil) spans = @mutex.synchronize do current = @entries.values.map { |entry| entry.fetch(:span) }.uniq @entries = {} current end spans.each(&:finish) end
Finishes spans still owned by this subscriber.
Source
# File lib/little_ghost/tracing/open_telemetry.rb, line 132 def start(name, attributes) start_span(name.to_sym, prepare_attributes(:start, name.to_sym, attributes)) end
Starts a span for a lifecycle operation.
# File lib/little_ghost/tracing/open_telemetry.rb, line 147 def trace_context(operation_id: nil, **) span = @mutex.synchronize { @entries.dig(operation_id, :span) } context = span&.context return {} unless context&.valid? carrier = {} trace_context_propagator.inject( carrier, context: ::OpenTelemetry::Trace.context_with_span(span) ) carrier.slice("traceparent", "tracestate").merge(trace_id: context.hex_trace_id) rescue context&.valid? ? {trace_id: context.hex_trace_id} : {} end
Supplies W3C propagation fields for an active operation when known.
# File lib/little_ghost/tracing/open_telemetry.rb, line 164 def with_span(name, attributes:, parent_operation_id: nil) parent = @mutex.synchronize { @entries[parent_operation_id] } parent_context = parent && ::OpenTelemetry::Trace.context_with_span(parent.fetch(:span)) options = {kind: :internal, attributes: limit_attributes(attributes)} options[:with_parent] = parent_context if parent_context span = tracer.start_span(name, **options) yield span rescue => error span.status = ::OpenTelemetry::Trace::Status.error(error.class.name) if span raise ensure span&.finish end
Wraps a block in a standalone internal span. Prefer lifecycle Instrumentation methods for normal framework operations.