Class: LLM::Tracer
- Inherits:
-
Object
- Object
- LLM::Tracer
- Defined in:
- lib/llm/tracer.rb
Overview
The LLM::Tracer is the superclass of all LLM tracers. It can be helpful for implementing instrumentation and hooking into the lifecycle of an LLM request. See LLM::Tracer::Telemetry, and LLM::Tracer::Logger for example tracer implementations.
Defined Under Namespace
Classes: Logger, Null, Telemetry
Instance Method Summary collapse
-
#flush! ⇒ nil
Flush the tracer.
-
#initialize(provider, options = {}) ⇒ Tracer
constructor
A new instance of Tracer.
- #inspect ⇒ String
-
#on_request_error(ex:, span:)
Called when an LLM provider request fails.
-
#on_request_finish(operation:, res:, model: nil, span: nil)
Called after an LLM provider request succeeds.
-
#on_request_start(operation:, model: nil)
Called before an LLM provider request is executed.
-
#on_tool_error(ex:, span:)
Called when a local tool/function raises.
-
#on_tool_finish(result:, span:)
Called after a local tool/function succeeds.
-
#on_tool_start(id:, name:, arguments:, model:)
Called before a local tool/function executes.
- #spans ⇒ Array
-
#start_trace(trace_group_id: nil, name: "llm", attributes: {}) ⇒ self
Opens a trace group so subsequent LLM spans share the same OpenTelemetry trace_id (and appear as one trace in backends like Langfuse).
-
#stop_trace ⇒ self
Finishes the trace group started by #start_trace.
Constructor Details
#initialize(provider, options = {}) ⇒ Tracer
Returns a new instance of Tracer.
21 22 23 24 |
# File 'lib/llm/tracer.rb', line 21 def initialize(provider, = {}) @provider = provider = {} end |
Instance Method Details
#flush! ⇒ nil
This method is only implemented by the Telemetry tracer. It is a noop for other tracers.
Flush the tracer
135 136 137 |
# File 'lib/llm/tracer.rb', line 135 def flush! nil end |
#inspect ⇒ String
119 120 121 |
# File 'lib/llm/tracer.rb', line 119 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} @provider=#{@provider.class} @tracer=#{@tracer.inspect}>" end |
#on_request_error(ex:, span:)
This method returns an undefined value.
Called when an LLM provider request fails.
51 52 53 |
# File 'lib/llm/tracer.rb', line 51 def on_request_error(ex:, span:) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#on_request_finish(operation:, res:, model: nil, span: nil)
This method returns an undefined value.
Called after an LLM provider request succeeds.
42 43 44 |
# File 'lib/llm/tracer.rb', line 42 def on_request_finish(operation:, res:, model: nil, span: nil) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#on_request_start(operation:, model: nil)
This method returns an undefined value.
Called before an LLM provider request is executed.
31 32 33 |
# File 'lib/llm/tracer.rb', line 31 def on_request_start(operation:, model: nil) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#on_tool_error(ex:, span:)
This method returns an undefined value.
Called when a local tool/function raises.
88 89 90 |
# File 'lib/llm/tracer.rb', line 88 def on_tool_error(ex:, span:) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#on_tool_finish(result:, span:)
This method returns an undefined value.
Called after a local tool/function succeeds.
77 78 79 |
# File 'lib/llm/tracer.rb', line 77 def on_tool_finish(result:, span:) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#on_tool_start(id:, name:, arguments:, model:)
This method returns an undefined value.
Called before a local tool/function executes.
66 67 68 |
# File 'lib/llm/tracer.rb', line 66 def on_tool_start(id:, name:, arguments:, model:) raise NotImplementedError, "#{self.class} does not implement '#{__method__}'" end |
#spans ⇒ Array
125 126 127 |
# File 'lib/llm/tracer.rb', line 125 def spans [] end |
#start_trace(trace_group_id: nil, name: "llm", attributes: {}) ⇒ self
Opens a trace group so subsequent LLM spans share the same OpenTelemetry trace_id (and appear as one trace in backends like Langfuse). When +trace_group_id+ is a string, it is used to derive the trace_id.
105 106 107 |
# File 'lib/llm/tracer.rb', line 105 def start_trace(trace_group_id: nil, name: "llm", attributes: {}) self end |
#stop_trace ⇒ self
Finishes the trace group started by #start_trace. Safe to call even if no trace is active.
113 114 115 |
# File 'lib/llm/tracer.rb', line 113 def stop_trace self end |