how to connect prometues with agent server
# ask-ai
p
how to connect prometues with agent server
t
Thanks for reaching out, arpan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Acknowledging your question about connecting Prometheus with an agent server — I can help you get that working. A few approaches are possible depending on whether you want built-in scraping or to export metrics from your agent code. Quick options and next steps: Options • If you can modify the agent server code: instrument the agent with OpenTelemetry (or a Prometheus client) and expose a /metrics HTTP endpoint that serves Prometheus-format metrics. Then point Prometheus to scrape that endpoint. • If you prefer using OpenTelemetry: export agent metrics via OTLP/OpenTelemetry and install an OpenTelemetry Collector configured with a Prometheus exporter so Prometheus can scrape the collector. • If you’re running services that support a
prometheus_port
(Ingress/Egress): set
prometheus_port
in their YAML to expose metrics directly. The agent server itself does not document a built-in
prometheus_port
, so custom instrumentation is typical. I can help with whichever path you choose. To give exact steps or example config, please tell me: 1. Which agent implementation/language are you using (Python, Go, Node, etc.)? 2. Are you able to change the agent server code or prefer using a collector/sidecar? 3. Are you self-hosting Prometheus and the agent, or using LiveKit Cloud Agent Insights? Which option would you like to pursue? Sources: Self-hosting the Ingress Service | LiveKit docs | Self-hosting the Egress Service | LiveKit docs | Agents (Python) | livekit/livekit_composite | DeepWiki | Observability data hooks | LiveKit docs | Agent insights in LiveKit Cloud | LiveKit docs
p
example
t
You want an example — got it. I’ll give a concise, concrete example using Go (instrument agent code with Prometheus metrics) plus a Prometheus scrape config and an alternative using an OpenTelemetry Collector. If you need a different language, tell me which. 1) Go agent — Prometheus client example • Add Prometheus client:
Copy code
import (
  "net/http"
  "github.com/prometheus/client_golang/prometheus"
  "github.com/prometheus/client_golang/prometheus/promhttp"
)
• Define and register metrics:
Copy code
var (
  agentCalls = prometheus.NewCounterVec(
    prometheus.CounterOpts{Name: "agent_calls_total", Help: "Total agent calls"},
    []string{"type"},
  )
  agentLatency = prometheus.NewHistogram(
    prometheus.HistogramOpts{Name: "agent_call_duration_seconds", Help: "Agent call duration seconds"},
  )
)

func init() {
  prometheus.MustRegister(agentCalls, agentLatency)
}
• Instrument code where agent events occur:
Copy code
func handleAgentCall(callType string, handler func()) {
  timer := prometheus.NewTimer(agentLatency)
  defer timer.ObserveDuration()
  agentCalls.WithLabelValues(callType).Inc()
  handler()
}
• Expose /metrics endpoint:
Copy code
func main() {
  http.Handle("/metrics", promhttp.Handler())
  go http.ListenAndServe(":9100", nil) // exposes metrics on :9100/metrics
  // start your agent server normally...
  select {} // or your server run loop
}
Prometheus scrape config (prometheus.yml)
Copy code
scrape_configs:
  - job_name: 'livekit_agent'
    static_configs:
      - targets: ['agent-hostname:9100']  # replace with your host:port
2) Alternative — instrument with OpenTelemetry + Collector • Export metrics from your agent using OpenTelemetry SDK (language of choice) to an OTLP endpoint. • Run an OpenTelemetry Collector with a Prometheus exporter. Example collector config:
Copy code
receivers:
  otlp:
    protocols:
      grpc:
      http:

exporters:
  prometheus:
    endpoint: "0.0.0.0:8889"

service:
  pipelines:
    metrics:
      receivers: [otlp]
      exporters: [prometheus]
• Prometheus scrape config for the collector:
Copy code
scrape_configs:
  - job_name: 'otel_collector'
    static_configs:
      - targets: ['collector-host:8889']
Which example do you want expanded? • A full Go module example and Dockerfile • Node.js or Python snippet • OpenTelemetry Collector advanced config (auth, relabeling)
p
self-hosting Prometheus alrady hosted in python
t
You're using a Python agent and self-hosted Prometheus — here’s a concise, runnable example to instrument your agent, expose /metrics, and configure Prometheus to scrape it. 1) Dependencies • pip install prometheus_client 2) Minimal Python agent instrumentation
Copy code
from prometheus_client import start_http_server, Counter, Histogram
import time
import random
import threading

# Metrics
agent_calls = Counter('agent_calls_total', 'Total agent calls', ['type'])
agent_latency = Histogram('agent_call_duration_seconds', 'Agent call duration seconds')

def handle_agent_call(call_type):
    start = time.time()
    agent_calls.labels(type=call_type).inc()
    # simulate work
    time.sleep(random.random() * 0.5)
    agent_latency.observe(time.time() - start)

# Expose /metrics on port 9100
def start_metrics_server(port=9100):
    start_http_server(port)

# Example agent loop
def agent_loop():
    types = ['llm', 'stt', 'tts', 'vad']
    while True:
        handle_agent_call(random.choice(types))
        time.sleep(1)

if __name__ == '__main__':
    start_metrics_server(9100)
    t = threading.Thread(target=agent_loop, daemon=True)
    t.start()
    # keep main thread alive (replace with your agent's run loop)
    while True:
        time.sleep(10)
3) Prometheus scrape config (prometheus.yml)
Copy code
scrape_configs:
  - job_name: 'livekit_python_agent'
    static_configs:
      - targets: ['agent-hostname:9100']  # replace with agent host or IP and port
Notes and next steps • Replace the simulated handler with your actual agent event hooks (increment
agent_calls
and observe durations where calls occur). • Add labels relevant to your environment (room_id, region) but keep cardinality low. • If you prefer OpenTelemetry, export metrics via OTLP to a Collector, then have Prometheus scrape the Collector. Want a full Dockerfile, systemd unit, or an example showing instrumenting specific LiveKit agent events?