Package com.tnsai.quality.observability
Class PrometheusMetricsExporter
java.lang.Object
com.tnsai.quality.observability.PrometheusMetricsExporter
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for PrometheusMetricsExporter. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a new builder.static PrometheusMetricsExporterCreates and starts a default exporter on the configured port.io.prometheus.metrics.core.metrics.CountergetOrCreateCounter(String name, String help, String... labelNames) Gets or creates a counter with the given name.io.prometheus.metrics.core.metrics.GaugegetOrCreateGauge(String name, String help, String... labelNames) Gets or creates a gauge with the given name.io.prometheus.metrics.core.metrics.HistogramgetOrCreateHistogram(String name, String help, String... labelNames) Gets or creates a histogram with the given name.intgetPort()Gets the port the server is running on.booleanChecks if the exporter is running.voidrecordActionFailure(String actionType, String errorType, double durationMs) Records a failed action.voidrecordActionSuccess(String actionType, double durationMs) Records a successful action.voidrecordLlmCall(String provider, String model, long promptTokens, long completionTokens, double latencyMs) Records an LLM API call.voidrecordLlmCallFailure(String provider, String model, String errorType, double latencyMs) Records a failed LLM API call.voidsetActiveAgents(long count) Sets the number of active agents.voidstart()Starts the HTTP server for metrics exposition.voidstop()Stops the HTTP server.
-
Method Details
-
start
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
Records a successful action.- Parameters:
actionType- the action typedurationMs- duration in milliseconds
-
recordActionFailure
Records a failed action.- Parameters:
actionType- the action typeerrorType- the error typedurationMs- 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 providermodel- the model namepromptTokens- prompt tokens usedcompletionTokens- completion tokens usedlatencyMs- latency in milliseconds
-
recordLlmCallFailure
Records a failed LLM API call.- Parameters:
provider- the LLM providermodel- the model nameerrorType- the error typelatencyMs- 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 namehelp- the help textlabelNames- 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 namehelp- the help textlabelNames- 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 namehelp- the help textlabelNames- label names- Returns:
- the histogram
-
builder
Creates a new builder.- Returns:
- a new builder
-
createAndStart
Creates and starts a default exporter on the configured port.- Returns:
- the started exporter
- Throws:
IOException- if startup fails
-