# What Are The 4 Types Of Metrics In Prometheus

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

[ad-logs-small]

## 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:

```python
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:

```python
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:

```python
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:

```python
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)          |


[ad-uptime]