Package com.tnsai.quality.observability
Class AgentInstrumentation
java.lang.Object
com.tnsai.quality.observability.AgentInstrumentation
OpenTelemetry instrumentation for TnsAI agents.
This class provides tracing and metrics instrumentation for agent operations including chat, action execution, and tool calls. It uses a wrapper pattern to add observability without modifying the Agent class directly.
Usage Example
// Initialize telemetry
TnsAITelemetry telemetry = TnsAITelemetry.builder()
.serviceName("my-agent-service")
.build();
// Create instrumentation
AgentInstrumentation instrumentation = new AgentInstrumentation(telemetry);
// Instrument agent operations
Agent agent = new MyAgent(principal, scope); // passes a wired ActionExecutor, see com.tnsai.agents
instrumentation.registerAgent(agent);
// Operations are automatically traced
String response = instrumentation.traceChat(agent, "Hello", () -> agent.chat("Hello"));
Traced Operations
agent.chat- Chat operations with user messagesagent.action- Action executionagent.tool_call- Individual tool callsagent.llm_call- LLM API calls
-
Constructor Summary
ConstructorsConstructorDescriptionAgentInstrumentation(OpenTelemetryTracer tracer, OpenTelemetryMetrics metrics) Creates a new AgentInstrumentation with custom tracer and metrics.AgentInstrumentation(TnsAITelemetry telemetry) Creates a new AgentInstrumentation with the given TnsAITelemetry. -
Method Summary
Modifier and TypeMethodDescriptionGets the underlying OpenTelemetryMetrics.Gets the underlying OpenTelemetryTracer.voidrecordLlmFailure(String provider, String model, double latencyMs, String errorType) Records a failed LLM call.voidrecordLlmTokens(String provider, String model, long promptTokens, long completionTokens, double latencyMs) Records LLM token usage.voidregisterAgent(com.tnsai.agents.Agent agent) Registers an agent for instrumentation and increments active agent count.<T> TtraceAction(com.tnsai.agents.Agent agent, String actionName, Map<String, Object> parameters, Supplier<T> actionOperation) Traces an action execution.<T> TTraces a chat operation.<T> TtraceLlmCall(com.tnsai.agents.Agent agent, String provider, String model, Supplier<T> llmOperation) Traces an LLM call with semantic conventions.<T> TtraceToolCall(com.tnsai.agents.Agent agent, String toolName, Map<String, Object> arguments, Supplier<T> toolOperation) Traces a tool call.voidunregisterAgent(com.tnsai.agents.Agent agent) Unregisters an agent and decrements active agent count.
-
Constructor Details
-
AgentInstrumentation
Creates a new AgentInstrumentation with the given TnsAITelemetry.- Parameters:
telemetry- the TnsAITelemetry instance
-
AgentInstrumentation
Creates a new AgentInstrumentation with custom tracer and metrics.- Parameters:
tracer- the OpenTelemetryTracer instancemetrics- the OpenTelemetryMetrics instance
-
-
Method Details
-
registerAgent
public void registerAgent(com.tnsai.agents.Agent agent) Registers an agent for instrumentation and increments active agent count.- Parameters:
agent- the agent to register
-
unregisterAgent
public void unregisterAgent(com.tnsai.agents.Agent agent) Unregisters an agent and decrements active agent count.- Parameters:
agent- the agent to unregister
-
traceChat
Traces a chat operation.- Type Parameters:
T- the return type- Parameters:
agent- the agent performing the chatmessage- the user messagechatOperation- the chat operation to execute- Returns:
- the result of the chat operation
-
traceAction
public <T> T traceAction(com.tnsai.agents.Agent agent, String actionName, Map<String, Object> parameters, Supplier<T> actionOperation) Traces an action execution.- Type Parameters:
T- the return type- Parameters:
agent- the agent executing the actionactionName- the name of the actionparameters- the action parametersactionOperation- the action operation to execute- Returns:
- the result of the action
-
traceToolCall
public <T> T traceToolCall(com.tnsai.agents.Agent agent, String toolName, Map<String, Object> arguments, Supplier<T> toolOperation) Traces a tool call.- Type Parameters:
T- the return type- Parameters:
agent- the agent making the tool calltoolName- the name of the toolarguments- the tool argumentstoolOperation- the tool operation to execute- Returns:
- the result of the tool call
-
traceLlmCall
public <T> T traceLlmCall(com.tnsai.agents.Agent agent, String provider, String model, Supplier<T> llmOperation) Traces an LLM call with semantic conventions.- Type Parameters:
T- the return type- Parameters:
agent- the agent making the LLM callprovider- the LLM provider namemodel- the model namellmOperation- the LLM operation to execute- Returns:
- the result of the LLM call
-
recordLlmTokens
public void recordLlmTokens(String provider, String model, long promptTokens, long completionTokens, double latencyMs) Records LLM token usage.- Parameters:
provider- the LLM providermodel- the model namepromptTokens- the number of prompt tokenscompletionTokens- the number of completion tokenslatencyMs- the latency in milliseconds
-
recordLlmFailure
Records a failed LLM call.- Parameters:
provider- the LLM providermodel- the model namelatencyMs- the latency in millisecondserrorType- the error type
-
getTracer
Gets the underlying OpenTelemetryTracer. -
getMetrics
Gets the underlying OpenTelemetryMetrics.
-