> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Counters and Gauges

> Explains Prometheus metric types counters and gauges, their meanings, usage, and PromQL functions to compute rates, plus when to use histograms or summaries.

Prometheus scrapes instrumented targets and reads metrics in the OpenMetrics plain-text format: metric name, optional labels, and a numeric value. For example:

```text theme={null}
# HELP http_requests_total The total number of HTTP requests to the backend service.
# TYPE http_requests_total counter
http_requests_total{endpoint="/items",request_method="GET"} 10
http_requests_total{endpoint="/items",request_method="PUT"} 2
http_requests_total{endpoint="/search",request_method="GET"} 4
# HELP node_memory_usage_bytes Current memory usage in bytes.
# TYPE node_memory_usage_bytes gauge
node_memory_usage_bytes{instance="node01"} 536870912
node_memory_usage_bytes{instance="node02"} 268435456
```

What do the numeric values mean? When Prometheus scrapes a target and sees `10` for an HTTP request metric or a large number for memory usage, is that a snapshot or a cumulative counter since the process started? Those are very different interpretations and change how you query and visualize metrics.

Prometheus supports four metric types — counters, gauges, histograms, and summaries. This guide focuses on the two simplest and most common types: counters and gauges.

## Counters

A counter is a monotonically increasing value. It typically starts at `0` when the instrumented process starts and increases until the process restarts (at which point it resets). Counters are used for values that accumulate over time, such as requests served or bytes transmitted. If a counter appears to decrease, it usually means the process restarted or the instrument was misused; Prometheus query functions are designed to handle these resets.

Example counter observed at multiple scrapes:

```text theme={null}
# at scrape T1
http_requests_total{endpoint="/items",request_method="GET"} 10

# at scrape T2
http_requests_total{endpoint="/items",request_method="GET"} 15

# at scrape T3
http_requests_total{endpoint="/items",request_method="GET"} 44
```

Common use cases for counters:

* Total HTTP requests served
* Total errors returned
* Total network bytes transmitted

![A presentation slide titled "Counters Examples" with a central blue "Counter" icon and three rounded blue boxes listing example metrics: "01 | Total HTTP Requests Served", "02 | Total errors returned", and "03 | Total network bytes transmitted." The slide includes a small "© Copyright KodeKloud" notice in the corner.](https://mintcdn.com/kodekloud-c4ac6d9a/qM9vA-hQoWNCxcKa/images/Grafana-Prometheus-for-Metrics-Driven-Observability/Prometheus-and-Grafana-Introduction/Counters-and-Gauges/counters-examples-http-errors-network-bytes.jpg?fit=max\&auto=format\&n=qM9vA-hQoWNCxcKa\&q=85\&s=783207c4ac20ba82910b1c2fe938f9ef)

The raw cumulative value of a counter is rarely what you want to chart directly. Instead, you typically want the rate of change (e.g., requests per second or errors per minute). Prometheus provides PromQL functions that compute rates and deltas from cumulative counters and correctly handle counter resets:

* Per-second rate (instantaneous over range): `rate(http_requests_total[5m])`
* Total increase in a time window: `increase(http_requests_total[1h])`

Example PromQL:

```promql theme={null}
# requests per second averaged over 5 minutes
rate(http_requests_total{endpoint="/items"}[5m])

# total requests in the last hour
increase(http_requests_total{endpoint="/items"}[1h])
```

Counters are monotonic — they only increase (except when a process restarts). Use PromQL functions like `rate()` and `increase()` to convert cumulative counters into meaningful rates or deltas over time. These functions also handle counter resets for you.

## Gauges

A gauge represents a value that can go up or down and is scraped as a snapshot at the time of collection. Gauges are ideal for current-state measurements such as memory usage, the number of running processes, or queue depth. Client libraries typically expose operations to set a gauge to a specific value or to increment/decrement it.

Example gauge:

```text theme={null}
# A gauge showing current memory usage
node_memory_usage_bytes{instance="node01"} 536870912
node_memory_usage_bytes{instance="node01"} 462422144
```

Gauges are suitable when the instantaneous value is meaningful rather than an accumulated total.

## Counters vs. Gauges — Quick reference

| Metric Type | Description | When to use | Typical PromQL examples |
| - | - | - | - |
| Counter | Monotonically increasing value (resets on process restart) | Counts: requests, errors, bytes transmitted | `rate(my_counter[5m])`, `increase(my_counter[1h])` |
| Gauge | Value that can go up or down (snapshot) | Current state: memory, CPU, queue length | `avg_over_time(my_gauge[5m])`, `max_over_time(my_gauge[1h])` |

## When counters and gauges are not enough

Counters and gauges cover many scenarios, but they do not capture distributions (for example, the spread of request latencies across many requests). To observe distributions and percentiles you should use histograms or summaries:

* Histograms expose bucketed counts, plus a `sum` and `count`, allowing you to compute percentiles and bucketed distributions server-side.
* Summaries expose client-side calculated quantiles along with a `sum` and `count`.

These are better suited for latency and size distributions.

![A presentation slide titled "Next Up: Histograms (and Summaries)" showing an "Application Latency Histogram" with colored bars across latency buckets. The chart has tall green bars around the 50–250ms range and smaller orange/red bars around the 2.5–10s range.](https://mintcdn.com/kodekloud-c4ac6d9a/qM9vA-hQoWNCxcKa/images/Grafana-Prometheus-for-Metrics-Driven-Observability/Prometheus-and-Grafana-Introduction/Counters-and-Gauges/application-latency-histogram-50-250ms-2-10s.jpg?fit=max\&auto=format\&n=qM9vA-hQoWNCxcKa\&q=85\&s=956fc19cc6de60d7ac27a0739d007623)

Histograms and summaries let you capture and query distributions such as request latency across many requests.

## Links and references

* Prometheus docs: [https://prometheus.io/docs/](https://prometheus.io/docs/)
* PromQL reference: [https://prometheus.io/docs/prometheus/latest/querying/basics/](https://prometheus.io/docs/prometheus/latest/querying/basics/)
* OpenMetrics format: [https://openmetrics.io/](https://openmetrics.io/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/grafana-prometheus-for-metrics-driven-observability/module/86c79392-532c-42b7-8908-18bf0a84210b/lesson/d86fe8d5-68ad-4ea2-a776-dc0e0d06860b)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.