Data Types in Prometheus
2 min readOct 1, 2024
In Prometheus, metrics are categorized into different types based on how they represent data. The four primary metric types are Counter, Gauge, Summary, and Histogram. Here’s a breakdown of each:
1. Counter
- Definition: A Counter is a metric that represents a cumulative count of occurrences of a particular event.
- Characteristics:
- Only increases over time (can be reset to zero).
- Suitable for counting things like requests served, errors encountered, etc.
- Example Use Case: Counting the number of HTTP requests received.
- Usage Example:
httpRequestsTotal := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests.",
},
[]string{"method", "handler"},
)
2. Gauge
- Definition: A Gauge is a metric that represents a single numerical value that can go up and down.
- Characteristics:
- Suitable for measuring values that can fluctuate, such as temperature, memory usage, or current queue size.
- Can increase or decrease at any time.
- Example Use Case: Current memory usage of an application.
- Usage Example:
temperatureGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "temperature_celsius",
Help: "Current temperature in Celsius.",
},
[]string{"location"},
)
3. Summary
- Definition: A Summary is used to sample observations (e.g., request durations or response sizes) and provides quantiles (e.g., 0.5, 0.95, 0.99) and total count/total sum.
- Characteristics:
- Provides the ability to track how often observations fall into various buckets and calculate quantiles (percentiles).
- It can have high overhead due to the storage of samples and the need for computing quantiles.
- Example Use Case: Tracking request latencies to calculate average response time.
- Usage Example:
requestDuration := prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "request_duration_seconds",
Help: "Histogram of request durations.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"path"},
)
4. Histogram
- Definition: A Histogram counts observations (e.g., execution durations or sizes) and provides a count and sum for observations falling into configurable buckets.
- Characteristics:
- Similar to a Summary but is more efficient for monitoring latency, as it provides histogram buckets and total counts.
- Bucketing allows more flexible analyses over a wider range of data without needing individual observation sampling.
- Example Use Case: Measuring request sizes or latency aggregations.
- Usage Example:
requestLatency := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "request_latency_seconds",
Help: "Histogram of request latency.",
Buckets: prometheus.LinearBuckets(0, 0.1, 10), // From 0 to 1 second, in 0.1 second increments.
},
[]string{"method"},
)
Summary of Differences
- Counter: Cumulative count that only increases (or resets).
- Gauge: Represents a value that can go up and down.
- Summary: Samples observations and provides quantiles but can consume more memory.
- Histogram: Counts observations in configurable buckets, offering efficient overall latency tracking.
Choosing the appropriate metric type depends on your specific monitoring needs and the nature of the data being collected.