Prometheus and PromQL: Metrics, Queries and Alerting Basics

How Prometheus scrapes metrics, metric types (counter, gauge, histogram), essential PromQL queries for rate, errors and latency, and writing alerts.

Published December 4, 2026

Prometheus is the default metrics system in the cloud-native world. It scrapes numeric time series from your services, stores them efficiently, and lets you query them with PromQL to build dashboards and alerts. A small set of query patterns covers most of what you need day to day.

The concept

Prometheus pulls metrics by scraping HTTP endpoints (usually /metrics) on a schedule. Each time series is a metric name plus labels, like http_requests_total{method="GET", status="200", route="/orders"}. Service discovery (Kubernetes, cloud APIs, static configs) tells Prometheus what to scrape.

The main metric types are counters (only increase, such as total requests), gauges (go up and down, such as memory in use or queue depth) and histograms (bucketed observations, such as request durations, from which percentiles are computed). Because counters only increase, you almost always query them with rate() or increase() over a time window.

Metric types

TypeBehaviourQuery withExample
CounterOnly increases (resets on restart)rate(), increase()http_requests_total
GaugeGoes up and downRaw value, avg_over_time()queue_depth
HistogramBucketed observationshistogram_quantile()http_request_duration_seconds
SummaryClient-side quantilesRaw quantile seriesLess common; not aggregatable

How it works

pullstorequeryApp /metricsexposition formatPrometheusscrape every 15-30 sTSDBtime seriesGrafana + Alertmanagerdashboards, alerts
The Prometheus model.

Essential queries

QuestionPromQL
Requests per secondsum(rate(http_requests_total[5m]))
Error ratiosum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m]))
p95 latencyhistogram_quantile(0.95, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))
Per-route RPSsum by (route) (rate(http_requests_total[5m]))
Memory by podsum by (pod) (container_memory_working_set_bytes{namespace="shop"})

Practical examples

Expose metrics from a Python app

The official client library.

from prometheus_client import Counter, Histogram, start_http_server
REQUESTS = Counter("http_requests_total", "Requests", ["route", "status"])
LATENCY = Histogram("http_request_duration_seconds", "Latency", ["route"])

start_http_server(9100)
with LATENCY.labels(route="/orders").time():
    handle()
REQUESTS.labels(route="/orders", status="200").inc()

An alerting rule

Fire when the error ratio stays high for 10 minutes.

groups:
  - name: api
    rules:
      - alert: HighErrorRate
        expr: |
          sum(rate(http_requests_total{job="api",status=~"5.."}[5m]))
          / sum(rate(http_requests_total{job="api"}[5m])) > 0.05
        for: 10m
        labels: { severity: page }
        annotations: { summary: "API error rate above 5%" }

Kubernetes scraping with a ServiceMonitor

Prometheus Operator discovers targets by labels.

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata: { name: api }
spec:
  selector: { matchLabels: { app: api } }
  endpoints: [{ port: metrics, interval: 30s }]

Common mistakes

  • Using user IDs or request IDs as label values (cardinality explosion).
  • Querying counters without rate() and seeing ever-growing lines.
  • Averaging percentiles across instances instead of aggregating histogram buckets.
  • Alerting on every symptom instead of user-facing SLO burn.

Key takeaways

  • Prometheus scrapes labelled time series from /metrics endpoints.
  • Counters need rate(); histograms give percentiles via histogram_quantile.
  • Keep label cardinality bounded.
  • Alert on meaningful conditions with a for: duration.

FAQ

How long does Prometheus keep data?

Local retention defaults to 15 days; long-term storage uses remote write to systems like Thanos, Mimir or managed services.

Push or pull?

Prometheus pulls; short-lived batch jobs can use the Pushgateway, and OpenTelemetry can push via remote write.

What is a good scrape interval?

15-60 seconds for most workloads; shorter intervals increase storage and load.

More DevOps articles