Kubernetes Blog
Follow
Building a Custom Metrics Exporter for Kubernetes
Kubernetes scales based on CPU and memory, but real-world demands often require custom metrics like queue depth or active connections. A Prometheus metrics exporter bridges this gap by exposing application state as plain text on an HTTP /metrics endpoint for Prometheus to scrape.Exporters can be standalone services or embedded directly within an application, depending on the data source and control over application code. Prometheus expects metrics in specific formats: counters for ever-increasing values, gauges for fluctuating values, and histograms for distributions. Metric names should follow a clear " __ " snake_case convention.
Building an exporter in Go involves initializing a module, setting up the Prometheus client library, and registering metrics like counters, gauges, and histograms. These metrics are then updated by a polling loop that reads from your application's data sources. An HTTP server exposes the /metrics endpoint, along with a /healthz endpoint for liveness probes.
The exporter is packaged into a minimal container image using a multi-stage Docker build, ensuring a small footprint. Deployment to a Kubernetes cluster involves a Deployment for pod management and a Service to provide a stable address for Prometheus.
Prometheus discovers these metrics via a ServiceMonitor if using the Prometheus Operator, or through annotation-based discovery on the Pod template. Verification involves checking the Prometheus targets page to ensure the exporter is "UP" and querying the metrics in the expression browser.
This foundation allows for autoscaling with the HorizontalPodAutoscaler, which uses a metrics adapter (like the Prometheus Adapter) to integrate custom metrics with the Kubernetes Custom Metrics API. This enables workloads to scale based on business-relevant signals, not just basic resource utilization.