Lesson 4 of 9 · 22m
Exporters and Instrumentation: Metrics as an API Contract
When to instrument in-process, when to use an exporter, and how to design metric surfaces that operators can depend on.
An exporter is not a pile of numbers; it is an operational API. Every metric name, label, and unit you expose becomes a dependency for alerts, dashboards, incident queries, and capacity models. Treat it with the same care as a public HTTP endpoint, because breaking it during a deploy is an observability outage.
Instrument in the process when you can
The best exporter is usually the application itself using a client library:
from prometheus_client import Counter, Histogram, start_http_server
REQUESTS = Counter(
"checkout_http_requests_total",
"HTTP requests handled by checkout.",
["method", "route", "status"],
)
LATENCY = Histogram(
"checkout_http_request_duration_seconds",
"HTTP request latency in seconds.",
["route"],
buckets=(0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5),
)
start_http_server(9100)Direct instrumentation gives you business-context metrics: checkout attempts, payment declines, queue lag, cache hit ratio. System exporters cannot infer these. The endpoint should normally be /metrics, bind predictably, and be cheap enough that a 15-second scrape is not a denial-of-service invitation.
Use external exporters for things you do not control
Node exporter, blackbox exporter, postgres exporter, mysqld exporter, SNMP exporter — they translate systems into Prometheus format. Each one has a different trust boundary:
node_exporterobserves the machine and should run close to it.blackbox_exporterprobes externally and measures what users experience.- Database exporters often need credentials; scope them to read-only monitoring grants.
- SNMP exporters can generate enormous cardinality from device interfaces; filter aggressively.
Do not contort an application to emit operating-system metrics that node exporter already provides. Conversely, do not scrape application internals through logs if the application can expose the real counter cheaply.
Metric design rules that age well
checkout_payment_attempts_total{processor="stripe", result="declined"}
checkout_queue_oldest_message_age_seconds
checkout_build_info{version="2026.11.30", commit="4e8b3f2"} 1Use plural nouns for counts, _total for counters, base units for quantities, and _info gauges for build metadata. Keep label values controlled. result="declined" is fine; decline_reason="card_zip_mismatch_issuer_484" is not.
The metrics endpoint is a cost center
Every scrape serializes every exposed series. I have seen services spend more CPU formatting metrics than serving requests because someone exposed per-customer counters. A metrics endpoint is not free just because it is plaintext.
If the payload is large, measure it:
curl -s http://127.0.0.1:9100/metrics | wc -l
curl -s http://127.0.0.1:9100/metrics | gzip -c | wc -cThousands of lines are normal. Millions are an incident in waiting.
Version metrics like APIs
Changing a label or renaming a metric silently breaks alerts and dashboards. When you must change them, ship overlap: expose old and new names for a deprecation window, update rules, then remove the old series.
Field notes
- A metric without documentation becomes folklore. Use the HELP text; it is the contract written at the point of truth.
- Do not export secrets or personally identifying data in labels. Metrics systems are widely readable and long retained.
- Cache expensive metric collection inside the exporter if a scrape would otherwise query a slow subsystem every 15 seconds.
- Emit zero for meaningful sparse states when possible; missing series make dashboards and alerts ambiguous.
Was this lesson helpful?