Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Prometheus Metrics

Export agent metrics in Prometheus text exposition format. The synaptic-metrics crate wraps MetricsCallback and serves a /metrics HTTP endpoint for scraping.

Setup

[dependencies]
synaptic = { version = "0.4", features = ["metrics", "callbacks"] }

Quick Start

use std::sync::Arc;
use synaptic::callbacks::MetricsCallback;
use synaptic::metrics::PrometheusExporter;

// Create a MetricsCallback (attach to your agent/model)
let metrics = Arc::new(MetricsCallback::new());

// Create exporter and serve
let exporter = PrometheusExporter::new(metrics.clone());
let handle = exporter.serve("0.0.0.0:9090").await?;

println!("Prometheus metrics at http://{}/metrics", handle.addr());

// ... run your agent ...

// Stop the server when done
handle.stop().await;

Rendered Metrics

The exporter renders these metrics (prefix defaults to synaptic):

MetricTypeLabelsDescription
synaptic_model_calls_totalcounter--Total LLM API calls
synaptic_model_latency_secondsgauge--Average model call latency
synaptic_model_errors_totalcounter--Total model call errors
synaptic_tokens_input_totalcounter--Total input tokens consumed
synaptic_tokens_output_totalcounter--Total output tokens consumed
synaptic_tool_calls_totalcountertoolPer-tool call count
synaptic_tool_latency_secondsgaugetoolPer-tool average latency
synaptic_tool_errors_totalcountertoolPer-tool error count

Custom Prefix

let exporter = PrometheusExporter::new(metrics.clone())
    .with_prefix("myapp");
// Metrics will be named: myapp_model_calls_total, etc.

Rendering Without a Server

If you prefer to integrate with an existing HTTP framework, call render() directly:

let exporter = PrometheusExporter::new(metrics.clone());
let text = exporter.render().await;
// text is Prometheus text exposition format, e.g.:
// # HELP synaptic_model_calls_total Total model calls
// # TYPE synaptic_model_calls_total counter
// synaptic_model_calls_total 42

Prometheus scrape_config Example

scrape_configs:
  - job_name: 'synaptic-agent'
    static_configs:
      - targets: ['localhost:9090']
    scrape_interval: 15s