TExcelTAD
Home › Observability: Prometheus, Grafana, Loki & OpenTelemetry › The Exposition Format & Naming Rules

The Exposition Format & Naming Rules

What a target returns from /metrics is a small text format. You will read it often (with curl), so it's worth knowing line by line, together with the naming rules that make metrics from different teams fit together.

The text format

text
# HELP http_requests_total HTTP requests handled, by route and status code.
# TYPE http_requests_total counter
http_requests_total{route="/{code}",code="302"} 51203
http_requests_total{route="/links",code="201"} 1288
# HELP linkly_links_stored Short links in the database.
# TYPE linkly_links_stored gauge
linkly_links_stored 1342
  • # HELP name text: a one-line description, shown in the UI and Grafana.
  • # TYPE name counter|gauge|histogram|summary|untyped.
  • One sample per line: name{label="value",…} value [timestamp]. Timestamps are almost never sent; Prometheus uses the scrape time.
  • Label values are quoted strings; \", \\ and \n are escaped.

OpenMetrics is the standardised successor (content type application/openmetrics-text): nearly the same, but counters are declared without _total in # TYPE and must have it on samples, there is # UNIT, exemplars (# {trace_id="…"} 0.12) can be attached to samples, and the body ends with # EOF. Client libraries negotiate the format with Prometheus; you mostly see the classic text format when you curl.

Prometheus 3 also accepts UTF-8 metric and label names ({"http.server.duration"}), which matters when OpenTelemetry metrics are ingested without translation. The classic charset [a-zA-Z_:][a-zA-Z0-9_:]* is still what you should write by hand.

Naming rules

Rule Good Bad
a prefix for the domain (app or library) linkly_links_created_total links_created
base units, spelled out, as a suffix _seconds, _bytes, _ratio (0–1) _ms, _kb, _percent
counters end in _total http_requests_total http_requests
one name = one meaning; vary with labels http_requests_total{code="500"} http_500_errors_total
a name you could sum or average across all its labels node_filesystem_avail_bytes{mountpoint=…} mixing bytes and percent under one name
_info for metadata, value 1 linkly_build_info{version="1.4.2",goversion="go1.27.3"} 1 encoding the version in the name

Base units matter because PromQL and Grafana don't know about units: if one service exports milliseconds and another seconds, every dashboard that compares them is wrong by a factor of 1000. Grafana formats _seconds and _bytes nicely when you set the panel unit.

promtool check metrics lints exposition text for exactly these mistakes. Pipe a scrape into it (here, a file with three mistakes):

bash · try it yourself
cat > metrics.txt <<'TXT'
# HELP linkly_redirects_total Redirects served.
# TYPE linkly_redirects_total counter
linkly_redirects_total{code="302"} 5120
# HELP linkly_request_latency_ms Request latency.
# TYPE linkly_request_latency_ms histogram
linkly_request_latency_ms_bucket{le="100"} 90
linkly_request_latency_ms_bucket{le="+Inf"} 100
linkly_request_latency_ms_sum 4210
linkly_request_latency_ms_count 100
# TYPE linkly_queue_items counter
linkly_queue_items 5
TXT
promtool check metrics < metrics.txt
echo "exit code: $?"
linkly_queue_items counter metrics should have "_total" suffix
linkly_queue_items no help text
linkly_request_latency_ms metric names should not contain abbreviated units
exit code: 3

Fixed version: linkly_request_duration_seconds (with buckets in seconds: le="0.1"), and linkly_queue_items either becomes a gauge (if it's "items in the queue now") or linkly_queue_items_enqueued_total (if it counts items ever added), plus a # HELP line. Run the check in CI against a scrape of your service's test instance, and new metrics get reviewed automatically.

bash · try it yourself
cat > metrics.txt <<'TXT'
# HELP linkly_redirects_total Redirects served.
# TYPE linkly_redirects_total counter
linkly_redirects_total{code="302"} 5120
# HELP linkly_request_duration_seconds Request latency.
# TYPE linkly_request_duration_seconds histogram
linkly_request_duration_seconds_bucket{le="0.1"} 90
linkly_request_duration_seconds_bucket{le="+Inf"} 100
linkly_request_duration_seconds_sum 4.21
linkly_request_duration_seconds_count 100
# HELP linkly_queue_items Items waiting in the queue.
# TYPE linkly_queue_items gauge
linkly_queue_items 5
TXT
promtool check metrics < metrics.txt && echo "clean"
clean

Labels: what they are for

Labels are the dimensions you'll slice by: route, method, code, instance, env. Good label values are a small, bounded set. Bad ones are unbounded: user IDs, short codes, full URLs, email addresses, request IDs, error messages. Each distinct combination is a separate series stored and indexed separately, so http_requests_total{path="/abc123"} for every short code would create a series per link. The next lesson quantifies that.

Two habits for routes:

  • use the route template (/{code}), never the raw path (/abc123);
  • map status codes to the exact code (302) or to classes (3xx) consistently across services, and stick to it.

Test yourself

Which metric name follows the conventions?

  1. linkly_latency_ms
  2. linkly_request_duration_seconds
  3. linklyRequestDuration
  4. linkly_request_duration_seconds_total as a histogram

How should a service expose its version?

  1. In the metric name: linkly_v1_4_2_up
  2. As an _info gauge with value 1 and the version as a label
  3. As the metric value
  4. In # HELP

Why must path="/abc123" (the raw request path) not be a label for linkly?

  1. Slashes aren't allowed
  2. Every short code would create its own series: unbounded cardinality
  3. Labels must be numbers
  4. Prometheus ignores it