@@ -8,7 +8,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
8
8
import type { Startable } from '@libp2p/interfaces/startable'
9
9
import type { Logger } from '@libp2p/logger'
10
10
import { PeerSet } from '@libp2p/peer-collections'
11
- import type { Metrics } from '@libp2p/interface-metrics'
11
+ import type { Metric , Metrics } from '@libp2p/interface-metrics'
12
12
import type { PeerStore } from '@libp2p/interface-peer-store'
13
13
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
14
14
@@ -54,10 +54,6 @@ export interface KBucketTree {
54
54
toIterable : ( ) => Iterable < KBucket >
55
55
}
56
56
57
- const METRIC_ROUTING_TABLE_SIZE = 'routing-table-size'
58
- const METRIC_PING_QUEUE_SIZE = 'ping-queue-size'
59
- const METRIC_PING_RUNNING = 'ping-running'
60
-
61
57
export interface RoutingTableInit {
62
58
lan : boolean
63
59
protocol : string
@@ -93,6 +89,11 @@ export class RoutingTable implements Startable {
93
89
private readonly protocol : string
94
90
private readonly tagName : string
95
91
private readonly tagValue : number
92
+ private metrics ?: {
93
+ routingTableSize : Metric
94
+ pingQueueSize : Metric
95
+ pingRunning : Metric
96
+ }
96
97
97
98
constructor ( components : RoutingTableComponents , init : RoutingTableInit ) {
98
99
const { kBucketSize, pingTimeout, lan, pingConcurrency, protocol, tagName, tagValue } = init
@@ -109,18 +110,8 @@ export class RoutingTable implements Startable {
109
110
this . tagValue = tagValue ?? KAD_CLOSE_TAG_VALUE
110
111
111
112
const updatePingQueueSizeMetric = ( ) => {
112
- this . components . metrics ?. updateComponentMetric ( {
113
- system : 'libp2p' ,
114
- component : `kad-dht-${ this . lan ? 'lan' : 'wan' } ` ,
115
- metric : METRIC_PING_QUEUE_SIZE ,
116
- value : this . pingQueue . size
117
- } )
118
- this . components . metrics ?. updateComponentMetric ( {
119
- system : 'libp2p' ,
120
- component : `kad-dht-${ this . lan ? 'lan' : 'wan' } ` ,
121
- metric : METRIC_PING_RUNNING ,
122
- value : this . pingQueue . pending
123
- } )
113
+ this . metrics ?. pingQueueSize . update ( this . pingQueue . size )
114
+ this . metrics ?. pingRunning . update ( this . pingQueue . pending )
124
115
}
125
116
126
117
this . pingQueue = new Queue ( { concurrency : this . pingConcurrency } )
@@ -137,6 +128,14 @@ export class RoutingTable implements Startable {
137
128
async start ( ) {
138
129
this . running = true
139
130
131
+ if ( this . components . metrics != null ) {
132
+ this . metrics = {
133
+ routingTableSize : this . components . metrics . registerMetric ( `libp2p_kad_dht_${ this . lan ? 'lan' : 'wan' } _routing_table_size` ) ,
134
+ pingQueueSize : this . components . metrics . registerMetric ( `libp2p_kad_dht_${ this . lan ? 'lan' : 'wan' } _ping_queue_size` ) ,
135
+ pingRunning : this . components . metrics . registerMetric ( `libp2p_kad_dht_${ this . lan ? 'lan' : 'wan' } _ping_running` )
136
+ }
137
+ }
138
+
140
139
const kBuck : KBucketTree = new KBuck ( {
141
140
localNodeId : await utils . convertPeerId ( this . components . peerId ) ,
142
141
numberOfNodesPerKBucket : this . kBucketSize ,
@@ -250,12 +249,7 @@ export class RoutingTable implements Startable {
250
249
timeoutController . clear ( )
251
250
}
252
251
253
- this . components . metrics ?. updateComponentMetric ( {
254
- system : 'libp2p' ,
255
- component : `kad-dht-${ this . lan ? 'lan' : 'wan' } ` ,
256
- metric : METRIC_ROUTING_TABLE_SIZE ,
257
- value : this . size
258
- } )
252
+ this . metrics ?. routingTableSize . update ( this . size )
259
253
}
260
254
} )
261
255
)
@@ -340,12 +334,7 @@ export class RoutingTable implements Startable {
340
334
341
335
this . log ( 'added %p with kad id %b' , peer , id )
342
336
343
- this . components . metrics ?. updateComponentMetric ( {
344
- system : 'libp2p' ,
345
- component : `kad-dht-${ this . lan ? 'lan' : 'wan' } ` ,
346
- metric : METRIC_ROUTING_TABLE_SIZE ,
347
- value : this . size
348
- } )
337
+ this . metrics ?. routingTableSize . update ( this . size )
349
338
}
350
339
351
340
/**
@@ -360,11 +349,6 @@ export class RoutingTable implements Startable {
360
349
361
350
this . kb . remove ( id )
362
351
363
- this . components . metrics ?. updateComponentMetric ( {
364
- system : 'libp2p' ,
365
- component : `kad-dht-${ this . lan ? 'lan' : 'wan' } ` ,
366
- metric : METRIC_ROUTING_TABLE_SIZE ,
367
- value : this . size
368
- } )
352
+ this . metrics ?. routingTableSize . update ( this . size )
369
353
}
370
354
}
0 commit comments