@libp2p/kad-dht 14.1.6 → 14.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/src/network.ts CHANGED
@@ -57,6 +57,61 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
57
57
  operations: components.metrics?.registerCounterGroup(`${init.metricsPrefix}_outbound_rpc_requests_total`),
58
58
  errors: components.metrics?.registerCounterGroup(`${init.metricsPrefix}_outbound_rpc_errors_total`)
59
59
  }
60
+
61
+ this.sendRequest = components.metrics?.traceFunction('libp2p.kadDHT.sendRequest', this.sendRequest.bind(this), {
62
+ optionsIndex: 2,
63
+ getAttributesFromArgs ([to, message], attrs) {
64
+ return {
65
+ ...attrs,
66
+ to: to.toString(),
67
+ 'message type': `${message.type}`
68
+ }
69
+ },
70
+ getAttributesFromYieldedValue: (event, attrs) => {
71
+ if (event.name === 'PEER_RESPONSE') {
72
+ if (event.providers.length > 0) {
73
+ event.providers.forEach((value, index) => {
74
+ attrs[`providers-${index}`] = value.id.toString()
75
+ })
76
+ }
77
+
78
+ if (event.closer.length > 0) {
79
+ event.closer.forEach((value, index) => {
80
+ attrs[`closer-${index}`] = value.id.toString()
81
+ })
82
+ }
83
+ }
84
+
85
+ return attrs
86
+ }
87
+ }) ?? this.sendRequest
88
+ this.sendMessage = components.metrics?.traceFunction('libp2p.kadDHT.sendMessage', this.sendMessage.bind(this), {
89
+ optionsIndex: 2,
90
+ getAttributesFromArgs ([to, message], attrs) {
91
+ return {
92
+ ...attrs,
93
+ to: to.toString(),
94
+ 'message type': `${message.type}`
95
+ }
96
+ },
97
+ getAttributesFromYieldedValue: (event, attrs) => {
98
+ if (event.name === 'PEER_RESPONSE') {
99
+ if (event.providers.length > 0) {
100
+ event.providers.forEach((value, index) => {
101
+ attrs[`providers-${index}`] = value.id.toString()
102
+ })
103
+ }
104
+
105
+ if (event.closer.length > 0) {
106
+ event.closer.forEach((value, index) => {
107
+ attrs[`closer-${index}`] = value.id.toString()
108
+ })
109
+ }
110
+ }
111
+
112
+ return attrs
113
+ }
114
+ }) ?? this.sendMessage
60
115
  }
61
116
 
62
117
  /**
@@ -22,12 +22,13 @@ import type { Network } from '../network.js'
22
22
  import type { QueryManager, QueryOptions } from '../query/manager.js'
23
23
  import type { QueryFunc } from '../query/types.js'
24
24
  import type { RoutingTable } from '../routing-table/index.js'
25
- import type { ComponentLogger, Logger, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
25
+ import type { ComponentLogger, Logger, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
26
26
 
27
27
  export interface PeerRoutingComponents {
28
28
  peerId: PeerId
29
29
  peerStore: PeerStore
30
30
  logger: ComponentLogger
31
+ metrics?: Metrics
31
32
  }
32
33
 
33
34
  export interface PeerRoutingInit {
@@ -55,6 +56,13 @@ export class PeerRouting {
55
56
  this.peerStore = components.peerStore
56
57
  this.peerId = components.peerId
57
58
  this.log = components.logger.forComponent(`${init.logPrefix}:peer-routing`)
59
+
60
+ this.findPeer = components.metrics?.traceFunction('libp2p.kadDHT.findPeer', this.findPeer.bind(this), {
61
+ optionsIndex: 1
62
+ }) ?? this.findPeer
63
+ this.getClosestPeers = components.metrics?.traceFunction('libp2p.kadDHT.getClosestPeers', this.getClosestPeers.bind(this), {
64
+ optionsIndex: 1
65
+ }) ?? this.getClosestPeers
58
66
  }
59
67
 
60
68
  /**
@@ -171,6 +171,7 @@ export class QueryManager implements Startable {
171
171
  // Create query paths from the starting peers
172
172
  const paths = peersToQuery.map((peer, index) => {
173
173
  return queryPath({
174
+ ...options,
174
175
  key,
175
176
  startingPeer: peer,
176
177
  ourPeerId: this.peerId,
@@ -121,6 +121,7 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
121
121
 
122
122
  try {
123
123
  for await (const event of query({
124
+ ...options,
124
125
  key,
125
126
  peer,
126
127
  signal: compoundSignal,
package/src/query-self.ts CHANGED
@@ -10,9 +10,8 @@ import { timeOperationMethod } from './utils.js'
10
10
  import type { OperationMetrics } from './kad-dht.js'
11
11
  import type { PeerRouting } from './peer-routing/index.js'
12
12
  import type { RoutingTable } from './routing-table/index.js'
13
- import type { ComponentLogger, Logger, PeerId, Startable } from '@libp2p/interface'
13
+ import type { ComponentLogger, Logger, Metrics, PeerId, Startable } from '@libp2p/interface'
14
14
  import type { DeferredPromise } from 'p-defer'
15
-
16
15
  export interface QuerySelfInit {
17
16
  logPrefix: string
18
17
  peerRouting: PeerRouting
@@ -28,6 +27,7 @@ export interface QuerySelfInit {
28
27
  export interface QuerySelfComponents {
29
28
  peerId: PeerId
30
29
  logger: ComponentLogger
30
+ metrics?: Metrics
31
31
  }
32
32
 
33
33
  /**