/
prometheus.go
150 lines (127 loc) · 4.36 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
140
141
142
143
144
145
146
147
148
149
150
package httpbp
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/reddit/baseplate.go/internal/prometheusbpint"
"github.com/reddit/baseplate.go/prometheusbp"
)
const (
methodLabel = "http_method"
successLabel = "http_success"
codeLabel = "http_response_code"
clientNameLabel = "http_client_name"
endpointLabel = "http_endpoint"
)
var payloadSizeBuckets = []float64{
100, // 100 B
500, // 500 B
1 << 10, // 1 KiB
4 << 10, // 4 KiB
10 << 10, // 10 KiB
50 << 10, // 50 KiB
100 << 10, // 100 KiB
500 << 10, // 500 KiB
1 << 20, // 1 MiB
5 << 20, // 5 MiB
10 << 20, // 10 MiB
50 << 20, // 50 MiB
100 << 20, // 100 MiB
}
var (
serverLabels = []string{
methodLabel,
successLabel,
endpointLabel,
}
serverLatency = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_server_latency_seconds",
Help: "HTTP server request latencies",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, serverLabels)
serverRequestSize = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_server_request_size_bytes",
Help: "Request size",
Buckets: payloadSizeBuckets,
}, serverLabels)
serverResponseSize = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_server_response_size_bytes",
Help: "Response size",
Buckets: payloadSizeBuckets,
}, serverLabels)
serverTimeToWriteHeader = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_server_time_to_write_header_seconds",
Help: "Request size",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, serverLabels)
serverTimeToFirstByte = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_server_time_to_first_byte_seconds",
Help: "Time elapsed before first byte was sent",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, serverLabels)
serverTotalRequestLabels = []string{
methodLabel,
successLabel,
codeLabel,
endpointLabel,
}
serverTotalRequests = promauto.With(prometheusbpint.GlobalRegistry).NewCounterVec(prometheus.CounterOpts{
Name: "http_server_requests_total",
Help: "Total request count",
}, serverTotalRequestLabels)
serverActiveRequestsLabels = []string{
methodLabel,
endpointLabel,
}
serverActiveRequests = promauto.With(prometheusbpint.GlobalRegistry).NewGaugeVec(prometheus.GaugeOpts{
Name: "http_server_active_requests",
Help: "The number of in-flight requests being handled by the service",
}, serverActiveRequestsLabels)
)
var (
clientLatencyLabels = []string{
methodLabel,
endpointLabel,
successLabel,
clientNameLabel,
}
clientLatencyDistribution = promauto.With(prometheusbpint.GlobalRegistry).NewHistogramVec(prometheus.HistogramOpts{
Name: "http_client_latency_seconds",
Help: "HTTP client request latencies",
Buckets: prometheusbp.DefaultLatencyBuckets,
}, clientLatencyLabels)
clientTotalRequestLabels = []string{
methodLabel,
endpointLabel,
successLabel,
codeLabel,
clientNameLabel,
}
clientTotalRequests = promauto.With(prometheusbpint.GlobalRegistry).NewCounterVec(prometheus.CounterOpts{
Name: "http_client_requests_total",
Help: "Total request count",
}, clientTotalRequestLabels)
clientActiveRequestsLabels = []string{
methodLabel,
endpointLabel,
clientNameLabel,
}
clientActiveRequests = promauto.With(prometheusbpint.GlobalRegistry).NewGaugeVec(prometheus.GaugeOpts{
Name: "http_client_active_requests",
Help: "The number of in-flight requests",
}, clientActiveRequestsLabels)
)
var (
panicRecoverLabels = []string{
methodLabel,
}
panicRecoverCounter = promauto.With(prometheusbpint.GlobalRegistry).NewCounterVec(prometheus.CounterOpts{
Name: "httpbp_server_recovered_panics_total",
Help: "The number of panics recovered from http server handlers",
}, panicRecoverLabels)
)
// PerformanceMonitoringMiddleware returns optional Prometheus historgram metrics for monitoring the following:
// 1. http server time to write header in seconds
// 2. http server time to write header in seconds
func PerformanceMonitoringMiddleware() (timeToWriteHeader, timeToFirstByte *prometheus.HistogramVec) {
return serverTimeToWriteHeader, serverTimeToFirstByte
}