/
metrics.go
169 lines (144 loc) · 4.35 KB
/
metrics.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import (
"net/http"
"strconv"
"time"
"github.com/ethersphere/bee"
m "github.com/ethersphere/bee/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
)
const bytesInKB = 1000
var fileSizeBucketsKBytes = []int64{100, 500, 2500, 4999, 5000, 10000}
type metrics struct {
// all metrics fields must be exported
// to be able to return them by Metrics()
// using reflection
RequestCount prometheus.Counter
ResponseDuration prometheus.Histogram
PingRequestCount prometheus.Counter
ResponseCodeCounts *prometheus.CounterVec
ContentApiDuration prometheus.HistogramVec
}
func newMetrics() metrics {
subsystem := "api"
return metrics{
RequestCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "request_count",
Help: "Number of API requests.",
}),
ResponseDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "response_duration_seconds",
Help: "Histogram of API response durations.",
Buckets: []float64{0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
}),
ResponseCodeCounts: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "response_code_count",
Help: "Response count grouped by status code",
},
[]string{"code", "method"},
),
ContentApiDuration: *prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "content_api_duration",
Help: "Histogram of file upload API response durations.",
Buckets: []float64{0.5, 1, 2.5, 5, 10, 30, 60},
}, []string{"filesize", "method"}),
}
}
func toFileSizeBucket(bytes int64) int64 {
for _, s := range fileSizeBucketsKBytes {
if (s * bytesInKB) >= bytes {
return s * bytesInKB
}
}
return fileSizeBucketsKBytes[len(fileSizeBucketsKBytes)-1] * bytesInKB
}
func (s *Service) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
}
func (s *Service) pageviewMetricsHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.metrics.RequestCount.Inc()
h.ServeHTTP(w, r)
s.metrics.ResponseDuration.Observe(time.Since(start).Seconds())
})
}
func (s *Service) responseCodeMetricsHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wrapper := newResponseWriter(w)
h.ServeHTTP(wrapper, r)
s.metrics.ResponseCodeCounts.WithLabelValues(
strconv.Itoa(wrapper.statusCode),
r.Method,
).Inc()
})
}
// UpgradedResponseWriter adds more functionality on top of ResponseWriter
type UpgradedResponseWriter interface {
http.ResponseWriter
http.Pusher
http.Hijacker
http.Flusher
// staticcheck SA1019 CloseNotifier interface is required by gorilla compress handler
// nolint:staticcheck
http.CloseNotifier
}
type responseWriter struct {
UpgradedResponseWriter
statusCode int
wroteHeader bool
}
func newResponseWriter(w http.ResponseWriter) *responseWriter {
// StatusOK is called by default if nothing else is called
uw := w.(UpgradedResponseWriter)
return &responseWriter{uw, http.StatusOK, false}
}
func (rw *responseWriter) Status() int {
return rw.statusCode
}
func (rw *responseWriter) WriteHeader(code int) {
if rw.wroteHeader {
return
}
rw.statusCode = code
rw.UpgradedResponseWriter.WriteHeader(code)
rw.wroteHeader = true
}
func newDebugMetrics() (r *prometheus.Registry) {
r = prometheus.NewRegistry()
// register standard metrics
r.MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{
Namespace: m.Namespace,
}),
collectors.NewGoCollector(),
prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: m.Namespace,
Name: "info",
Help: "Bee information.",
ConstLabels: prometheus.Labels{
"version": bee.Version,
},
}),
)
return r
}
func (s *Service) MetricsRegistry() *prometheus.Registry {
return s.metricsRegistry
}
func (s *Service) MustRegisterMetrics(cs ...prometheus.Collector) {
s.metricsRegistry.MustRegister(cs...)
}