Recraftory

Prometheus Dasar

Sistem monitoring dan alerting open-source

Apa itu Prometheus

  • Sistem monitoring dan alerting open-source
  • Dibuat oleh SoundCloud, sekarang CNCF graduated project
  • Menggunakan model pull-based (scraping) metrics
  • Query language: PromQL

Arsitektur Prometheus

Komponen Utama

  • Prometheus Server: Scraping, storage, query
  • Alertmanager: Routing dan deduplikasi alert
  • Pushgateway: Untuk short-lived job
  • Exporters: Konversi metric ke format Prometheus

Pull vs Push

Pull Model

  • Prometheus mengambil metric dari target
  • Service expose endpoint /metrics
  • Lebih reliable untuk long-running service

Push Model

  • Aplikasi mengirim metric ke collector
  • Diperlukan untuk batch job atau short-lived task
  • Pushgateway untuk kasus ini

Data Model Prometheus

Time Series

  • Metric dengan timestamp dan nilai
  • Identifikasi dengan nama dan label

Format Metric

http_requests_total{method="GET",status="200"} 1027
http_requests_total{method="POST",status="500"} 4
  • Metric Name: http_requests_total
  • Labels: method, status
  • Value: angka

Tipe Metric

Counter

  • Nilai hanya naik atau reset ke nol
  • Contoh: total request, total error
  • Gunakan rate() atau increase() untuk query

Gauge

  • Nilai bisa naik dan turun
  • Contoh: memory usage, queue size, temperature

Histogram

  • Mengelompokkan data ke bucket
  • Contoh: request latency
  • Otomatis menghitung _count, _sum, dan _bucket

Summary

  • Mirip histogram tapi dengan quantile yang dihitung di client
  • Lebih akurat tapi lebih berat di aplikasi

Instalasi

Docker

docker run -p 9090:9090 prom/prometheus

Kubernetes (Helm)

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/prometheus

Konfigurasi Scraping

prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']

  - job_name: 'my-app'
    static_configs:
      - targets: ['app:8080']

Exporter Populer

  • Node Exporter: Metric OS dan hardware
  • Blackbox Exporter: Probe endpoint eksternal
  • MySQL Exporter: Metric database
  • Nginx Exporter: Metric web server
  • Redis Exporter: Metric cache

PromQL Dasar

Memilih Metric

http_requests_total
http_requests_total{status="200"}

Rate dan Increase

rate(http_requests_total[5m])
increase(http_requests_total[1h])

Aggregation

sum(rate(http_requests_total[5m])) by (status)
avg(http_request_duration_seconds) by (job)

Operator

# Arithmatic
up == 1

# Comparison
http_requests_total > 100

# Logical
up and on(instance) (memory_usage_bytes > 1000000)

Best Practice

  • Gunakan label dengan konsisten (cardinality rendah)
  • Hindari label dengan nilai yang unik per request (user ID, email)
  • Gunakan counter untuk total, gauge untuk instantaneous
  • Retention time default 15 hari, pertimbangkan long-term storage
  • Alert pada symptom, bukan cause