OpenTelemetryObserver

class OpenTelemetryObserver(openTelemetry: OpenTelemetry = GlobalOpenTelemetry.get(), recordContent: Boolean = false, maxContentLength: Int = ConversationAttributes.DEFAULT_MAX_LENGTH) : ConversationObserver

Sends the runs of a graph to OpenTelemetry as traces. Give it to a run with GraphConfig.observers:

val tracing = OpenTelemetryObserver(openTelemetry)
graph.invoke(order, GraphConfig(observers = listOf(tracing)))

A run is a span named telar.run, and each node a span named telar.node <name> inside it. A subgraph is a telar.run inside the span of the node that runs it. A run started inside a span, such as the span of a request to a server, is a child of that span.

  • The run has the attributes telar.thread_id and telar.outcome (completed, interrupted, failed or cancelled), and telar.next_nodes when it paused. It has an event telar.step for each step, with the nodes that ran and those that the edges chose next, and an event telar.checkpoint for each saved checkpoint.

  • A node has the attributes telar.node, telar.step and telar.outcome, and an event telar.retry for each try that failed and was followed by another.

  • An edge with a condition is a span telar.edge <from> inside the run, with telar.edge.from, telar.step (the step whose results it routes, 0 for an edge from START), telar.outcome, and telar.next_nodes, what the condition chose. A plain edge has no span.

  • A failure is recorded on the span of the node and of the run, with the status ERROR. A cancelled node or run is not an error.

The span of a node is current while the node runs, and that of an edge while its condition runs, so the spans of what they call, such as an HTTP client with OpenTelemetry instrumentation, are inside them.

Calls to models and tools. It is also an AgentObserver, so the calls that a node of toolLoop or toolAgent, ask, chatWithProgress or a ChatDecisionModel makes are spans inside the span of the node, or of the edge for the condition of a decisionEdge, named as the semantic conventions for generative AI name them:

  • A call to a model is a span chat <model> of kind CLIENT, with gen_ai.operation.name, gen_ai.request.model (the name of the model), gen_ai.response.model (the model that answered, when the provider says), and the tokens its provider reported: gen_ai.usage.input_tokens (the tokens read from a cache included) and gen_ai.usage.output_tokens, and gen_ai.usage.cache_read.input_tokens and gen_ai.usage.cache_write.input_tokens when they are not 0. A backend prices the call with the model and the tokens. A model whose name is not known, such as one of your own that does not override it, gives a span chat without gen_ai.request.model. A call that failed has the status ERROR, an error.type and the exception.

  • A tool call is a span execute_tool <name>, with gen_ai.tool.name and gen_ai.tool.call.id. A tool that returned an error to the model has the status ERROR and error.type tool_error, although the run goes on. A call that ended without a result, because the run was cancelled or paused, has no status: the span of the node says what happened.

Observer functions are not suspend, so the span of a call is not current while it runs: the span of the HTTP request of a model is inside the span of the node, next to the span chat.

The state of the run is never recorded, nor what a node reports with reportProgress: both can hold personal data.

The conversation, with recordContent: the span of a model call gets what it sent (gen_ai.system_instructions and gen_ai.input.messages) and the answer (gen_ai.output.messages), and the span of a tool call its input (gen_ai.tool.call.arguments) and result (gen_ai.tool.call.result), as JSON in strings, as the conventions write them. A backend such as Langfuse shows them as the input and output of the call. Each text is cut at maxContentLength characters, with a note of how long it was. An image or a PDF becomes a note of its type and size, such as [image/png, 47 KB]: its bytes are never recorded. Nor is the providerContent of a message, such as the thinking of Claude, nor the tools the model was offered. The conversation can hold personal data, such as what the users of the app wrote, so it is off by default: turn it on only when you know who can read the traces.

Parameters

openTelemetry

where the spans go. The default is the instance that the OpenTelemetry Java agent or GlobalOpenTelemetry.set registered, which records nothing when there is none.

recordContent

records the messages of each model call and the input and result of each tool call. Off by default, because they can hold personal data.

maxContentLength

the most characters of each text that recordContent records.

Throws

Constructors

Link copied to clipboard
constructor(openTelemetry: OpenTelemetry = GlobalOpenTelemetry.get(), recordContent: Boolean = false, maxContentLength: Int = ConversationAttributes.DEFAULT_MAX_LENGTH)

Functions

Link copied to clipboard
open override fun edgeContext(edge: ObservedEdge): CoroutineContext
Link copied to clipboard
open override fun nodeContext(node: ObservedNode): CoroutineContext
Link copied to clipboard
open override fun onCheckpointSaved(run: ObservedRun, step: Int, nextNodes: List<String>)
Link copied to clipboard
open override fun onEdgeCompleted(edge: ObservedEdge, targets: List<String>)
Link copied to clipboard
open override fun onEdgeFailed(edge: ObservedEdge, cause: Throwable)
Link copied to clipboard
open override fun onEdgeStarted(edge: ObservedEdge)
Link copied to clipboard
open override fun onModelCallCompleted(call: ObservedModelCall, usage: TokenUsage?)
Link copied to clipboard
open override fun onModelCallFailed(call: ObservedModelCall, cause: Throwable)
Link copied to clipboard
open override fun onModelCallRequest(call: ObservedModelCall, request: ChatRequest)
Link copied to clipboard
open override fun onModelCallResponse(call: ObservedModelCall, response: ChatResponse)
Link copied to clipboard
open override fun onModelCallStarted(call: ObservedModelCall)
Link copied to clipboard
open override fun onNodeCompleted(node: ObservedNode)
Link copied to clipboard
open override fun onNodeFailed(node: ObservedNode, cause: Throwable)
Link copied to clipboard
open override fun onNodeInterrupted(node: ObservedNode)
Link copied to clipboard
open fun onNodeProgress(node: ObservedNode, value: Any)
Link copied to clipboard
open override fun onNodeRetry(node: ObservedNode, attempt: Int, cause: Throwable, pause: Duration)
Link copied to clipboard
open override fun onNodeStarted(node: ObservedNode)
Link copied to clipboard
open override fun onRunCompleted(run: ObservedRun)
Link copied to clipboard
open override fun onRunFailed(run: ObservedRun, cause: Throwable)
Link copied to clipboard
open override fun onRunInterrupted(run: ObservedRun, nextNodes: List<String>)
Link copied to clipboard
open override fun onRunStarted(run: ObservedRun)
Link copied to clipboard
open override fun onStepCompleted(run: ObservedRun, step: Int, nodes: List<String>, nextNodes: List<String>)
Link copied to clipboard
open override fun onToolCallCompleted(call: ObservedToolCall, isError: Boolean)
Link copied to clipboard
open override fun onToolCallFailed(call: ObservedToolCall, cause: Throwable)
Link copied to clipboard
open override fun onToolCallInput(call: ObservedToolCall, toolCall: ToolCall)
Link copied to clipboard
open override fun onToolCallResult(call: ObservedToolCall, result: ChatMessage.ToolResult)
Link copied to clipboard
open override fun onToolCallStarted(call: ObservedToolCall)