Sổ tay kỹ thuật

PromQL – query hay dùng cho telemetry mạng

cheatsheet#prometheus#promql#grafana· cập nhật 2026-11-06

Tra nhanh PromQL tính bps từ counter, top-N, gom nhóm, percentile, phát hiện mất metric và viết recording rule.

Sửa trang này
Mục lục

Giả định metric do Telegraf prometheus_client xuất (Telegraf gNMI): interfaces_in_octets, interfaces_out_octets, interfaces_in_errors, label source (thiết bị) và name (interface).

Counter và tốc độ

Mục đích Query
bps vào trung bình 5 phút rate(interfaces_in_octets[5m]) * 8
bps tức thời (2 mẫu cuối, nhạy với spike) irate(interfaces_in_octets[1m]) * 8
Tổng byte trong 1 giờ increase(interfaces_in_octets[1h])
Số lần counter bị reset resets(interfaces_in_octets[1d])
Interface có lỗi tăng trong 15 phút increase(interfaces_in_errors[15m]) > 0
Số lần trạng thái đổi (flap) changes(interfaces_oper_status[1h]) > 2

Luôn rate() trước rồi mới sum(). sum trước rate làm hỏng xử lý counter reset. Range vector nên ≥ 4 lần scrape interval.

Lọc label

Mục đích Query
Một thiết bị interfaces_in_octets{source="198.51.100.11"}
Regex interfaces_in_octets{name=~"HundredGigE.*"}
Loại trừ regex interfaces_in_octets{name!~"Null.*|Loopback.*"}
Biến Grafana nhiều giá trị interfaces_in_octets{source=~"$device"}

Gom nhóm và top-N

Mục đích Query
Tổng bps theo thiết bị sum by (source) (rate(interfaces_in_octets[5m])) * 8
Tổng, bỏ label name sum without (name) (rate(interfaces_in_octets[5m]))
Top 10 interface topk(10, rate(interfaces_in_octets[5m]) * 8)
5 interface ít traffic nhất bottomk(5, rate(interfaces_in_octets[5m]))
Đếm interface mỗi thiết bị count by (source) (interfaces_in_octets)
Phần trăm sử dụng (speed Mbps) rate(interfaces_in_octets[5m]) * 8 / on (source, name) (interfaces_speed * 1e6) * 100
Giá trị lớn nhất trong 1 ngày max_over_time(rate(interfaces_in_octets[5m])[1d:5m])
So với tuần trước rate(interfaces_in_octets[5m]) / rate(interfaces_in_octets[5m] offset 1w)

Histogram và dự báo

Mục đích Query
p99 latency histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))
p95 theo job histogram_quantile(0.95, sum by (job, le) (rate(http_request_duration_seconds_bucket[5m])))
Disk đầy trong 24h tới predict_linear(node_filesystem_avail_bytes[6h], 24 * 3600) < 0
p95 của gauge theo thời gian quantile_over_time(0.95, node_load1[1h])

Phát hiện mất dữ liệu

Mục đích Query
Target scrape thất bại up == 0
Metric biến mất hoàn toàn absent(interfaces_in_octets{source="198.51.100.11"})
Không có mẫu trong 10 phút absent_over_time(interfaces_in_octets{source="198.51.100.11"}[10m])
Series không cập nhật (stale) time() - timestamp(interfaces_in_octets) > 300
Đổi tên label label_replace(up, "host", "$1", "instance", "(.*):.*")

Recording rule

/etc/prometheus/rules/interfaces.yml
groups:
  - name: interfaces
    interval: 30s
    rules:
      - record: source_name:interfaces_in_bps:rate5m
        expr: rate(interfaces_in_octets[5m]) * 8
      - record: source:interfaces_in_bps:sum_rate5m
        expr: sum by (source) (rate(interfaces_in_octets[5m])) * 8

Khai báo trong prometheus.yml bằng rule_files: ["rules/*.yml"]. Cảnh báo dựa trên metric này xem Grafana Alerting.

promtool và API

Mục đích Lệnh
Kiểm tra file rule promtool check rules /etc/prometheus/rules/interfaces.yml
Kiểm tra config promtool check config /etc/prometheus/prometheus.yml
Chạy query từ CLI promtool query instant http://localhost:9090 'topk(5, rate(interfaces_in_octets[5m]))'
Query qua HTTP API curl -s localhost:9090/api/v1/query --data-urlencode 'query=up' | jq .data.result
Reload (cần --web.enable-lifecycle) curl -X POST localhost:9090/-/reload
Series nhiều nhất (cardinality) topk(10, count by (__name__) ({__name__=~".+"}))