Skip to content

Commit 7f89024

Browse files
authored
feat: option to pass custom prometheus registry (#136)
1 parent e98b629 commit 7f89024

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

middleware.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type FiberPrometheus struct {
4040
defaultURL string
4141
}
4242

43-
func create(serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus {
43+
func create(registry prometheus.Registerer, serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus {
4444
constLabels := make(prometheus.Labels)
4545
if serviceName != "" {
4646
constLabels["service"] = serviceName
@@ -49,15 +49,15 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
4949
constLabels[label] = value
5050
}
5151

52-
counter := promauto.NewCounterVec(
52+
counter := promauto.With(registry).NewCounterVec(
5353
prometheus.CounterOpts{
5454
Name: prometheus.BuildFQName(namespace, subsystem, "requests_total"),
5555
Help: "Count all http requests by status code, method and path.",
5656
ConstLabels: constLabels,
5757
},
5858
[]string{"status_code", "method", "path"},
5959
)
60-
histogram := promauto.NewHistogramVec(prometheus.HistogramOpts{
60+
histogram := promauto.With(registry).NewHistogramVec(prometheus.HistogramOpts{
6161
Name: prometheus.BuildFQName(namespace, subsystem, "request_duration_seconds"),
6262
Help: "Duration of all HTTP requests by status code, method and path.",
6363
ConstLabels: constLabels,
@@ -101,7 +101,7 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
101101
[]string{"status_code", "method", "path"},
102102
)
103103

104-
gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
104+
gauge := promauto.With(registry).NewGaugeVec(prometheus.GaugeOpts{
105105
Name: prometheus.BuildFQName(namespace, subsystem, "requests_in_progress_total"),
106106
Help: "All the requests in progress",
107107
ConstLabels: constLabels,
@@ -118,7 +118,7 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
118118
// New creates a new instance of FiberPrometheus middleware
119119
// serviceName is available as a const label
120120
func New(serviceName string) *FiberPrometheus {
121-
return create(serviceName, "http", "", nil)
121+
return create(prometheus.DefaultRegisterer, serviceName, "http", "", nil)
122122
}
123123

124124
// NewWith creates a new instance of FiberPrometheus middleware but with an ability
@@ -129,7 +129,7 @@ func New(serviceName string) *FiberPrometheus {
129129
// For e.g. namespace = "my_app", subsystem = "http" then metrics would be
130130
// `my_app_http_requests_total{...,service= "serviceName"}`
131131
func NewWith(serviceName, namespace, subsystem string) *FiberPrometheus {
132-
return create(serviceName, namespace, subsystem, nil)
132+
return create(prometheus.DefaultRegisterer, serviceName, namespace, subsystem, nil)
133133
}
134134

135135
// NewWithLabels creates a new instance of FiberPrometheus middleware but with an ability
@@ -141,7 +141,19 @@ func NewWith(serviceName, namespace, subsystem string) *FiberPrometheus {
141141
// then then metrics would become
142142
// `my_app_http_requests_total{...,key1= "value1", key2= "value2" }``
143143
func NewWithLabels(labels map[string]string, namespace, subsystem string) *FiberPrometheus {
144-
return create("", namespace, subsystem, labels)
144+
return create(prometheus.DefaultRegisterer, "", namespace, subsystem, labels)
145+
}
146+
147+
// NewWithRegistry creates a new instance of FiberPrometheus middleware but with an ability
148+
// to pass a custom registry, serviceName, namespace, subsystem and labels
149+
// Here labels are created as a constant-labels for the metrics
150+
// Namespace, subsystem get prefixed to the metrics.
151+
//
152+
// For e.g. namespace = "my_app", subsystem = "http" and labels = map[string]string{"key1": "value1", "key2":"value2"}
153+
// then then metrics would become
154+
// `my_app_http_requests_total{...,key1= "value1", key2= "value2" }``
155+
func NewWithRegistry(registry prometheus.Registerer, serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus {
156+
return create(registry, serviceName, namespace, subsystem, labels)
145157
}
146158

147159
// RegisterAt will register the prometheus handler at a given URL

middleware_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929

3030
"github.com/gofiber/fiber/v2"
3131
"github.com/gofiber/fiber/v2/middleware/basicauth"
32+
"github.com/prometheus/client_golang/prometheus"
33+
"github.com/prometheus/client_golang/prometheus/promhttp"
3234
)
3335

3436
func TestMiddleware(t *testing.T) {
@@ -211,3 +213,55 @@ func TestMiddlewareWithBasicAuth(t *testing.T) {
211213
t.Fail()
212214
}
213215
}
216+
217+
func TestMiddlewareWithCustomRegistry(t *testing.T) {
218+
app := fiber.New()
219+
registry := prometheus.NewRegistry()
220+
221+
srv := httptest.NewServer(promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
222+
t.Cleanup(srv.Close)
223+
224+
promfiber := NewWithRegistry(registry, "unique-service", "my_service_with_name", "http", nil)
225+
app.Use(promfiber.Middleware)
226+
227+
app.Get("/", func(c *fiber.Ctx) error {
228+
return c.SendString("Hello World")
229+
})
230+
req := httptest.NewRequest("GET", "/", nil)
231+
resp, err := app.Test(req, -1)
232+
if err != nil {
233+
t.Fail()
234+
}
235+
if resp.StatusCode != 200 {
236+
t.Fail()
237+
}
238+
239+
resp, err = srv.Client().Get(srv.URL)
240+
if err != nil {
241+
t.Fail()
242+
}
243+
if resp == nil {
244+
t.Fatal("response is nil")
245+
}
246+
if resp.StatusCode != 200 {
247+
t.Fail()
248+
}
249+
defer resp.Body.Close()
250+
251+
body, _ := ioutil.ReadAll(resp.Body)
252+
got := string(body)
253+
want := `my_service_with_name_http_requests_total{method="GET",path="/",service="unique-service",status_code="200"} 1`
254+
if !strings.Contains(got, want) {
255+
t.Errorf("got %s; want %s", got, want)
256+
}
257+
258+
want = `my_service_with_name_http_request_duration_seconds_count{method="GET",path="/",service="unique-service",status_code="200"} 1`
259+
if !strings.Contains(got, want) {
260+
t.Errorf("got %s; want %s", got, want)
261+
}
262+
263+
want = `my_service_with_name_http_requests_in_progress_total{method="GET",service="unique-service"} 0`
264+
if !strings.Contains(got, want) {
265+
t.Errorf("got %s; want %s", got, want)
266+
}
267+
}

0 commit comments

Comments
 (0)