What are labels in Prometheus and why are they powerful?
Learn what Prometheus labels are, how metric name plus labels define a time series, and how they power PromQL filtering and aggregation, with clear examples.
Expected Interview Answer
Labels are key-value pairs attached to Prometheus metrics that add dimensions to a metric name, so a single metric like http_requests_total can be sliced by method, status, or instance. Each unique combination of metric name and labels defines a distinct time series.
Labels turn a flat metric into a multi-dimensional data model. Instead of creating separate metrics for every case, you keep one metric name and distinguish measurements with labels such as {method="GET", status="200"}. PromQL then lets you filter, aggregate, and group by these labels, so you can sum across all instances or break a total down by any dimension without redefining the metric.
- One metric name covers many dimensions
- Powerful filtering and aggregation in PromQL
- Flexible grouping without new metrics
- Cleaner, more maintainable instrumentation
- Enables per-instance, per-endpoint drill-down
AI Mentor Explanation
Think of a single batting scorecard column called runs, then adding tags for who scored, against which bowler, in which over, and on which ground. One tidy record now answers many questions — runs off spin, runs at this venue, runs by this batter — just by filtering the tags instead of keeping a separate sheet for every breakdown you might ever want.
Step-by-Step Explanation
Step 1
Pick a metric name
Choose a broad, reusable name like http_requests_total that describes the measurement, not the dimension.
Step 2
Identify dimensions
Decide which attributes matter — method, status, endpoint, instance — and express each as a label key.
Step 3
Attach label values
Instrument code so each observation carries the right label values, e.g. {method="GET", status="200"}.
Step 4
Query with PromQL
Use label matchers to filter and by/without clauses to aggregate across the dimensions you care about.
Step 5
Control cardinality
Keep label values bounded — avoid user IDs or timestamps — so the number of series stays manageable.
What Interviewer Expects
- Definition of labels as key-value pairs on metrics
- Understanding that metric name plus labels defines a unique time series
- How labels enable PromQL filtering and aggregation
- Awareness that unbounded label values cause cardinality problems
- A concrete example such as http_requests_total
Common Mistakes
- Encoding dimensions in the metric name instead of using labels
- Putting unbounded values like user IDs or emails in labels
- Confusing labels with metric types
- Not realizing each label combination is a separate time series
Best Answer (HR Friendly)
“Labels are little tags Prometheus attaches to a measurement, like marking each website request with its method and status code. This lets one metric answer many questions, because you can filter and group by those tags instead of creating a new metric for every case.”
Code Example
# Requests for GET calls that returned 500
http_requests_total{method="GET", status="500"}
# Total requests per status code across all instances
sum by (status) (rate(http_requests_total[5m]))Follow-up Questions
- What is the difference between a metric name and a label?
- How does adding a label affect the number of time series?
- What are reserved labels like job and instance?
- How do you relabel targets during scraping?
- Why should you avoid high-cardinality label values?
MCQ Practice
1. What uniquely identifies a time series in Prometheus?
A time series is defined by the metric name together with its unique combination of label key-value pairs.
2. Which of these is a poor choice for a label value?
Unbounded values like user IDs create a new series per value, causing a cardinality explosion.
3. Which PromQL clause aggregates while keeping a label?
sum by (label) aggregates the data but preserves the specified label in the result.
Flash Cards
What is a Prometheus label? — A key-value pair attached to a metric that adds a dimension for filtering and aggregation.
What defines a unique time series? — The metric name plus the full set of label key-value pairs.
Why avoid user IDs in labels? — They are unbounded and create a new time series per value, exploding cardinality.
How do you aggregate by a label? — Use PromQL like sum by (status) (...) to group results by that label.