Stage 3 · Build
Observability & Networking
Writing Prometheus Exporters
Collector pattern, metrics types, and the promhttp package for exposing custom metrics.
Exporter Anatomy
A Prometheus exporter exposes metrics about your system in the Prometheus format. It runs an HTTP server, collects metrics on demand, and returns them when Prometheus scrapes the /metrics endpoint.
Metrics Types
Prometheus has four metric types: counter, gauge, histogram, and summary. Each serves a different purpose and has different query patterns.
| Type | Use Case | Example |
|---|---|---|
| Counter | Monotonically increasing value | requests_total, errors_total |
| Gauge | Value that can go up and down | temperature, goroutines, queue_size |
| Histogram | Distribution of values with buckets | request_duration_seconds |
| Summary | Distribution with precomputed quantiles | request_duration_seconds (client-side) |
Collector Pattern
The Collector interface lets you generate metrics on demand. Instead of pushing metrics, you compute them when Prometheus scrapes. This is ideal for metrics that are expensive to compute or depend on external state.
Labels and Dimensions
Labels add dimensions to metrics. They let you filter and aggregate by specific values. Use labels sparingly — high cardinality labels create performance problems.
promhttp Handlers
promhttp provides HTTP handlers for serving metrics. Use the default handler for basic setups, or configure it for multi-tenant or filtered metrics.
Testing Exporters
Test exporters by gathering metrics programmatically and asserting on their values. This ensures your metrics are correct without scraping an HTTP endpoint.
Always register metrics during initialization. Never create new prometheus.Desc or prometheus.Metric objects in Collect. Collect should only update existing metric values and send them to the channel.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.