@@ -9,29 +9,30 @@ import (
9
9
chimw "github.com/go-chi/chi/v5/middleware"
10
10
11
11
"github.com/prometheus/client_golang/prometheus"
12
- "github.com/prometheus/client_golang/prometheus/promauto"
12
+
13
+ "github.com/coder/coder/coderd/monitoring"
13
14
)
14
15
15
16
var (
16
- requestsProcessed = promauto .NewCounterVec (prometheus.CounterOpts {
17
+ requestsProcessed = prometheus .NewCounterVec (prometheus.CounterOpts {
17
18
Namespace : "coderd" ,
18
19
Subsystem : "api" ,
19
20
Name : "requests_processed_total" ,
20
21
Help : "The total number of processed API requests" ,
21
22
}, []string {"code" , "method" , "path" })
22
- requestsConcurrent = promauto .NewGauge (prometheus.GaugeOpts {
23
+ requestsConcurrent = prometheus .NewGauge (prometheus.GaugeOpts {
23
24
Namespace : "coderd" ,
24
25
Subsystem : "api" ,
25
26
Name : "concurrent_requests" ,
26
27
Help : "The number of concurrent API requests" ,
27
28
})
28
- websocketsConcurrent = promauto .NewGauge (prometheus.GaugeOpts {
29
+ websocketsConcurrent = prometheus .NewGauge (prometheus.GaugeOpts {
29
30
Namespace : "coderd" ,
30
31
Subsystem : "api" ,
31
32
Name : "concurrent_websockets" ,
32
33
Help : "The total number of concurrent API websockets" ,
33
34
})
34
- websocketsDist = promauto .NewHistogramVec (prometheus.HistogramOpts {
35
+ websocketsDist = prometheus .NewHistogramVec (prometheus.HistogramOpts {
35
36
Namespace : "coderd" ,
36
37
Subsystem : "api" ,
37
38
Name : "websocket_durations_ms" ,
45
46
durationToFloatMs (30 * time .Hour ),
46
47
},
47
48
}, []string {"path" })
48
- requestsDist = promauto .NewHistogramVec (prometheus.HistogramOpts {
49
+ requestsDist = prometheus .NewHistogramVec (prometheus.HistogramOpts {
49
50
Namespace : "coderd" ,
50
51
Subsystem : "api" ,
51
52
Name : "request_latencies_ms" ,
@@ -58,45 +59,55 @@ func durationToFloatMs(d time.Duration) float64 {
58
59
return float64 (d .Milliseconds ())
59
60
}
60
61
61
- func Prometheus (next http.Handler ) http.Handler {
62
- return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
63
- var (
64
- start = time .Now ()
65
- method = r .Method
66
- rctx = chi .RouteContext (r .Context ())
67
- )
68
- sw , ok := w .(chimw.WrapResponseWriter )
69
- if ! ok {
70
- panic ("dev error: http.ResponseWriter is not chimw.WrapResponseWriter" )
71
- }
62
+ func Prometheus (monitor * monitoring.Monitor ) func (http.Handler ) http.Handler {
63
+ monitor .MustRegister (
64
+ monitoring .TelemetryLevelNone ,
65
+ requestsProcessed ,
66
+ requestsConcurrent ,
67
+ websocketsConcurrent ,
68
+ requestsDist ,
69
+ )
72
70
73
- var (
74
- dist * prometheus.HistogramVec
75
- distOpts []string
76
- )
77
- // We want to count websockets separately.
78
- if isWebsocketUpgrade (r ) {
79
- websocketsConcurrent .Inc ()
80
- defer websocketsConcurrent .Dec ()
71
+ return func (next http.Handler ) http.Handler {
72
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
73
+ var (
74
+ start = time .Now ()
75
+ method = r .Method
76
+ rctx = chi .RouteContext (r .Context ())
77
+ )
78
+ sw , ok := w .(chimw.WrapResponseWriter )
79
+ if ! ok {
80
+ panic ("dev error: http.ResponseWriter is not chimw.WrapResponseWriter" )
81
+ }
81
82
82
- dist = websocketsDist
83
- } else {
84
- requestsConcurrent .Inc ()
85
- defer requestsConcurrent .Dec ()
83
+ var (
84
+ dist * prometheus.HistogramVec
85
+ distOpts []string
86
+ )
87
+ // We want to count websockets separately.
88
+ if isWebsocketUpgrade (r ) {
89
+ websocketsConcurrent .Inc ()
90
+ defer websocketsConcurrent .Dec ()
86
91
87
- dist = requestsDist
88
- distOpts = []string {method }
89
- }
92
+ dist = websocketsDist
93
+ } else {
94
+ requestsConcurrent .Inc ()
95
+ defer requestsConcurrent .Dec ()
90
96
91
- next .ServeHTTP (w , r )
97
+ dist = requestsDist
98
+ distOpts = []string {method }
99
+ }
92
100
93
- path := rctx .RoutePattern ()
94
- distOpts = append (distOpts , path )
95
- statusStr := strconv .Itoa (sw .Status ())
101
+ next .ServeHTTP (w , r )
96
102
97
- requestsProcessed .WithLabelValues (statusStr , method , path ).Inc ()
98
- dist .WithLabelValues (distOpts ... ).Observe (float64 (time .Since (start )) / 1e6 )
99
- })
103
+ path := rctx .RoutePattern ()
104
+ distOpts = append (distOpts , path )
105
+ statusStr := strconv .Itoa (sw .Status ())
106
+
107
+ requestsProcessed .WithLabelValues (statusStr , method , path ).Inc ()
108
+ dist .WithLabelValues (distOpts ... ).Observe (float64 (time .Since (start )) / 1e6 )
109
+ })
110
+ }
100
111
}
101
112
102
113
func isWebsocketUpgrade (r * http.Request ) bool {
0 commit comments