Skip to content

Prometheus HTTP Listener

Core retains pure Prometheus text rendering (render_prometheus_metrics / renderPrometheusMetrics), while apcore-toolkit provides the optional HTTP listener for scraping metrics and Kubernetes health probes.

Endpoints & Behaviour

All three SDKs expose the same HTTP contract:

Path Method Behaviour
/metrics (configurable) GET Calls the host-supplied renderer callback and returns Prometheus text exposition format (text/plain; version=0.0.4; charset=utf-8).
/healthz GET Liveness probe; returns 200 OK. Reserved and cannot be overridden by the metrics path.
/readyz GET Readiness probe; calls the host-supplied readiness callback (200 OK when ready, 503 Service Unavailable until ready; defaults to not ready).
  • Routes match the URL pathname and ignore query parameters.
  • The host application controls the bind address (default 127.0.0.1:9090 where defaulted), readiness state, and graceful shutdown lifecycle.

Usage

from threading import Event, Thread

from apcore.observability import MetricsCollector, UsageCollector, render_prometheus_metrics
from apcore_toolkit import create_prometheus_server

metrics = MetricsCollector()
usage = UsageCollector()
ready = Event()

with create_prometheus_server(
    lambda: render_prometheus_metrics(metrics, usage),
    is_ready=ready.is_set,
) as server:
    thread = Thread(target=server.serve_forever)
    thread.start()
    try:
        ready.set()
        # Run the application's main loop here.
    finally:
        server.shutdown()
        thread.join()
import { once } from "node:events";
import { MetricsCollector, UsageCollector, renderPrometheusMetrics } from "apcore-js/observability";
import { createPrometheusServer } from "apcore-toolkit";

const metrics = new MetricsCollector();
const usage = new UsageCollector();
let ready = false;

const server = createPrometheusServer({
  renderMetrics: () => renderPrometheusMetrics(metrics, usage),
  isReady: () => ready,
});
const listening = once(server, "listening");
server.listen(9090, "127.0.0.1");
await listening;
ready = true;
process.once("SIGTERM", () => {
  server.close();
});

Enable the optional prometheus-server Cargo feature on apcore-toolkit:

use std::{io, net::SocketAddr};

use apcore::{render_prometheus_metrics, MetricsCollector, UsageCollector};
use apcore_toolkit::{PrometheusServer, PrometheusServerHandle};

async fn listen_for_scrapes(
    address: SocketAddr,
    metrics: MetricsCollector,
    usage: UsageCollector,
) -> io::Result<PrometheusServerHandle> {
    PrometheusServer::new(move || render_prometheus_metrics(&metrics, Some(&usage)))
        .with_readiness(|| true)
        .listen(address)
        .await
}