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

指标类型详解

了解基本的指标类型:仪表盘指标、计数器、直方图和摘要。学习何时以及如何应用每种类型以实现有效监控。

第 1 / 4 课11 个步骤

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

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

What are Metric Types?

Welcome to "Types of Metrics Explained"! In observability, metrics are crucial for understanding your system's health and performance.

But not all numbers are the same! Categorizing metrics helps us collect, store, and analyze them effectively.

We'll explore the four fundamental types: Gauges, Counters, Histograms, and Summaries.

Gauges: Snapshot of Now

A Gauge represents a single numerical value that can go up and down over time. Think of it like a car's speedometer or a thermometer.

Gauges are perfect for capturing the current state of a system at a specific moment.

  • Use for: Current CPU usage, memory consumption, queue size, temperature.
  • Nature: Point-in-time value.

Gauge Code Example

Here's a simple Java example simulating a gauge tracking current CPU utilization. Notice how its value can change freely.

public class Main {
  public static void main(String[] args) {
    double cpuUsage = 0.5; // 50% CPU
    System.out.println("Current CPU Usage: " + cpuUsage);

    // Later, CPU usage might change
    cpuUsage = 0.8; // 80% CPU
    System.out.println("Updated CPU Usage: " + cpuUsage);

    cpuUsage = 0.3; // 30% CPU
    System.out.println("Further Updated CPU Usage: " + cpuUsage);
  }
}

Counters: Always Increasing

A Counter is a cumulative metric that only ever increases. It represents a total count of something that has occurred since the system started.

It can reset to zero only when the monitored system restarts.

  • Use for: Total requests served, errors encountered, bytes sent, login attempts.
  • Nature: Monotonically increasing total.

Counter Code Example

This Java example demonstrates a counter for total requests. Each 'request' simply increments the counter.

public class Main {
  private static long totalRequests = 0;

  public static void handleRequest() {
    totalRequests++;
    System.out.println("Requests handled: " + totalRequests);
  }

  public static void main(String[] args) {
    System.out.println("Initial requests: " + totalRequests);
    handleRequest(); // First request
    handleRequest(); // Second request
    // ... more requests later
    handleRequest(); // Third request
  }
}

Histograms: Value Distributions

Histograms sample observations (like request durations or response sizes) and count them in configurable buckets.

They give you insight into the distribution of values, not just the average. This is vital for understanding latency and performance.

  • Benefit: You can calculate percentiles (e.g., 99th percentile latency) on the server side.
  • Use for: Request latency, response sizes, data transfer rates.

Summaries: Pre-calculated Percentiles

Summaries are similar to histograms but often pre-calculate configurable quantiles (like p99, p95, p50) on the client side.

Instead of sending raw data, the client library sends pre-computed statistics (sum, count, and quantiles) to the monitoring system.

  • Benefit: Less data sent over the network, but less flexible for custom percentile calculations later.
  • Use for: Latency measurements where specific percentiles are known to be needed.

Histograms vs. Summaries

Both Histograms and Summaries track distributions, but they differ in where calculations happen:

  • Histograms: Send raw data (counts in buckets). Percentiles are calculated on the server. More flexible for ad-hoc analysis.
  • Summaries: Calculate percentiles on the client and send pre-computed results. More resource-efficient if you know exactly which percentiles you need.

For most modern systems, Histograms are generally preferred due to their flexibility.

Picking the Best Metric Type

Choosing the right metric type is key for effective monitoring:

  • Gauges: For current values that can go up/down (e.g., disk usage, active users).
  • Counters: For cumulative totals that only increase (e.g., total errors, processed items).
  • Histograms: For distributions of values where you need server-side percentile calculation and flexibility (e.g., request durations).
  • Summaries: For distributions where client-side pre-calculated percentiles are sufficient and network efficiency is critical (less common than histograms now).

Metric Type Challenge

Your application processes user orders. You want to track the total number of orders placed since the application started, and also the current number of items in the processing queue.

Key Takeaways on Metrics

Great job! You've now learned about the four fundamental metric types:

  • Gauges: For current, fluctuating values.
  • Counters: For cumulative, ever-increasing totals.
  • Histograms: For understanding the distribution of values and calculating percentiles server-side.
  • Summaries: For pre-calculated percentiles client-side.

Understanding these types helps you choose the right tool for the right job, leading to more insightful monitoring and faster debugging!

免费开始

用 AI 导师学习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「指标类型详解」课时是免费的吗?

是的 — 「指标类型详解」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 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)