What are Prometheus Labels?

Better Stack Team
Updated on November 29, 2024

What are Prometheus labels?

Prometheus labels are key-value pairs used to add metadata to metrics, making them more descriptive and allowing flexible queries. They enable Prometheus to organize, filter, and aggregate metrics efficiently.

Key characteristics of labels

  1. Key-value format: Each label is a key paired with a value. Example: status="200".

  2. Attached to metrics: Labels provide context or dimensions to raw metrics. For instance, the metric http_requests_total can have labels like method="GET" or endpoint="/api".

  3. Dynamic values: Labels can represent dynamic attributes, such as instance names, environments, or HTTP status codes.


Example of labels

Metric with labels:

 
http_requests_total{method="GET", endpoint="/home", status="200"} 42

Breakdown:

  • http_requests_total: The metric name.
  • {method="GET", endpoint="/home", status="200"}: Labels that provide context.
  • 42: The metric value.

Use cases for labels

  1. Filtering metrics: Labels allow querying specific subsets of metrics.

    • Query: http_requests_total{status="200"} retrieves metrics for successful requests.
  2. Aggregating metrics: Labels enable grouping or aggregating data.

    • Query: sum(rate(http_requests_total[5m])) by (status) calculates the rate of requests grouped by status.
  3. Adding context: Labels provide metadata like instance, region, or version to distinguish metrics.


Best practices for using labels

  1. Avoid high cardinality: Labels with many unique values (like user_id) can lead to a large number of time series, increasing resource usage.

  2. Use meaningful keys: Use descriptive and consistent keys such as region, environment, or status.

  3. Keep values short: Avoid embedding long strings or logs as label values.


Label pitfalls

  1. Unbounded values: Dynamic values like UUIDs or timestamps can create infinite time series, overwhelming Prometheus.

  2. Query complexity: Excessive labels can make PromQL queries difficult to construct and understand.

  3. Immutable labels: Labels cannot be modified once a time series is created. Changes require creating a new time series.


Labels are essential for making Prometheus metrics flexible and insightful. When used effectively, they enable powerful filtering, aggregation, and monitoring capabilities. However, careful design is required to avoid performance issues.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github