1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/prometheus.go

85 lines
2.1 KiB
Go
Raw Normal View History

2018-10-29 14:04:47 +02:00
package main
import (
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
prometheusEnabled = false
prometheusRequestsTotal prometheus.Counter
prometheusErrorsTotal *prometheus.CounterVec
prometheusRequestDuration prometheus.Histogram
prometheusDownloadDuration prometheus.Histogram
prometheusProcessingDuration prometheus.Histogram
)
func initPrometheus() {
if len(conf.PrometheusBind) == 0 {
return
}
2018-10-29 14:04:47 +02:00
prometheusRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "requests_total",
Help: "A counter of the total number of HTTP requests imgproxy processed.",
})
prometheusErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "errors_total",
Help: "A counter of the occured errors separated by type.",
}, []string{"type"})
prometheusRequestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "A histogram of the response latency.",
})
prometheusDownloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "download_duration_seconds",
Help: "A histogram of the source image downloading latency.",
})
prometheusProcessingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "processing_duration_seconds",
Help: "A histogram of the image processing latency.",
})
prometheus.MustRegister(
prometheusRequestsTotal,
prometheusErrorsTotal,
prometheusRequestDuration,
prometheusDownloadDuration,
prometheusProcessingDuration,
)
prometheusEnabled = true
s := http.Server{
Addr: conf.PrometheusBind,
Handler: promhttp.Handler(),
}
go func() {
log.Printf("Starting Prometheus server at %s\n", s.Addr)
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalln(err)
}
}()
}
func startPrometheusDuration(m prometheus.Histogram) func() {
t := time.Now()
return func() {
m.Observe(time.Since(t).Seconds())
}
}
func incrementPrometheusErrorsTotal(t string) {
prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
}