1
1
import type { CalculatedMetricOptions , Counter , CounterGroup , Metric , MetricGroup , MetricOptions , Metrics } from '@libp2p/interface-metrics'
2
- import { collectDefaultMetrics , DefaultMetricsCollectorConfiguration , register } from 'prom-client'
2
+ import { collectDefaultMetrics , DefaultMetricsCollectorConfiguration , register , Registry } from 'prom-client'
3
3
import type { MultiaddrConnection , Stream , Connection } from '@libp2p/interface-connection'
4
4
import type { Duplex } from 'it-stream-types'
5
5
import each from 'it-foreach'
@@ -12,6 +12,12 @@ import { logger } from '@libp2p/logger'
12
12
const log = logger ( 'libp2p:prometheus-metrics' )
13
13
14
14
export interface PrometheusMetricsInit {
15
+ /**
16
+ * Use a custom registry to register metrics.
17
+ * By default, the global registry is used to register metrics.
18
+ */
19
+ registry ?: Registry
20
+
15
21
/**
16
22
* By default we collect default metrics - CPU, memory etc, to not do
17
23
* this, pass true here
@@ -31,18 +37,25 @@ export interface PrometheusMetricsInit {
31
37
preserveExistingMetrics ?: boolean
32
38
}
33
39
40
+ export interface PrometheusCalculatedMetricOptions < T = number > extends CalculatedMetricOptions < T > {
41
+ registry ?: Registry
42
+ }
43
+
34
44
class PrometheusMetrics implements Metrics {
35
45
private transferStats : Map < string , number >
46
+ private readonly registry ?: Registry
36
47
37
48
constructor ( init ?: Partial < PrometheusMetricsInit > ) {
49
+ this . registry = init ?. registry
50
+
38
51
if ( init ?. preserveExistingMetrics !== true ) {
39
52
log ( 'Clearing existing metrics' )
40
- register . clear ( )
53
+ ; ( this . registry ?? register ) . clear ( )
41
54
}
42
55
43
56
if ( init ?. preserveExistingMetrics !== false ) {
44
57
log ( 'Collecting default metrics' )
45
- collectDefaultMetrics ( init ?. defaultMetrics )
58
+ collectDefaultMetrics ( { ... init ?. defaultMetrics , register : this . registry ?? init ?. defaultMetrics ?. register } )
46
59
}
47
60
48
61
// holds global and per-protocol sent/received stats
@@ -120,60 +133,60 @@ class PrometheusMetrics implements Metrics {
120
133
this . _track ( stream , stream . stat . protocol )
121
134
}
122
135
123
- registerMetric ( name : string , opts : CalculatedMetricOptions ) : void
136
+ registerMetric ( name : string , opts : PrometheusCalculatedMetricOptions ) : void
124
137
registerMetric ( name : string , opts ?: MetricOptions ) : Metric
125
138
registerMetric ( name : string , opts : any = { } ) : any {
126
139
if ( name == null ?? name . trim ( ) === '' ) {
127
140
throw new Error ( 'Metric name is required' )
128
141
}
129
142
130
143
log ( 'Register metric' , name )
131
- const metric = new PrometheusMetric ( name , opts ?? { } )
144
+ const metric = new PrometheusMetric ( name , { registry : this . registry , ... opts } )
132
145
133
146
if ( opts . calculate == null ) {
134
147
return metric
135
148
}
136
149
}
137
150
138
- registerMetricGroup ( name : string , opts : CalculatedMetricOptions < Record < string , number > > ) : void
151
+ registerMetricGroup ( name : string , opts : PrometheusCalculatedMetricOptions < Record < string , number > > ) : void
139
152
registerMetricGroup ( name : string , opts ?: MetricOptions ) : MetricGroup
140
153
registerMetricGroup ( name : string , opts : any = { } ) : any {
141
154
if ( name == null ?? name . trim ( ) === '' ) {
142
155
throw new Error ( 'Metric name is required' )
143
156
}
144
157
145
158
log ( 'Register metric group' , name )
146
- const group = new PrometheusMetricGroup ( name , opts ?? { } )
159
+ const group = new PrometheusMetricGroup ( name , { registry : this . registry , ... opts } )
147
160
148
161
if ( opts . calculate == null ) {
149
162
return group
150
163
}
151
164
}
152
165
153
- registerCounter ( name : string , opts : CalculatedMetricOptions ) : void
166
+ registerCounter ( name : string , opts : PrometheusCalculatedMetricOptions ) : void
154
167
registerCounter ( name : string , opts ?: MetricOptions ) : Counter
155
168
registerCounter ( name : string , opts : any = { } ) : any {
156
169
if ( name == null ?? name . trim ( ) === '' ) {
157
170
throw new Error ( 'Counter name is required' )
158
171
}
159
172
160
173
log ( 'Register counter' , name )
161
- const counter = new PrometheusCounter ( name , opts )
174
+ const counter = new PrometheusCounter ( name , { registry : this . registry , ... opts } )
162
175
163
176
if ( opts . calculate == null ) {
164
177
return counter
165
178
}
166
179
}
167
180
168
- registerCounterGroup ( name : string , opts : CalculatedMetricOptions < Record < string , number > > ) : void
181
+ registerCounterGroup ( name : string , opts : PrometheusCalculatedMetricOptions < Record < string , number > > ) : void
169
182
registerCounterGroup ( name : string , opts ?: MetricOptions ) : CounterGroup
170
183
registerCounterGroup ( name : string , opts : any = { } ) : any {
171
184
if ( name == null ?? name . trim ( ) === '' ) {
172
185
throw new Error ( 'Metric name is required' )
173
186
}
174
187
175
188
log ( 'Register counter group' , name )
176
- const group = new PrometheusCounterGroup ( name , opts )
189
+ const group = new PrometheusCounterGroup ( name , { registry : this . registry , ... opts } )
177
190
178
191
if ( opts . calculate == null ) {
179
192
return group
0 commit comments