OpenTelemetryTracing

org.llm4s.trace.OpenTelemetryTracing
See theOpenTelemetryTracing companion object

Exports trace events as OpenTelemetry spans over OTLP/gRPC.

An event placed in a trace (TraceEvent.span, design 4.58) becomes a span with exactly the placement's ids: its trace and span id, its parent - which may be a remote parent, a span another process exported - its start time and, as its end, the event's timestamp. An agent's run is so one tree: invoke_agent <agent> (from AgentRunEnded) over chat <model> (agent.model_call_completed), execute_tool <tool> (agent.tool_executed) and the run's other events, and each delegated child's invoke_agent under the execute_tool task that started it, whichever process ran it. An event without a placement becomes a span of its own, as before. The agent's spans carry no message content (Stage 5 rule 10).

Model calls follow the OpenTelemetry GenAI semantic conventions, version OpenTelemetryTracing.SemconvVersion (#2275): a completion - plain or streamed - is a CLIENT span chat <model>, an embedding call one named embeddings <model>, with gen_ai.operation.name, gen_ai.provider.name, the request and response model, the response id and finish reasons, token usage, the request parameters and server.address; a failed call is that span with error.type and an ERROR status. Agent spans carry gen_ai.provider.name too.

Message content - prompts, completions, an agent's query and tool input and output - is recorded only with captureContent (llm4s.tracing.opentelemetry.captureContent, off by default), as the conventions' gen_ai.input.messages / gen_ai.output.messages, with secrets redacted first.

Attributes

Companion
object
Source
OpenTelemetryTracing.scala
Graph
Supertypes
trait Tracing
class Object
trait Matchable
class Any

Members list

Value members

Constructors

def this(serviceName: String, endpoint: String, headers: Map[String, String], captureContent: Boolean)

Attributes

Source
OpenTelemetryTracing.scala

Concrete methods

override def close(): Unit

Releases what the tracer holds - an exporter's threads and connections - flushing what it has buffered. Does nothing unless a backend overrides it; a tracer that wraps others closes them. Call it once, when the application is done tracing.

Releases what the tracer holds - an exporter's threads and connections - flushing what it has buffered. Does nothing unless a backend overrides it; a tracer that wraps others closes them. Call it once, when the application is done tracing.

Attributes

Definition Classes
Source
OpenTelemetryTracing.scala
override def traceEvent(event: TraceEvent): Result[Unit]

Handles a structured trace event using this tracing backend: the one method an implementation writes. Implementations should report tracing failures through the returned result rather than throwing exceptions.

Handles a structured trace event using this tracing backend: the one method an implementation writes. Implementations should report tracing failures through the returned result rather than throwing exceptions.

Value parameters

event

the structured event to trace

Attributes

Returns

Right(()) when the tracing call is handled successfully, including when a no-op backend discards the event, or Left(error) if tracing fails. Callers may ignore or log tracing failures.

Definition Classes
Source
OpenTelemetryTracing.scala

Inherited methods

final def traceCompletion(completion: Completion, model: ModelName): Result[Unit]

Records a completion received from a model: a TraceEvent.CompletionReceived - the completion's id, model, the number of tool calls in its message, the message content and the served tier - passed to traceEvent.

Records a completion received from a model: a TraceEvent.CompletionReceived - the completion's id, model, the number of tool calls in its message, the message content and the served tier - passed to traceEvent.

Value parameters

completion

the model response whose details are to be traced

model

the model identifier to associate with this completion

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceCost(costUsd: Double, model: ModelName, operation: String, tokenCount: Int, costType: String): Result[Unit]

Trace cost in USD for any operation.

Trace cost in USD for any operation.

Value parameters

costType

Category: "embedding", "completion", "total"

costUsd

Cost in US dollars

model

Model name

operation

Type: "embedding", "completion", "evaluation"

tokenCount

Total tokens used

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceEmbeddingUsage(usage: EmbeddingUsage, model: ModelName, operation: String, inputCount: Int): Result[Unit]

Trace embedding token usage for cost tracking.

Trace embedding token usage for cost tracking.

Value parameters

inputCount

Number of texts embedded

model

Embedding model name

operation

Type: "indexing", "query", "evaluation"

usage

Token usage from embedding operation

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceError(error: LLMError, context: String): Result[Unit]

Records an LLMError - the Left of a Result, which is not an exception - with where it occurred: a TraceEvent.ErrorOccurred with its message and type, passed to traceEvent.

Records an LLMError - the Left of a Result, which is not an exception - with where it occurred: a TraceEvent.ErrorOccurred with its message and type, passed to traceEvent.

Value parameters

context

the operation in which the error occurred, such as "fetching weather data"

error

the error to record

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceError(error: Throwable, context: String): Result[Unit]

Records an error with where it occurred: a TraceEvent.ErrorOccurred passed to traceEvent.

Records an error with where it occurred: a TraceEvent.ErrorOccurred passed to traceEvent.

Value parameters

context

the operation in which the error occurred, such as "fetching weather data"

error

the error to record

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceEvent(event: String): Result[Unit]

Emits a named CustomEvent with no additional data payload.

Emits a named CustomEvent with no additional data payload.

Wraps the string in a TraceEvent.CustomEvent with an empty JSON object as its data field before delegating to traceEvent(TraceEvent). Use the typed overload for events that carry structured data.

Value parameters

event

Human-readable event name forwarded as CustomEvent.name

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceRAGOperation(operation: String, duration: FiniteDuration, embeddingTokens: Option[Int], llmPromptTokens: Option[Int], llmCompletionTokens: Option[Int], totalCostUsd: Option[Double]): Result[Unit]

Trace completion of a RAG operation with metrics.

Trace completion of a RAG operation with metrics.

Value parameters

duration

Wall-clock duration

embeddingTokens

Optional embedding token count

llmCompletionTokens

Optional LLM completion tokens

llmPromptTokens

Optional LLM prompt tokens

operation

Type: "index", "search", "answer", "evaluate"

totalCostUsd

Optional total cost in USD

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceTokenUsage(usage: TokenUsage, model: ModelName, operation: String): Result[Unit]

Records token usage for a model operation: a TraceEvent.TokenUsageRecorded passed to traceEvent.

Records token usage for a model operation: a TraceEvent.TokenUsageRecorded passed to traceEvent.

Value parameters

model

the model identifier to associate with this usage

operation

a caller-defined label describing the operation, such as "completion"

usage

the token usage reported for the operation

Attributes

Inherited from:
Tracing
Source
Tracing.scala
final def traceToolCall(toolName: String, input: String, output: String, duration: FiniteDuration, success: Boolean): Result[Unit]

Records a tool call: a TraceEvent.ToolExecuted passed to traceEvent.

Records a tool call: a TraceEvent.ToolExecuted passed to traceEvent.

Value parameters

duration

how long the call took

input

the input supplied to the tool

output

the output the tool returned, or its error

success

whether the call succeeded

toolName

the name of the tool that was called

Attributes

Inherited from:
Tracing
Source
Tracing.scala