CodeBlanks Logo Icon CodeBlanks

PromQL basics

Learn to query metrics with PromQL.

PromQL basics

PromQL is Prometheus's query language. A metric name selects all series, label filters a subset; [5m] is a time range:

http_requests_total                                # all series
http_requests_total{status="500"}                  # equality
http_requests_total{status=~"5.."}                 # regular expression
rate(http_requests_total[5m])                      # per-second increase
sum by (path) (rate(http_requests_total[5m]))      # total per path

Counters are always used with rate() or increase(); the raw value resets on restart.

Kubernetes metrics

kube-prometheus-stack also collects cAdvisor (container resources) and kube-state-metrics (object state) metrics:

# CPU usage per namespace (cores)
sum by (namespace) (rate(container_cpu_usage_seconds_total[5m]))
# containers that restarted
increase(kube_pod_container_status_restarts_total[1h]) > 0
# deployments with unavailable replicas
kube_deployment_status_replicas_available < kube_deployment_spec_replicas

Queries are run in the Prometheus UI or with the /api/v1/query HTTP API.

Practice Query via the API

Write two commands: forward the svc/izleme-kube-prometheus-sta-prometheus service in izleme from local 9090 to 9090; then send --data-urlencode 'query=sum(rate(http_requests_total[5m]))' to the http://localhost:9090/api/v1/query endpoint with curl -s.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl port-forward -n izleme svc/izleme-kube-prometheus-sta-prometheus 9090:9090
curl -s http://localhost:9090/api/v1/query --data-urlencode 'query=sum(rate(http_requests_total[5m]))'

Expected output

Forwarding from 127.0.0.1:9090 -> 9090
{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1793710800,"42.7"]}]}}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does increase(http_requests_total[1h]) give?

  • The per-second rate
  • The counter's raw value
  • The value an hour ago
  • The total increase in requests over the last hour Correct answer

Explanation: increase ≈ rate × the range duration.

2 Calculate the per-second rate for a counter.

Question
____(http_requests_total[5m])
Answer
rate(http_requests_total[5m])

Explanation: rate is the counter's average increase speed over the range.

3 Match.

Term Meaning
{status="500"} Equality filter
{status=~"5.."} Regex filter
[5m] Time range
sum by (path) Grouped sum

All lessons in Advanced