@@ -8,7 +8,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
88import type { Startable } from '@libp2p/interfaces/startable'
99import type { Logger } from '@libp2p/logger'
1010import { PeerSet } from '@libp2p/peer-collections'
11- import type { Metrics } from '@libp2p/interface-metrics'
11+ import type { Metric , Metrics } from '@libp2p/interface-metrics'
1212import type { PeerStore } from '@libp2p/interface-peer-store'
1313import type { ConnectionManager } from '@libp2p/interface-connection-manager'
1414
@@ -54,10 +54,6 @@ export interface KBucketTree {
5454 toIterable : ( ) => Iterable < KBucket >
5555}
5656
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-
6157export interface RoutingTableInit {
6258 lan : boolean
6359 protocol : string
@@ -93,6 +89,11 @@ export class RoutingTable implements Startable {
9389 private readonly protocol : string
9490 private readonly tagName : string
9591 private readonly tagValue : number
92+ private metrics ?: {
93+ routingTableSize : Metric
94+ pingQueueSize : Metric
95+ pingRunning : Metric
96+ }
9697
9798 constructor ( components : RoutingTableComponents , init : RoutingTableInit ) {
9899 const { kBucketSize, pingTimeout, lan, pingConcurrency, protocol, tagName, tagValue } = init
@@ -109,18 +110,8 @@ export class RoutingTable implements Startable {
109110 this . tagValue = tagValue ?? KAD_CLOSE_TAG_VALUE
110111
111112 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 )
124115 }
125116
126117 this . pingQueue = new Queue ( { concurrency : this . pingConcurrency } )
@@ -137,6 +128,14 @@ export class RoutingTable implements Startable {
137128 async start ( ) {
138129 this . running = true
139130
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+
140139 const kBuck : KBucketTree = new KBuck ( {
141140 localNodeId : await utils . convertPeerId ( this . components . peerId ) ,
142141 numberOfNodesPerKBucket : this . kBucketSize ,
@@ -250,12 +249,7 @@ export class RoutingTable implements Startable {
250249 timeoutController . clear ( )
251250 }
252251
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 )
259253 }
260254 } )
261255 )
@@ -340,12 +334,7 @@ export class RoutingTable implements Startable {
340334
341335 this . log ( 'added %p with kad id %b' , peer , id )
342336
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 )
349338 }
350339
351340 /**
@@ -360,11 +349,6 @@ export class RoutingTable implements Startable {
360349
361350 this . kb . remove ( id )
362351
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 )
369353 }
370354}
0 commit comments