Class PrometheusMetricsExporter

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

public class PrometheusMetricsExporter extends Object
Prometheus metrics exporter for TnsAI.

Exposes TnsAI metrics in Prometheus format via an HTTP endpoint. Integrates with the existing Metrics class to export all recorded metrics.

Usage


 // Create and start the exporter on port 9090
 PrometheusMetricsExporter exporter = PrometheusMetricsExporter.builder()
     .port(9090)
     .build();
 exporter.start();

 // Metrics will be available at http://localhost:9090/metrics

 // Record metrics using the normal Metrics class
 Metrics.getInstance().increment("agent.actions.total");

 // Or use Prometheus-native metrics
 exporter.incrementCounter("my_counter", "label_value");

 // Shutdown when done
 exporter.stop();
 

Prometheus Configuration


 # prometheus.yml
 scrape_configs:
   - job_name: 'tnsai'
     static_configs:
       - targets: ['localhost:9090']
 

Environment Variables

  • TNSAI_PROMETHEUS_PORT - HTTP server port (default: 9090)
  • TNSAI_PROMETHEUS_ENABLED - Enable/disable exporter (default: true)
See Also:
  • Method Details

    • start

      public void start() throws IOException
      Starts the HTTP server for metrics exposition.
      Throws:
      IOException - if the server cannot be started
    • stop

      public void stop()
      Stops the HTTP server.
    • isRunning

      public boolean isRunning()
      Checks if the exporter is running.
      Returns:
      true if running
    • getPort

      public int getPort()
      Gets the port the server is running on.
      Returns:
      the port number
    • recordActionSuccess

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

      public void recordActionFailure(String actionType, String errorType, double durationMs)
      Records a failed action.
      Parameters:
      actionType - the action type
      errorType - the error type
      durationMs - duration in milliseconds
    • recordLlmCall

      public void recordLlmCall(String provider, String model, long promptTokens, long completionTokens, double latencyMs)
      Records an LLM API call.
      Parameters:
      provider - the LLM provider
      model - the model name
      promptTokens - prompt tokens used
      completionTokens - completion tokens used
      latencyMs - latency in milliseconds
    • recordLlmCallFailure

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

      public void setActiveAgents(long count)
      Sets the number of active agents.
      Parameters:
      count - the count
    • getOrCreateCounter

      public io.prometheus.metrics.core.metrics.Counter getOrCreateCounter(String name, String help, String... labelNames)
      Gets or creates a counter with the given name.
      Parameters:
      name - the metric name
      help - the help text
      labelNames - label names
      Returns:
      the counter
    • getOrCreateGauge

      public io.prometheus.metrics.core.metrics.Gauge getOrCreateGauge(String name, String help, String... labelNames)
      Gets or creates a gauge with the given name.
      Parameters:
      name - the metric name
      help - the help text
      labelNames - label names
      Returns:
      the gauge
    • getOrCreateHistogram

      public io.prometheus.metrics.core.metrics.Histogram getOrCreateHistogram(String name, String help, String... labelNames)
      Gets or creates a histogram with the given name.
      Parameters:
      name - the metric name
      help - the help text
      labelNames - label names
      Returns:
      the histogram
    • builder

      public static PrometheusMetricsExporter.Builder builder()
      Creates a new builder.
      Returns:
      a new builder
    • createAndStart

      public static PrometheusMetricsExporter createAndStart() throws IOException
      Creates and starts a default exporter on the configured port.
      Returns:
      the started exporter
      Throws:
      IOException - if startup fails