๐Ÿ”งDevOps

Prometheus

Prometheus

A time-series metrics collection/storage system. Queried with PromQL. Used with Grafana.

Detailed Explanation

Prometheus is an open-source monitoring system that began development at SoundCloud in 2012 and became the second graduated project of the CNCF (Cloud Native Computing Foundation) in 2016. It is an all-in-one solution that has a built-in time series database (TSDB) to collect, store, and query metrics.

Its key feature is the Pull model. Prometheus accesses configured targets over HTTP and fetches metrics from the /metrics endpoint. This approach keeps coupling loose, because the monitored targets do not need to know about Prometheus. The default collection interval (scrape_interval) is 15 seconds.

PromQL (Prometheus Query Language) is a powerful metric query language. Functions such as rate(), increase(), and histogram_quantile() can express complex calculations. For example: rate(http_requests_total[5m]) calculates the number of requests per second over 5 minutes.

In practice, the combination of Prometheus + Grafana (visualization) + Alertmanager (alerting) is the standard. In Kubernetes environments, automatic target discovery is possible with the ServiceMonitor CRD, and in large-scale environments, long-term retention and high availability are achieved with Thanos or Mimir.

Code Example

# prometheus.yml - Prometheus ์„ค์ • ํŒŒ์ผ
global:
  scrape_interval: 15s      # ๋ฉ”ํŠธ๋ฆญ ์ˆ˜์ง‘ ์ฃผ๊ธฐ
  evaluation_interval: 15s  # ๋ฃฐ ํ‰๊ฐ€ ์ฃผ๊ธฐ
  external_labels:
    cluster: 'production'
    region: 'ap-northeast-2'

# ์•Œ๋ฆผ ๊ทœ์น™ ํŒŒ์ผ
rule_files:
  - '/etc/prometheus/rules/*.yml'

# Alertmanager ์„ค์ •
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

# ๋ฉ”ํŠธ๋ฆญ ์ˆ˜์ง‘ ๋Œ€์ƒ
scrape_configs:
  # Prometheus ์ž์ฒด ๋ฉ”ํŠธ๋ฆญ
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # Kubernetes ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      # prometheus.io/scrape: "true" ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ๋Š” ํŒŒ๋“œ๋งŒ ์ˆ˜์ง‘
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        target_label: __address__
        regex: (.+)
        replacement: ${1}:$1

  # Node Exporter (์„œ๋ฒ„ ๋ฉ”ํŠธ๋ฆญ)
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']

---
# alert-rules.yml - ์•Œ๋ฆผ ๊ทœ์น™
groups:
  - name: application
    rules:
      # ๋†’์€ ์—๋Ÿฌ์œจ ์•Œ๋ฆผ
      - alert: HighErrorRate
        expr: |
          sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
          /
          sum(rate(http_requests_total[5m])) by (service)
          > 0.05
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.service }} ์—๋Ÿฌ์œจ 5% ์ดˆ๊ณผ"
          description: "์ตœ๊ทผ 5๋ถ„๊ฐ„ ์—๋Ÿฌ์œจ: {{ $value | humanizePercentage }}"

      # ๋†’์€ ๋ ˆ์ดํ„ด์‹œ ์•Œ๋ฆผ
      - alert: HighLatency
        expr: |
          histogram_quantile(0.95,
            sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service)
          ) > 1
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "{{ $labels.service }} P95 ๋ ˆ์ดํ„ด์‹œ 1์ดˆ ์ดˆ๊ณผ"

---
# ์œ ์šฉํ•œ PromQL ์ฟผ๋ฆฌ ์˜ˆ์‹œ

# 1. CPU ์‚ฌ์šฉ๋ฅ  (%)
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# 2. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋ฅ  (%)
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

# 3. ์ดˆ๋‹น ์š”์ฒญ ์ˆ˜ (RPS)
sum(rate(http_requests_total[5m])) by (service)

# 4. P99 ์‘๋‹ต ์‹œ๊ฐ„
histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))

# 5. ํŒŒ๋“œ ์žฌ์‹œ์ž‘ ํšŸ์ˆ˜ (์ตœ๊ทผ 1์‹œ๊ฐ„)
increase(kube_pod_container_status_restarts_total[1h])

How It's Said in Practice

Senior: "Prometheus metric retention is 15 days, but we need long-term retention to pull monthly reports. Have you looked into adopting Thanos?"

Junior: "Yes, if we store to S3 with the Thanos Sidecar, we can retain data without limit, and I think about $50 a month would be enough for the cost."

Interviewer: "What is the difference between Prometheus's Pull model and Push model?"

Candidate: "In the Pull model, Prometheus fetches metrics from targets, so problems are easy to detect when there is a network issue, while in the Push model, targets send the metrics, which suits ephemeral jobs. Prometheus uses Pull by default, but it also supports Push through the Pushgateway."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "If the range in this rate() query is smaller than scrape_interval, you won't get accurate values. You need to raise [1m] to at least [2m]."

Developer: "Ah, right. Since scrape_interval is 15 seconds, I'll set it to at least 4 times that, which is 1 minute or more."

Cautions

Learn More