Skip to main content
Previously, examples assumed metrics already existed: Prometheus scrapes them, Grafana visualizes them, and queries use PromQL. But where do those metrics originate? Often exporters provide many system-level metrics: node_exporter for host metrics, kube-state-metrics for Kubernetes object state, and application exporters for app-specific metrics. This article (and the next) demystifies how to go from zero metrics to instrumenting an application so it exposes multiple metrics directly to Prometheus. When you instrument an application you do two things:
  1. Compute metrics in your application and keep the current value for each time series in memory.
  2. Expose an HTTP metrics endpoint that Prometheus scrapes to read those values.
A diagram titled "Demystifying the Metric Instrumentation Process" showing the Prometheus monitoring stack. It depicts client libraries and exporters feeding metrics into a Prometheus server (with local storage and service discovery), Grafana for visualization, and Alertmanager sending notifications to services like OpsGenie and PagerDuty.
Example of the Prometheus exposition (text) format returned by a metrics endpoint:
For security, serve metric endpoints on a separate port or behind internal network controls so they are not publicly exposed. Prometheus client libraries that both calculate metrics and expose them via an HTTP endpoint are available for most major languages: Python, Go, Java, Node.js, Ruby, and more. The instrumentation pattern is consistent between languages; examples here use the Python Prometheus client library.
A slide titled "Prometheus Clients for Multiple Languages" showing the logos and labels for Python, Go, Java, Node.js, and Ruby.
General pattern
  1. Define your metric at module scope (outside request handlers or event loops). Provide a stable metric name, a help/description string, and optional label keys. For histograms you can predefine bucket boundaries.
  2. Update the metric from application logic when relevant events occur (requests, errors, queue lengths, business events).
  3. Expose a metrics HTTP endpoint so Prometheus can scrape the current values.
Example metric declarations (module scope) in Python:
Update metrics inside application logic when events occur:
Metric types at a glance Conceptually, counters, gauges, and histograms differ; in code, the difference is which methods you call. Client libraries manage the time series and format the endpoint output into the OpenMetrics/Prometheus exposition format, but they do not prevent cardinality explosions caused by high-cardinality labels — design labels carefully.
Label cardinality is a common pitfall. Avoid using unbounded values (e.g., user IDs, UUIDs, or timestamps) as label values. Prefer aggregations or buckets for high-cardinality attributes to keep the number of time series manageable.
Many client libraries provide decorators or context managers to simplify timing or counting around a block of code; for example, Python’s Histogram.time() can measure function or request latency automatically. Security and deployment tips
  • Expose metrics on a separate port or an internal-only endpoint to prevent public access.
  • Scrape metrics with appropriate authentication or network policies in production.
  • Monitor the cardinality and churn of time series — high cardinality increases memory use and query cost.
If you accidentally use high-cardinality labels (like user IDs or request UUIDs), Prometheus storage and query performance can degrade rapidly. Audit label usage before deploying instrumentation to production.
Why this matters to DevOps and SRE This lesson is intended to give DevOps and SRE practitioners enough context to collaborate effectively with developers about what metrics to expose and where to look when something important isn’t being tracked. Instrumentation decisions affect alerting, dashboards, and query performance. OpenTelemetry and auto-instrumentation OpenTelemetry can auto-instrument common frameworks to provide metrics such as HTTP request counts and latencies without writing manual instrumentation for every handler. Compare manual instrumentation and auto-instrumentation in a running lab to see which metrics each approach provides and to determine which metrics you can rely on versus which require explicit code changes. Further reading and references

Watch Video