Package com.tnsai.quality.observability
Class OpenTelemetryMetrics
java.lang.Object
com.tnsai.quality.observability.OpenTelemetryMetrics
Bridge between TnsAI's Metrics and OpenTelemetry metrics.
This class provides OpenTelemetry-based metrics that follow the same
patterns as the existing Metrics class while exporting to
OpenTelemetry backends like Prometheus, Jaeger, or OTLP collectors.
Usage Example
TnsAITelemetry telemetry = TnsAITelemetry.builder()
.serviceName("my-agent")
.build();
OpenTelemetryMetrics metrics = new OpenTelemetryMetrics(telemetry.getMeter());
// Counter
metrics.increment("agent.actions.total");
metrics.incrementWithAttributes("agent.actions.total",
Map.of("action.type", "web_search"));
// Gauge
metrics.gauge("agent.active", 5);
// Histogram (duration/latency)
metrics.record("llm.latency_ms", 150.0);
LLM-Specific Metrics
// Record LLM call metrics
metrics.recordLlmCall("openai", "gpt-4", 500, 150, 1200);
-
Constructor Summary
ConstructorsConstructorDescriptionOpenTelemetryMetrics(io.opentelemetry.api.metrics.Meter meter) Creates a new OpenTelemetryMetrics with the given Meter. -
Method Summary
Modifier and TypeMethodDescriptionvoidSets a gauge value.io.opentelemetry.api.metrics.MetergetMeter()Gets the underlying OpenTelemetry Meter.voidIncrements a counter by 1.voidIncrements a counter by a specified amount.voidincrementWithAttributes(String name, long delta, Map<String, String> attributes) Increments a counter with attributes.voidincrementWithAttributes(String name, Map<String, String> attributes) Increments a counter with attributes.voidRecords a value in a histogram.voidrecordActionFailure(String actionType, double durationMs, String errorType) Records a failed action execution.voidrecordActionSuccess(String actionType, double durationMs) Records a successful action execution.voidrecordLlmCall(String provider, String model, long promptTokens, long completionTokens, double latencyMs) Records an LLM API call with all relevant metrics.voidrecordLlmCallFailure(String provider, String model, double latencyMs, String errorType) Records a failed LLM API call.voidrecordMessageReceived(String agentId, String messageType) Records a message received.voidrecordMessageSent(String agentId, String messageType) Records a message sent.voidrecordWithAttributes(String name, double value, Map<String, String> attributes) Records a value in a histogram with attributes.voidsetActiveAgents(long count) Updates the active agents gauge.
-
Constructor Details
-
OpenTelemetryMetrics
public OpenTelemetryMetrics(io.opentelemetry.api.metrics.Meter meter) Creates a new OpenTelemetryMetrics with the given Meter.- Parameters:
meter- the OpenTelemetry Meter instance
-
-
Method Details
-
increment
Increments a counter by 1.- Parameters:
name- the counter name
-
increment
Increments a counter by a specified amount.- Parameters:
name- the counter namedelta- the amount to add
-
incrementWithAttributes
Increments a counter with attributes.- Parameters:
name- the counter nameattributes- the attributes to attach
-
incrementWithAttributes
Increments a counter with attributes.- Parameters:
name- the counter namedelta- the amount to addattributes- the attributes to attach
-
gauge
Sets a gauge value.- Parameters:
name- the gauge namevalue- the value
-
record
Records a value in a histogram.- Parameters:
name- the histogram namevalue- the value to record
-
recordWithAttributes
Records a value in a histogram with attributes.- Parameters:
name- the histogram namevalue- the value to recordattributes- the attributes to attach
-
recordActionSuccess
Records a successful action execution.- Parameters:
actionType- the type of actiondurationMs- the duration in milliseconds
-
recordActionFailure
Records a failed action execution.- Parameters:
actionType- the type of actiondurationMs- the duration in millisecondserrorType- the error type
-
recordLlmCall
public void recordLlmCall(String provider, String model, long promptTokens, long completionTokens, double latencyMs) Records an LLM API call with all relevant metrics.- Parameters:
provider- the LLM provider (e.g., "openai", "anthropic")model- the model namepromptTokens- number of prompt tokenscompletionTokens- number of completion tokenslatencyMs- the latency in milliseconds
-
recordLlmCallFailure
Records a failed LLM API call.- Parameters:
provider- the LLM providermodel- the model namelatencyMs- the latency in millisecondserrorType- the error type
-
recordMessageSent
Records a message sent.- Parameters:
agentId- the sender agent IDmessageType- the message type
-
recordMessageReceived
Records a message received.- Parameters:
agentId- the receiver agent IDmessageType- the message type
-
setActiveAgents
public void setActiveAgents(long count) Updates the active agents gauge.- Parameters:
count- the number of active agents
-
getMeter
public io.opentelemetry.api.metrics.Meter getMeter()Gets the underlying OpenTelemetry Meter.
-