forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
169 lines (144 loc) · 4.78 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package prometheus
import (
"bufio"
"encoding/json"
"net"
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/meta-quick/opax/metrics"
)
// Provider wraps a metrics.Metrics provider with a Prometheus registry that can
// instrument the HTTP server's handlers.
type Provider struct {
registry *prometheus.Registry
durationHistogram *prometheus.HistogramVec
cancellationCounters *prometheus.CounterVec
inner metrics.Metrics
logger func(attrs map[string]interface{}, f string, a ...interface{})
}
// New returns a new Provider object.
func New(inner metrics.Metrics, logger func(attrs map[string]interface{}, f string, a ...interface{})) *Provider {
registry := prometheus.NewRegistry()
registry.MustRegister(collectors.NewGoCollector())
durationHistogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "A histogram of duration for requests.",
Buckets: []float64{
1e-6, // 1 microsecond
5e-6,
1e-5,
5e-5,
1e-4,
5e-4,
1e-3, // 1 millisecond
0.01,
0.1,
1, // 1 second
},
},
[]string{"code", "handler", "method"},
)
registry.MustRegister(durationHistogram)
cancellationCounters := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_request_cancellations",
Help: "A count of cancelled requests.",
},
[]string{"code", "handler", "method"},
)
registry.MustRegister(cancellationCounters)
return &Provider{
registry: registry,
durationHistogram: durationHistogram,
cancellationCounters: cancellationCounters,
inner: inner,
logger: logger,
}
}
// RegisterEndpoints registers `/metrics` endpoint
func (p *Provider) RegisterEndpoints(registrar func(path, method string, handler http.Handler)) {
registrar("/metrics", http.MethodGet, promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{}))
}
// InstrumentHandler returned wrapped HTTP handler with added prometheus instrumentation
func (p *Provider) InstrumentHandler(handler http.Handler, label string) http.Handler {
durationCollector := p.durationHistogram.MustCurryWith(prometheus.Labels{"handler": label})
cancellationsCollector := p.cancellationCounters.MustCurryWith(prometheus.Labels{"handler": label})
return promhttp.InstrumentHandlerDuration(durationCollector, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csrw := &captureStatusResponseWriter{ResponseWriter: w, status: http.StatusOK}
var rw http.ResponseWriter
if h, ok := w.(http.Hijacker); ok {
rw = &hijacker{ResponseWriter: csrw, hijacker: h}
} else {
rw = csrw
}
handler.ServeHTTP(rw, r)
if r.Context().Err() != nil {
cancellationsCollector.With(prometheus.Labels{"code": strconv.Itoa(csrw.status), "method": r.Method}).Inc()
}
}))
}
// Info returns attributes that describe the metric provider.
func (p *Provider) Info() metrics.Info {
return metrics.Info{
Name: "prometheus",
}
}
// All returns the union of the inner metric provider and the underlying
// prometheus registry.
func (p *Provider) All() map[string]interface{} {
all := p.inner.All()
families, err := p.registry.Gather()
if err != nil && p.logger != nil {
p.logger(map[string]interface{}{
"err": err,
}, "Failed to gather metrics from Prometheus registry.")
}
for _, f := range families {
all[f.GetName()] = f
}
return all
}
// MarshalJSON returns a JSON representation of the unioned metrics.
func (p *Provider) MarshalJSON() ([]byte, error) {
return json.Marshal(p.All())
}
// Timer returns a named timer.
func (p *Provider) Timer(name string) metrics.Timer {
return p.inner.Timer(name)
}
// Counter returns a named counter.
func (p *Provider) Counter(name string) metrics.Counter {
return p.inner.Counter(name)
}
// Histogram returns a named histogram.
func (p *Provider) Histogram(name string) metrics.Histogram {
return p.inner.Histogram(name)
}
// Clear resets the inner metric provider. The Prometheus registry does not
// expose an interface to clear the metrics so this call has no affect on
// metrics tracked by Prometheus.
func (p *Provider) Clear() {
p.inner.Clear()
}
type captureStatusResponseWriter struct {
http.ResponseWriter
status int
}
type hijacker struct {
http.ResponseWriter
hijacker http.Hijacker
}
func (h *hijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return h.hijacker.Hijack()
}
func (c *captureStatusResponseWriter) WriteHeader(statusCode int) {
c.ResponseWriter.WriteHeader(statusCode)
c.status = statusCode
}