OtlpObserver

class OtlpObserver(client: HttpClient, scope: CoroutineScope, endpoint: String = DEFAULT_ENDPOINT, serviceName: String = DEFAULT_SERVICE_NAME, resource: Map<String, String> = emptyMap(), headers: Map<String, String> = emptyMap(), exportDelay: Duration = 1.seconds, recordContent: Boolean = false, maxContentLength: Int = ConversationAttributes.DEFAULT_MAX_LENGTH, onExportFailed: (OtlpExportException) -> Unit = {}) : ConversationObserver

Sends the runs of a graph as traces to an OpenTelemetry collector, or to any backend that receives OTLP over HTTP, on every platform: Android, iOS, desktop, servers and the browser. Give it to a run with GraphConfig.observers:

val tracing = OtlpObserver(httpClient, appScope, endpoint = "https://collector.example.com/v1/traces")
graph.invoke(order, GraphConfig(observers = listOf(tracing)))

The spans are the same as those of OpenTelemetryObserver, which is for the JVM. 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.

  • 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, as an event exception and the status ERROR. A cancelled node or run is not an error.

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, so that a backend that knows them shows them as calls to a model:

telar.run                        telar.thread_id=ticket-42
├─ telar.node model telar.step=1
│ └─ chat gpt-5 gen_ai.request.model=gpt-5 gen_ai.usage.input_tokens=812
├─ telar.node tools telar.step=2
│ └─ execute_tool order_status gen_ai.tool.call.id=toolu_01...
└─ telar.node model telar.step=3
└─ chat gpt-5 gen_ai.request.model=gpt-5 gen_ai.usage.input_tokens=901
  • 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 an event 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 cannot be current while it runs: 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.

Sending. A span is sent when it ends, together with the others that end within exportDelay, as OTLP/HTTP with JSON, in a coroutine of scope. The app creates the client, with the engine of its platform, and closes it. Nothing is ever thrown into a run: a request that fails is reported to onExportFailed and its spans are dropped. When more spans wait than can be sent, the oldest are dropped. Call flush before the app ends, so that the last spans are sent.

In the browser, the collector has to allow the origin of the page, for example with cors.allowed_origins in the http protocol of the otlp receiver of the OpenTelemetry Collector. A key in headers can be read by everyone who opens the page.

What a node calls. While a node or the condition of an edge runs, its coroutine context holds a TraceContext with the ids of its span. The Traceparent plugin of Ktor sends them with each request, so that a server of the app records its spans inside the node.

Inside a trace of the app. A run is a trace of its own, unless its coroutine context holds a TraceContext: then the run is inside that span. The app puts one there, for example from the traceparent header of the request that started the run (see TraceContext.parse), and a run started inside a node traced by an OtlpObserver already has the one of that node.

Parameters

client

sends the spans. Its engine decides the platform: Js in the browser, Darwin on iOS, OkHttp on Android, CIO or Java on the JVM.

scope

the coroutines that send the spans run in it. Cancel it, and nothing more is sent except by flush.

endpoint

the URL of the traces, /v1/traces of the collector. The default is a collector on the same machine.

serviceName

the service.name of the spans, the name a backend shows for the app.

resource

more attributes of the app, such as service.version or deployment.environment.

headers

sent with each request, for example the key of a backend that receives OTLP itself.

exportDelay

how long a span that ended waits for others before they are sent together.

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.

onExportFailed

told when spans could not be sent. It must not throw.

Throws

if endpoint is blank, exportDelay is negative or maxContentLength is less than 1.

Constructors

Link copied to clipboard
constructor(client: HttpClient, scope: CoroutineScope, endpoint: String = DEFAULT_ENDPOINT, serviceName: String = DEFAULT_SERVICE_NAME, resource: Map<String, String> = emptyMap(), headers: Map<String, String> = emptyMap(), exportDelay: Duration = 1.seconds, recordContent: Boolean = false, maxContentLength: Int = ConversationAttributes.DEFAULT_MAX_LENGTH, onExportFailed: (OtlpExportException) -> Unit = {})

Functions

Link copied to clipboard
open override fun edgeContext(edge: ObservedEdge): CoroutineContext
Link copied to clipboard
suspend fun flush()

Sends every span that ended and was not sent yet, and returns when the collector answered. Call it before the app ends, or when a page in the browser is hidden. A span that has not ended, of a run that is still going, is sent when it ends. Failures go to onExportFailed.

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)