Package com.tnsai.quality.observability
Class LlmInstrumentation
java.lang.Object
com.tnsai.quality.observability.LlmInstrumentation
OpenTelemetry instrumentation for LLM calls following semantic conventions.
This class provides detailed tracing for LLM API calls with attributes that follow the OpenTelemetry GenAI semantic conventions for better interoperability with observability tools like Jaeger, Zipkin, and Grafana Tempo.
Semantic Conventions
This instrumentation follows the OpenTelemetry semantic conventions for Generative AI (GenAI) operations. Key attributes include:
gen_ai.system- The LLM provider (openai, anthropic, etc.)gen_ai.request.model- The model namegen_ai.request.max_tokens- Max tokens requestedgen_ai.request.temperature- Temperature settinggen_ai.usage.prompt_tokens- Prompt tokens usedgen_ai.usage.completion_tokens- Completion tokens generatedgen_ai.response.finish_reasons- Finish reason (stop, length, etc.)
Usage Example
TnsAITelemetry telemetry = TnsAITelemetry.builder()
.serviceName("my-agent")
.build();
LlmInstrumentation llmInstr = new LlmInstrumentation(telemetry.getTracer());
// Trace an LLM call
ChatResponse response = llmInstr.traceChat(
"openai",
"gpt-4",
() -> openAIClient.chat(message)
);
// Record token usage
llmInstr.recordTokenUsage(currentSpan, 150, 50);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating LLM spans with full configuration. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<Double> static final io.opentelemetry.api.common.AttributeKey<Double> static final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<String> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<Long> static final io.opentelemetry.api.common.AttributeKey<Boolean> -
Constructor Summary
ConstructorsConstructorDescriptionLlmInstrumentation(io.opentelemetry.api.trace.Tracer tracer) Creates a new LlmInstrumentation with the given Tracer. -
Method Summary
Modifier and TypeMethodDescriptionvoidrecordResponse(io.opentelemetry.api.trace.Span span, long promptTokens, long completionTokens, String finishReason) Records token usage on a span from a ChatResponse-like object.voidrecordTokenUsage(io.opentelemetry.api.trace.Span span, long promptTokens, long completionTokens) Records token usage on a span.voidrecordToolCallEvent(io.opentelemetry.api.trace.Span span, String toolName, boolean success) Adds an event for a tool call within an LLM span.voidrecordToolUse(io.opentelemetry.api.trace.Span span, int toolCallsCount) Records tool use information on a span.spanBuilder(String provider, String model) Creates a builder for configuring and starting an LLM span.<T> TtraceChat(String provider, String model, Integer maxTokens, Double temperature, Supplier<T> operation) Traces an LLM chat completion operation with request parameters.<T> TTraces an LLM chat completion operation.<T> TtraceEmbeddings(String provider, String model, int inputCount, Supplier<T> operation) Traces an LLM embeddings operation.<T> TtraceStreamChat(String provider, String model, Supplier<T> operation) Traces an LLM streaming chat operation.
-
Field Details
-
GEN_AI_SYSTEM
-
GEN_AI_REQUEST_MODEL
-
GEN_AI_REQUEST_MAX_TOKENS
-
GEN_AI_REQUEST_TEMPERATURE
-
GEN_AI_REQUEST_TOP_P
-
GEN_AI_USAGE_PROMPT_TOKENS
-
GEN_AI_USAGE_COMPLETION_TOKENS
-
GEN_AI_USAGE_TOTAL_TOKENS
-
GEN_AI_RESPONSE_FINISH_REASONS
-
GEN_AI_OPERATION_NAME
-
TNSAI_AGENT_ID
-
TNSAI_AGENT_NAME
-
TNSAI_MESSAGE_LENGTH
-
TNSAI_TOOL_USE_ENABLED
-
TNSAI_TOOL_CALLS_COUNT
-
-
Constructor Details
-
LlmInstrumentation
public LlmInstrumentation(io.opentelemetry.api.trace.Tracer tracer) Creates a new LlmInstrumentation with the given Tracer.- Parameters:
tracer- the OpenTelemetry Tracer instance
-
-
Method Details
-
traceChat
Traces an LLM chat completion operation.- Type Parameters:
T- the return type- Parameters:
provider- the LLM provider (e.g., "openai", "anthropic")model- the model nameoperation- the LLM operation to execute- Returns:
- the result of the operation
-
traceChat
public <T> T traceChat(String provider, String model, Integer maxTokens, Double temperature, Supplier<T> operation) Traces an LLM chat completion operation with request parameters.- Type Parameters:
T- the return type- Parameters:
provider- the LLM providermodel- the model namemaxTokens- maximum tokens (null if not specified)temperature- temperature setting (null if not specified)operation- the LLM operation to execute- Returns:
- the result of the operation
-
traceStreamChat
Traces an LLM streaming chat operation.- Type Parameters:
T- the return type (typically a Stream)- Parameters:
provider- the LLM providermodel- the model nameoperation- the streaming operation to execute- Returns:
- the result of the operation
-
traceEmbeddings
Traces an LLM embeddings operation.- Type Parameters:
T- the return type- Parameters:
provider- the LLM providermodel- the model nameinputCount- number of inputs to embedoperation- the embedding operation to execute- Returns:
- the result of the operation
-
recordTokenUsage
public void recordTokenUsage(io.opentelemetry.api.trace.Span span, long promptTokens, long completionTokens) Records token usage on a span.- Parameters:
span- the span to add attributes topromptTokens- number of prompt tokenscompletionTokens- number of completion tokens
-
recordResponse
public void recordResponse(io.opentelemetry.api.trace.Span span, long promptTokens, long completionTokens, String finishReason) Records token usage on a span from a ChatResponse-like object.- Parameters:
span- the span to add attributes topromptTokens- number of prompt tokenscompletionTokens- number of completion tokensfinishReason- the finish reason (e.g., "stop", "length", "tool_calls")
-
recordToolUse
public void recordToolUse(io.opentelemetry.api.trace.Span span, int toolCallsCount) Records tool use information on a span.- Parameters:
span- the span to add attributes totoolCallsCount- number of tool calls made
-
recordToolCallEvent
public void recordToolCallEvent(io.opentelemetry.api.trace.Span span, String toolName, boolean success) Adds an event for a tool call within an LLM span.- Parameters:
span- the parent spantoolName- the tool namesuccess- whether the tool call succeeded
-
spanBuilder
Creates a builder for configuring and starting an LLM span.- Parameters:
provider- the LLM providermodel- the model name- Returns:
- a new LlmSpanBuilder
-