πŸ“Š Prometheus Metric Types: The Complete Guide to Mastery!

Hello everyone! πŸ‘‹ Today, we’re going to take a deep dive into the core components of Prometheus, an essential tool for system and application monitoring: metric types. Whether you’re new to Prometheus or already using it, this is a great opportunity to solidify your understanding of these concepts! πŸš€

To accurately understand the state of your system, it’s crucial to decide what data to collect and how. Prometheus offers four core metric types for this purpose: Counter, Gauge, Histogram, and Summary. Let’s explore the characteristics of each type and when to use them, one by one!


1. Counter πŸ“ˆ: A Number That Only Goes Up

A Counter, as its name suggests, is a metric type that records cumulative values. Its most significant characteristic is that its value can only increase. It’s like a car’s odometer, always moving forward, never backward. Of course, it can be reset to 0 if the system restarts.

  • πŸ€” When to use it?
  • Total number of HTTP requests to a web server
  • Number of successful or failed API calls
  • Number of completed tasks
  • Total number of errors occurred
  • Example: http_requests_total
  • This metric represents the total number of HTTP requests received since the service started.
  • πŸ’‘ Key Point!
  • Counters are primarily used to extract meaningful data, such as “requests per second,” by calculating the rate of change over time rather than the counter’s absolute value. For this, PromQL’s rate() or irate() functions are used.

2. Gauge 🌑️: A Thermometer Showing Real-time Status

A Gauge is a metric type that represents the current state measurement. Unlike a Counter, its value can both increase and decrease. It’s like a thermometer that goes up and down with the ambient temperature.

  • πŸ€” When to use it?
  • Current memory usage
  • Number of active user sessions
  • Number of tasks in a queue
  • CPU utilization
  • Example: memory_usage_bytes, cpu_temperature_celsius
  • memory_usage_bytes indicates the current memory used by the application, and cpu_temperature_celsius shows the current CPU temperature. These values can change frequently.
  • πŸ’‘ Key Point!
  • Useful when you want to understand the current state of the system like a snapshot.

3. Histogram πŸ“Š: Data Distribution at a Glance

A Histogram is a slightly more complex metric type that measures the distribution of observed data. It’s primarily used when understanding the distribution of values is important, such as request processing time (latency) or response size.

Histograms divide data into several predefined buckets. For example, buckets might be defined as less than or equal to 0.1 seconds, 0.5 seconds, 1 second, etc. Each time a request is processed, the count for the corresponding bucket is incremented by 1.

  • πŸ€” When to use it?
  • Response time distribution of API requests
  • HTTP request size distribution

Internally, a Histogram creates several time series data points:

  • _bucket{le=”“}: The cumulative count of values less than or equal to the upper bound (le) of each bucket.
  • _sum: The sum of all observed values.
  • _count: The total number of observed events.
  • Example: http_request_duration_seconds
  • This metric allows you to calculate percentiles, such as “what percentage of all requests were processed within X seconds?”
  • πŸ’‘ Key Point!
  • Extremely useful for aggregating data and calculating percentiles across multiple instances. Percentiles can be flexibly calculated on the server side using PromQL’s histogram_quantile() function.

4. Summary πŸ“: A Summary for Accurate Percentiles

A Summary, like a Histogram, is also used to measure the distribution of data. However, there’s a crucial difference in how they operate. Unlike Histograms, which calculate percentiles on the server, Summaries calculate and expose percentiles (quantiles) directly on the client (application) side.

  • πŸ€” When to use it?
  • When you need to know the median (50th percentile), 95th percentile, 99th percentile, etc., of response times accurately.

A Summary creates the following time series data:

  • {quantile=”“}: The value corresponding to the configured quantile.
  • _sum: The sum of all observed values.
  • _count: The total number of observed events.
  • Example: http_request_duration_seconds (as a Summary type)
  • This metric directly provides percentile values, such as http_request_duration_seconds{quantile=”0.95″}.
  • πŸ’‘ Key Point!
  • Since percentiles are calculated directly on the client, the percentile values themselves are very accurate. However, a major drawback is that it’s not possible to aggregate data from multiple instances (e.g., calculate an average). Therefore, in most cases, the more flexible Histogram is recommended for aggregation.

🧐 Metric Type Comparison at a Glance

Metric Type Characteristic Key Use Cases PromQL Function Example
Counter Monotonically increasing cumulative value Request count, error count rate(), increase()
Gauge Current value that can go up or down Memory usage, CPU temperature avg_over_time()
Histogram Data distribution (percentiles calculated on server) Response time distribution, request size histogram_quantile()
Summary Data distribution (percentiles calculated on client) Accurate single-instance percentiles Direct lookup

Conclusion

We’ve now explored the four core metric types of Prometheus. Understanding the characteristics and uses of each type and applying the appropriate metric for the situation is the first step towards building an effective monitoring system.

  • For simple counting, use a Counter!
  • If you need the current state value, use a Gauge!
  • For data distribution and aggregation, use a Histogram!
  • If you need accurate individual percentiles, use a Summary!

We hope this guide helps you on your Prometheus journey! 😊

Tags: Prometheus, Monitoring, Metric Types, Counter, Gauge, Histogram, Summary, Observability, DevOps


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *