Prometheus — это open-source система мониторинга и алертинга с временными рядами данных. Разработана в SoundCloud, сейчас входит в CNCF (Cloud Native Computing Foundation).
Ключевые особенности:
Grafana — это платформа для визуализации и анализа метрик с открытым исходным кодом. Позволяет создавать интерактивные дашборды для данных из различных источников, включая Prometheus.
Основные возможности Grafana:
Типичный стек мониторинга:
Мониторинг производительности
Выявление аномалий
Анализ трендов
Автоматические алерты
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// Регистрируем метрики
requestsTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests",
})
prometheus.MustRegister(requestsTotal)
// Экспортер метрик
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":2112", nil)
}
rate(http_requests_total{status="500"}[5m]) / rate(http_requests_total[5m])
scrape_configs:
- job_name: 'my_go_service'
scrape_interval: 15s
static_configs:
- targets: ['localhost:2112']
goCollector := prometheus.NewGoCollector()
prometheus.MustRegister(goCollector)
ordersProcessed := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "orders_processed_total",
Help: "Total number of processed orders",
},
[]string{"status", "payment_method"},
)
httpDurations := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "HTTP request duration distribution",
Buckets: prometheus.DefBuckets,
},
[]string{"path", "method"},
)
Для production-сред обычно используют:
Prometheus и Grafana образуют мощный тандем для мониторинга Go-приложений. Prometheus отвечает за сбор и хранение метрик, Grafana — за их визуализацию. Вместе они позволяют: