@@ -11,7 +11,7 @@ import type { PeerInfoMapper, Validators } from '../index.js'
11
11
import type { PeerRouting } from '../peer-routing'
12
12
import type { Providers } from '../providers'
13
13
import type { RoutingTable } from '../routing-table'
14
- import type { Logger , PeerId } from '@libp2p/interface'
14
+ import type { CounterGroup , Logger , Metrics , PeerId } from '@libp2p/interface'
15
15
import type { IncomingStreamData } from '@libp2p/interface-internal'
16
16
17
17
export interface DHTMessageHandler {
@@ -28,16 +28,18 @@ export interface RPCInit {
28
28
}
29
29
30
30
export interface RPCComponents extends GetValueHandlerComponents , PutValueHandlerComponents , FindNodeHandlerComponents , GetProvidersHandlerComponents {
31
-
31
+ metrics ?: Metrics
32
32
}
33
33
34
34
export class RPC {
35
35
private readonly handlers : Record < string , DHTMessageHandler >
36
36
private readonly routingTable : RoutingTable
37
37
private readonly log : Logger
38
+ private readonly metrics ?: CounterGroup
38
39
39
40
constructor ( components : RPCComponents , init : RPCInit ) {
40
41
const { providers, peerRouting, validators, logPrefix, peerInfoMapper } = init
42
+ this . metrics = components . metrics ?. registerCounterGroup ( `${ logPrefix . replaceAll ( ':' , '_' ) } _inbound_rpc_requests` )
41
43
42
44
this . log = components . logger . forComponent ( `${ logPrefix } :rpc` )
43
45
this . routingTable = init . routingTable
@@ -69,7 +71,19 @@ export class RPC {
69
71
return
70
72
}
71
73
72
- return handler . handle ( peerId , msg )
74
+ try {
75
+ const value = await handler . handle ( peerId , msg )
76
+
77
+ this . metrics ?. increment ( {
78
+ [ `${ msg . type } _SUCCESS` ] : true
79
+ } )
80
+
81
+ return value
82
+ } catch {
83
+ this . metrics ?. increment ( {
84
+ [ `${ msg . type } _ERROR` ] : true
85
+ } )
86
+ }
73
87
}
74
88
75
89
/**
0 commit comments