forked from ansrivas/fiberprometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
200 lines (180 loc) · 6.14 KB
/
middleware.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Copyright (c) 2021-present Ankur Srivastava and Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package fiberprometheus
import (
"strconv"
"time"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// FiberPrometheus ...
type FiberPrometheus struct {
requestsTotal *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
requestInFlight *prometheus.GaugeVec
defaultURL string
}
func create(serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus {
constLabels := make(prometheus.Labels)
if serviceName != "" {
constLabels["service"] = serviceName
}
for label, value := range labels {
constLabels[label] = value
}
counter := promauto.NewCounterVec(
prometheus.CounterOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "requests_total"),
Help: "Count all http requests by status code, method and path.",
ConstLabels: constLabels,
},
[]string{"status_code", "method", "path"},
)
histogram := promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "request_duration_seconds"),
Help: "Duration of all HTTP requests by status code, method and path.",
ConstLabels: constLabels,
Buckets: []float64{
0.000000001, // 1ns
0.000000002,
0.000000005,
0.00000001, // 10ns
0.00000002,
0.00000005,
0.0000001, // 100ns
0.0000002,
0.0000005,
0.000001, // 1µs
0.000002,
0.000005,
0.00001, // 10µs
0.00002,
0.00005,
0.0001, // 100µs
0.0002,
0.0005,
0.001, // 1ms
0.002,
0.005,
0.01, // 10ms
0.02,
0.05,
0.1, // 100 ms
0.2,
0.5,
1.0, // 1s
2.0,
5.0,
10.0, // 10s
15.0,
20.0,
30.0,
},
},
[]string{"status_code", "method", "path"},
)
gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "requests_in_progress_total"),
Help: "All the requests in progress",
ConstLabels: constLabels,
}, []string{"method", "path"})
return &FiberPrometheus{
requestsTotal: counter,
requestDuration: histogram,
requestInFlight: gauge,
defaultURL: "/metrics",
}
}
// New creates a new instance of FiberPrometheus middleware
// serviceName is available as a const label
func New(serviceName string) *FiberPrometheus {
return create(serviceName, "http", "", nil)
}
// NewWith creates a new instance of FiberPrometheus middleware but with an ability
// to pass namespace and a custom subsystem
// Here serviceName is created as a constant-label for the metrics
// Namespace, subsystem get prefixed to the metrics.
//
// For e.g. namespace = "my_app", subsystem = "http" then metrics would be
// `my_app_http_requests_total{...,service= "serviceName"}`
func NewWith(serviceName, namespace, subsystem string) *FiberPrometheus {
return create(serviceName, namespace, subsystem, nil)
}
// NewWithLabels creates a new instance of FiberPrometheus middleware but with an ability
// to pass namespace and a custom subsystem
// Here labels are created as a constant-labels for the metrics
// Namespace, subsystem get prefixed to the metrics.
//
// For e.g. namespace = "my_app", subsystem = "http" and labels = map[string]string{"key1": "value1", "key2":"value2"}
// then then metrics would become
// `my_app_http_requests_total{...,key1= "value1", key2= "value2" }``
func NewWithLabels(labels map[string]string, namespace, subsystem string) *FiberPrometheus {
return create("", namespace, subsystem, labels)
}
// RegisterAt will register the prometheus handler at a given URL
func (ps *FiberPrometheus) RegisterAt(app *fiber.App, url string) {
ps.defaultURL = url
app.Get(ps.defaultURL, adaptor.HTTPHandler(promhttp.Handler()))
}
// Unregister will unregister all prometheus collectors
func (ps *FiberPrometheus) Unregister() bool {
for _, collector := range []prometheus.Collector{ps.requestDuration, ps.requestInFlight, ps.requestsTotal} {
if !prometheus.Unregister(collector) {
return false
}
}
return true
}
// Middleware is the actual default middleware implementation
func (ps *FiberPrometheus) Middleware(ctx *fiber.Ctx) error {
start := time.Now()
method := ctx.Route().Method
path := ctx.Route().Path
if path == ps.defaultURL {
return ctx.Next()
}
ps.requestInFlight.WithLabelValues(method, path).Inc()
defer func() {
ps.requestInFlight.WithLabelValues(method, path).Dec()
}()
err := ctx.Next()
var status int
if err != nil {
if e, ok := err.(*fiber.Error); ok {
// Get correct error code from fiber.Error type
status = e.Code
} else {
// fallback to default error code
// https://docs.gofiber.io/guide/error-handling
status = fiber.StatusInternalServerError
}
} else {
status = ctx.Response().StatusCode()
}
statusCode := strconv.Itoa(status)
ps.requestsTotal.WithLabelValues(statusCode, method, path).Inc()
elapsed := float64(time.Since(start).Nanoseconds()) / 1e9
ps.requestDuration.WithLabelValues(statusCode, method, path).Observe(elapsed)
return err
}