11import { InvalidParametersError } from '@libp2p/interface'
22import { mergeOptions } from '@libp2p/utils/merge-options'
3+ import { trackedMap } from '@libp2p/utils/tracked-map'
34import * as errorsJs from './errors.js'
4- import type { IdentifyResult , Libp2pEvents , Logger , PeerUpdate , TypedEventTarget , PeerId , PeerStore , Topology , StreamHandler , StreamHandlerRecord , StreamHandlerOptions , AbortOptions } from '@libp2p/interface'
5+ import type { IdentifyResult , Libp2pEvents , Logger , PeerUpdate , TypedEventTarget , PeerId , PeerStore , Topology , StreamHandler , StreamHandlerRecord , StreamHandlerOptions , AbortOptions , Metrics } from '@libp2p/interface'
56import type { Registrar as RegistrarInterface } from '@libp2p/interface-internal'
67import type { ComponentLogger } from '@libp2p/logger'
78
@@ -13,6 +14,7 @@ export interface RegistrarComponents {
1314 peerStore : PeerStore
1415 events : TypedEventTarget < Libp2pEvents >
1516 logger : ComponentLogger
17+ metrics ?: Metrics
1618}
1719
1820/**
@@ -25,10 +27,24 @@ export class Registrar implements RegistrarInterface {
2527 private readonly components : RegistrarComponents
2628
2729 constructor ( components : RegistrarComponents ) {
30+ this . components = components
2831 this . log = components . logger . forComponent ( 'libp2p:registrar' )
2932 this . topologies = new Map ( )
30- this . handlers = new Map ( )
31- this . components = components
33+ components . metrics ?. registerMetricGroup ( 'libp2p_registrar_topologies' , {
34+ calculate : ( ) => {
35+ const output : Record < string , number > = { }
36+
37+ for ( const [ key , value ] of this . topologies ) {
38+ output [ key ] = value . size
39+ }
40+
41+ return output
42+ }
43+ } )
44+ this . handlers = trackedMap ( {
45+ name : 'libp2p_registrar_protocol_handlers' ,
46+ metrics : components . metrics
47+ } )
3248
3349 this . _onDisconnect = this . _onDisconnect . bind ( this )
3450 this . _onPeerUpdate = this . _onPeerUpdate . bind ( this )
0 commit comments