Class OpenTelemetryMetrics

java.lang.Object
com.tnsai.quality.observability.OpenTelemetryMetrics

public class OpenTelemetryMetrics extends Object
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 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

      public void increment(String name)
      Increments a counter by 1.
      Parameters:
      name - the counter name
    • increment

      public void increment(String name, long delta)
      Increments a counter by a specified amount.
      Parameters:
      name - the counter name
      delta - the amount to add
    • incrementWithAttributes

      public void incrementWithAttributes(String name, Map<String,String> attributes)
      Increments a counter with attributes.
      Parameters:
      name - the counter name
      attributes - the attributes to attach
    • incrementWithAttributes

      public void incrementWithAttributes(String name, long delta, Map<String,String> attributes)
      Increments a counter with attributes.
      Parameters:
      name - the counter name
      delta - the amount to add
      attributes - the attributes to attach
    • gauge

      public void gauge(String name, long value)
      Sets a gauge value.
      Parameters:
      name - the gauge name
      value - the value
    • record

      public void record(String name, double value)
      Records a value in a histogram.
      Parameters:
      name - the histogram name
      value - the value to record
    • recordWithAttributes

      public void recordWithAttributes(String name, double value, Map<String,String> attributes)
      Records a value in a histogram with attributes.
      Parameters:
      name - the histogram name
      value - the value to record
      attributes - the attributes to attach
    • recordActionSuccess

      public void recordActionSuccess(String actionType, double durationMs)
      Records a successful action execution.
      Parameters:
      actionType - the type of action
      durationMs - the duration in milliseconds
    • recordActionFailure

      public void recordActionFailure(String actionType, double durationMs, String errorType)
      Records a failed action execution.
      Parameters:
      actionType - the type of action
      durationMs - the duration in milliseconds
      errorType - 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 name
      promptTokens - number of prompt tokens
      completionTokens - number of completion tokens
      latencyMs - the latency in milliseconds
    • recordLlmCallFailure

      public void recordLlmCallFailure(String provider, String model, double latencyMs, String errorType)
      Records a failed LLM API call.
      Parameters:
      provider - the LLM provider
      model - the model name
      latencyMs - the latency in milliseconds
      errorType - the error type
    • recordMessageSent

      public void recordMessageSent(String agentId, String messageType)
      Records a message sent.
      Parameters:
      agentId - the sender agent ID
      messageType - the message type
    • recordMessageReceived

      public void recordMessageReceived(String agentId, String messageType)
      Records a message received.
      Parameters:
      agentId - the receiver agent ID
      messageType - 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.