What Are The 4 Types Of Metrics In Prometheus

Better Stack Team
Updated on November 29, 2024

Prometheus supports four main metric types, each suited for specific use cases. These types help capture various aspects of system performance and behavior.

1. Counter

A counter is a cumulative metric that only increases over time or resets to zero. It is ideal for tracking counts, such as the number of requests or errors.

Characteristics:

  • Monotonically increasing.
  • Resets only when the application restarts.

Use cases:

  • Total number of HTTP requests.
  • Count of failed operations.
  • Number of tasks completed.

Example:

 
from prometheus_client import Counter

http_requests = Counter('http_requests_total', 'Total HTTP requests')
http_requests.inc()  # Increment by 1
http_requests.inc(5)  # Increment by 5

2. Gauge

A gauge represents a value that can go up or down. It measures instantaneous values, such as memory usage or active connections.

Characteristics:

  • Can increase or decrease.
  • Tracks current state at a specific moment.

Use cases:

  • CPU or memory usage.
  • Number of in-progress requests.
  • Size of a queue.

Example:

 
from prometheus_client import Gauge

memory_usage = Gauge('memory_usage_bytes', 'Current memory usage in bytes')
memory_usage.set(512)  # Set value
memory_usage.inc(128)  # Increment
memory_usage.dec(64)   # Decrement

3. Histogram

A histogram samples observations and categorizes them into predefined buckets. It is used to measure the distribution of values, such as request durations or response sizes.

Characteristics:

  • Buckets define ranges for observations.
  • Provides cumulative counts for each bucket and a +Inf bucket for all values.
  • Also tracks the total sum and count of observations.

Use cases:

  • Measuring request latency.
  • Analyzing response size distribution.
  • Tracking database query durations.

Example:

 
from prometheus_client import Histogram

request_latency = Histogram(
    'http_request_latency_seconds', 'Request latency in seconds',
    buckets=[0.1, 0.5, 1, 2.5, 5, 10]
)
request_latency.observe(1.2)  # Record an observation

Metrics exposed:

  • http_request_latency_seconds_bucket{le="0.5"} 15
  • http_request_latency_seconds_sum 25
  • http_request_latency_seconds_count 30

4. Summary

A summary is similar to a histogram but focuses on providing quantiles (e.g., 95th percentile). It tracks the total number of observations, their sum, and configurable quantile values.

Characteristics:

  • Quantiles are configurable per summary.
  • Does not use buckets.
  • Best for small-scale scenarios where exact quantiles are needed.

Use cases:

  • Measuring request latency with specific percentiles.
  • Monitoring exact error rates.

Example:

 
from prometheus_client import Summary

request_latency = Summary('http_request_latency_seconds', 'Request latency in seconds')
request_latency.observe(1.2)  # Record an observation

Comparison of metric types

Metric Type Tracks Can decrease Aggregation support (e.g., rate(), sum())
Counter Cumulative values No Yes
Gauge Instantaneous values Yes Yes
Histogram Value distributions No Yes
Summary Value distributions No Limited (quantiles are pre-computed)

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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