forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
139 lines (113 loc) · 4.04 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
prometheusBufferSize *prometheus.HistogramVec
prometheusBufferDefaultSize *prometheus.GaugeVec
prometheusBufferMaxSize *prometheus.GaugeVec
prometheusVipsMemory prometheus.Gauge
prometheusVipsMaxMemory prometheus.Gauge
prometheusVipsAllocs prometheus.Gauge
)
func initPrometheus() {
if len(conf.PrometheusBind) == 0 {
return
}
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 occurred 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.",
})
prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "buffer_size_bytes",
Help: "A histogram of the buffer size in bytes.",
}, []string{"type"})
prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "buffer_default_size_bytes",
Help: "A gauge of the buffer default size in bytes.",
}, []string{"type"})
prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "buffer_max_size_bytes",
Help: "A gauge of the buffer max size in bytes.",
}, []string{"type"})
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.",
})
prometheus.MustRegister(
prometheusRequestsTotal,
prometheusErrorsTotal,
prometheusRequestDuration,
prometheusDownloadDuration,
prometheusProcessingDuration,
prometheusBufferSize,
prometheusBufferDefaultSize,
prometheusBufferMaxSize,
prometheusVipsMemory,
prometheusVipsMaxMemory,
prometheusVipsAllocs,
)
prometheusEnabled = true
s := http.Server{Handler: promhttp.Handler()}
go func() {
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 {
logFatal(err.Error())
}
}()
}
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()
}
func observePrometheusBufferSize(t string, size int) {
prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
}
func setPrometheusBufferDefaultSize(t string, size int) {
prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
}
func setPrometheusBufferMaxSize(t string, size int) {
prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
}