2018-10-29 14:04:47 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
2019-01-28 18:18:54 +02:00
|
|
|
prometheusBufferSize *prometheus.HistogramVec
|
2019-01-31 10:00:31 +02:00
|
|
|
prometheusBufferDefaultSize *prometheus.GaugeVec
|
|
|
|
prometheusBufferMaxSize *prometheus.GaugeVec
|
2019-02-21 16:55:16 +02:00
|
|
|
prometheusVipsMemory prometheus.Gauge
|
|
|
|
prometheusVipsMaxMemory prometheus.Gauge
|
|
|
|
prometheusVipsAllocs prometheus.Gauge
|
2018-10-29 14:04:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func initPrometheus() {
|
2018-11-02 17:34:55 +02:00
|
|
|
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",
|
2019-02-01 13:53:46 +02:00
|
|
|
Help: "A counter of the occurred errors separated by type.",
|
2018-10-29 14:04:47 +02:00
|
|
|
}, []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.",
|
|
|
|
})
|
|
|
|
|
2019-01-28 18:18:54 +02:00
|
|
|
prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
2019-01-31 10:56:51 +02:00
|
|
|
Name: "buffer_size_bytes",
|
|
|
|
Help: "A histogram of the buffer size in bytes.",
|
2019-01-28 18:18:54 +02:00
|
|
|
}, []string{"type"})
|
|
|
|
|
2019-01-31 10:00:31 +02:00
|
|
|
prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
2019-01-31 10:56:51 +02:00
|
|
|
Name: "buffer_default_size_bytes",
|
|
|
|
Help: "A gauge of the buffer default size in bytes.",
|
2019-01-31 10:00:31 +02:00
|
|
|
}, []string{"type"})
|
|
|
|
|
|
|
|
prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
2019-01-31 10:56:51 +02:00
|
|
|
Name: "buffer_max_size_bytes",
|
|
|
|
Help: "A gauge of the buffer max size in bytes.",
|
2019-01-31 10:00:31 +02:00
|
|
|
}, []string{"type"})
|
|
|
|
|
2019-02-21 16:55:16 +02:00
|
|
|
prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "vips_memory_bytes",
|
|
|
|
Help: "A gauge of the vips tracked memory usage in bytes.",
|
|
|
|
})
|
|
|
|
|
|
|
|
prometheusVipsMaxMemory = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "vips_max_memory_bytes",
|
|
|
|
Help: "A gauge of the max vips tracked memory usage in bytes.",
|
|
|
|
})
|
|
|
|
|
|
|
|
prometheusVipsAllocs = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "vips_allocs",
|
|
|
|
Help: "A gauge of the number of active vips allocations.",
|
|
|
|
})
|
|
|
|
|
2018-10-29 14:04:47 +02:00
|
|
|
prometheus.MustRegister(
|
|
|
|
prometheusRequestsTotal,
|
|
|
|
prometheusErrorsTotal,
|
|
|
|
prometheusRequestDuration,
|
|
|
|
prometheusDownloadDuration,
|
|
|
|
prometheusProcessingDuration,
|
2019-01-28 18:18:54 +02:00
|
|
|
prometheusBufferSize,
|
2019-01-31 10:00:31 +02:00
|
|
|
prometheusBufferDefaultSize,
|
|
|
|
prometheusBufferMaxSize,
|
2019-02-21 16:55:16 +02:00
|
|
|
prometheusVipsMemory,
|
|
|
|
prometheusVipsMaxMemory,
|
|
|
|
prometheusVipsAllocs,
|
2018-10-29 14:04:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
prometheusEnabled = true
|
|
|
|
|
2019-06-27 10:00:39 +02:00
|
|
|
s := http.Server{Handler: promhttp.Handler()}
|
2018-10-29 14:04:47 +02:00
|
|
|
|
|
|
|
go func() {
|
2019-06-27 10:00:39 +02:00
|
|
|
l, err := listenReuseport("tcp", conf.PrometheusBind)
|
|
|
|
if err != nil {
|
|
|
|
logFatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
logNotice("Starting Prometheus server at %s\n", conf.PrometheusBind)
|
|
|
|
if err := s.Serve(l); err != nil && err != http.ErrServerClosed {
|
2019-01-11 17:01:48 +02:00
|
|
|
logFatal(err.Error())
|
2018-10-29 14:04:47 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2019-01-28 18:18:54 +02:00
|
|
|
|
2019-01-31 10:56:51 +02:00
|
|
|
func observePrometheusBufferSize(t string, size int) {
|
|
|
|
prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
|
2019-01-28 18:18:54 +02:00
|
|
|
}
|
2019-01-31 10:00:31 +02:00
|
|
|
|
2019-01-31 10:56:51 +02:00
|
|
|
func setPrometheusBufferDefaultSize(t string, size int) {
|
|
|
|
prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
|
2019-01-31 10:00:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:56:51 +02:00
|
|
|
func setPrometheusBufferMaxSize(t string, size int) {
|
|
|
|
prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
|
2019-01-31 10:00:31 +02:00
|
|
|
}
|