0Pricing
System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) · 课时

指标基数与标签设计最佳实践

学习标签如何产生基数,了解高基数为何会破坏指标系统,并设计出既快速又经济的标签方案。

指标基数与标签设计最佳实践 是 CoddyKit 上的免费 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Is Cardinality?

Cardinality is the number of unique time series produced by a metric. Each distinct combination of a metric name and its label values is a separate series.

  • http_requests_total{method="GET"} is one series
  • Adding status="200" multiplies the count

Labels Multiply

Cardinality grows by the product of the number of values per label. Three methods times five status codes times ten endpoints equals 150 series for one metric.

method: 3 values
status: 5 values
endpoint: 10 values
=> 3 * 5 * 10 = 150 series

Why High Cardinality Hurts

Each series consumes memory, disk, and index space. Unbounded labels can explode a single metric into millions of series, slowing queries and crashing collectors.

  • Memory pressure on the TSDB
  • Slow dashboards and alerts
  • Higher storage cost

The Classic Anti-Pattern

Putting unbounded values like user IDs, request IDs, or full URLs into labels is the most common mistake. These create one series per unique value.

http_requests_total{user_id="48213"}
http_requests_total{request_id="a9f...c1"}

Bounded vs Unbounded Labels

Good labels have a small, finite set of values.

  • Bounded: method, status_class, region
  • Unbounded: user_id, session_id, email

Keep unbounded data in logs or traces, not metric labels.

Normalize Before Labeling

Reduce cardinality by bucketing values. Replace exact status codes with classes and template dynamic path segments.

status=503  ->  status_class="5xx"
/users/48213 -> route="/users/:id"

Estimating Series Count

Before adding a label, estimate its impact. Multiply the existing series count by the number of new label values.

existing = 200 series
new label region: 4 values
=> 200 * 4 = 800 series

Aggregation Removes Detail

When you query, aggregations like sum by (status_class) collapse series. Design labels so the dimensions you aggregate by are exactly the ones you need.

sum by (status_class) (rate(http_requests_total[5m]))

Limits and Guardrails

Most TSDBs let you cap cardinality.

  • Prometheus: sample_limit per scrape
  • OpenTelemetry: cardinality limit on aggregations
  • Alert when series counts spike

Dropping Bad Labels

If a noisy label slips in, drop or relabel it at ingestion rather than storing it.

metric_relabel_configs:
  - source_labels: [user_id]
    action: labeldrop

A Healthy Label Set

Aim for a handful of bounded labels per metric. This example stays well under a few hundred series.

orders_total{region="eu", status_class="2xx", channel="web"}

Quick Check

Identify the riskiest label choice.

Recap

You learned that cardinality is the count of unique series, that labels multiply it, and that unbounded values like IDs must stay out of labels. Bucket and template values, estimate impact before adding labels, and enforce guardrails to keep metrics fast and cheap.

常见问题解答

「指标基数与标签设计最佳实践」课时是免费的吗?

是的 — 「指标基数与标签设计最佳实践」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程的其余内容,请升级到 CoddyKit PRO。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

「指标基数与标签设计最佳实践」这节课中我会学到什么?

学习标签如何产生基数,了解高基数为何会破坏指标系统,并设计出既快速又经济的标签方案。 你通过在浏览器中直接运行的动手代码来练习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「指标基数与标签设计最佳实践」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课中编写并运行代码吗?

能。每节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 指标类型详解
  2. 指标收集策略
  3. 指标可视化与告警
  4. 指标基数与标签设计最佳实践
← 返回 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry)