@@ -40,7 +40,7 @@ type FiberPrometheus struct {
40
40
defaultURL string
41
41
}
42
42
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 {
44
44
constLabels := make (prometheus.Labels )
45
45
if serviceName != "" {
46
46
constLabels ["service" ] = serviceName
@@ -49,15 +49,15 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
49
49
constLabels [label ] = value
50
50
}
51
51
52
- counter := promauto .NewCounterVec (
52
+ counter := promauto .With ( registry ). NewCounterVec (
53
53
prometheus.CounterOpts {
54
54
Name : prometheus .BuildFQName (namespace , subsystem , "requests_total" ),
55
55
Help : "Count all http requests by status code, method and path." ,
56
56
ConstLabels : constLabels ,
57
57
},
58
58
[]string {"status_code" , "method" , "path" },
59
59
)
60
- histogram := promauto .NewHistogramVec (prometheus.HistogramOpts {
60
+ histogram := promauto .With ( registry ). NewHistogramVec (prometheus.HistogramOpts {
61
61
Name : prometheus .BuildFQName (namespace , subsystem , "request_duration_seconds" ),
62
62
Help : "Duration of all HTTP requests by status code, method and path." ,
63
63
ConstLabels : constLabels ,
@@ -101,7 +101,7 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
101
101
[]string {"status_code" , "method" , "path" },
102
102
)
103
103
104
- gauge := promauto .NewGaugeVec (prometheus.GaugeOpts {
104
+ gauge := promauto .With ( registry ). NewGaugeVec (prometheus.GaugeOpts {
105
105
Name : prometheus .BuildFQName (namespace , subsystem , "requests_in_progress_total" ),
106
106
Help : "All the requests in progress" ,
107
107
ConstLabels : constLabels ,
@@ -118,7 +118,7 @@ func create(serviceName, namespace, subsystem string, labels map[string]string)
118
118
// New creates a new instance of FiberPrometheus middleware
119
119
// serviceName is available as a const label
120
120
func New (serviceName string ) * FiberPrometheus {
121
- return create (serviceName , "http" , "" , nil )
121
+ return create (prometheus . DefaultRegisterer , serviceName , "http" , "" , nil )
122
122
}
123
123
124
124
// NewWith creates a new instance of FiberPrometheus middleware but with an ability
@@ -129,7 +129,7 @@ func New(serviceName string) *FiberPrometheus {
129
129
// For e.g. namespace = "my_app", subsystem = "http" then metrics would be
130
130
// `my_app_http_requests_total{...,service= "serviceName"}`
131
131
func NewWith (serviceName , namespace , subsystem string ) * FiberPrometheus {
132
- return create (serviceName , namespace , subsystem , nil )
132
+ return create (prometheus . DefaultRegisterer , serviceName , namespace , subsystem , nil )
133
133
}
134
134
135
135
// NewWithLabels creates a new instance of FiberPrometheus middleware but with an ability
@@ -141,7 +141,19 @@ func NewWith(serviceName, namespace, subsystem string) *FiberPrometheus {
141
141
// then then metrics would become
142
142
// `my_app_http_requests_total{...,key1= "value1", key2= "value2" }``
143
143
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 )
145
157
}
146
158
147
159
// RegisterAt will register the prometheus handler at a given URL
0 commit comments