Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 3182cb0

Browse files
authored
fix!: update to metrics v4 (#398)
Updates to latest metrics interface. BREAKING CHANGE: requires @libp2p/interface-metrics v4
1 parent ee333ca commit 3182cb0

File tree

3 files changed

+40
-52
lines changed

3 files changed

+40
-52
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"@libp2p/interface-connection": "^3.0.2",
143143
"@libp2p/interface-connection-manager": "^1.1.1",
144144
"@libp2p/interface-dht": "^1.0.1",
145-
"@libp2p/interface-metrics": "^3.0.0",
145+
"@libp2p/interface-metrics": "^4.0.0",
146146
"@libp2p/interface-peer-discovery": "^1.0.1",
147147
"@libp2p/interface-peer-id": "^1.0.4",
148148
"@libp2p/interface-peer-info": "^1.0.3",
@@ -205,7 +205,7 @@
205205
"protons": "^6.0.0",
206206
"sinon": "^14.0.0",
207207
"ts-sinon": "^2.0.2",
208-
"which": "^2.0.2"
208+
"which": "^3.0.0"
209209
},
210210
"browser": {
211211
"./dist/src/routing-table/generated-prefix-list.js": "./dist/src/routing-table/generated-prefix-list-browser.js"

src/query/manager.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import type { Startable } from '@libp2p/interfaces/startable'
1414
import type { QueryFunc } from './types.js'
1515
import type { QueryOptions } from '@libp2p/interface-dht'
1616
import { PeerSet } from '@libp2p/peer-collections'
17-
import type { Metrics } from '@libp2p/interface-metrics'
18-
19-
const METRIC_RUNNING_QUERIES = 'running-queries'
17+
import type { Metric, Metrics } from '@libp2p/interface-metrics'
2018

2119
export interface CleanUpEvents {
2220
'cleanup': CustomEvent
@@ -44,6 +42,10 @@ export class QueryManager implements Startable {
4442
private readonly controllers: Set<AbortController>
4543
private running: boolean
4644
private queries: number
45+
private metrics?: {
46+
runningQueries: Metric
47+
queryTime: Metric
48+
}
4749

4850
constructor (components: QueryManagerComponents, init: QueryManagerInit) {
4951
const { lan = false, disjointPaths = K, alpha = ALPHA } = init
@@ -66,6 +68,13 @@ export class QueryManager implements Startable {
6668
*/
6769
async start () {
6870
this.running = true
71+
72+
if (this.components.metrics != null && this.metrics == null) {
73+
this.metrics = {
74+
runningQueries: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_running_queries`),
75+
queryTime: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_query_time_seconds`)
76+
}
77+
}
6978
}
7079

7180
/**
@@ -86,6 +95,7 @@ export class QueryManager implements Startable {
8695
throw new Error('QueryManager not started')
8796
}
8897

98+
const stopQueryTimer = this.metrics?.queryTime.timer()
8999
let timeoutController
90100

91101
if (options.signal == null) {
@@ -131,12 +141,7 @@ export class QueryManager implements Startable {
131141
try {
132142
log('query:start')
133143
this.queries++
134-
this.components.metrics?.updateComponentMetric({
135-
system: 'libp2p',
136-
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
137-
metric: METRIC_RUNNING_QUERIES,
138-
value: this.queries
139-
})
144+
this.metrics?.runningQueries.update(this.queries)
140145

141146
if (peers.length === 0) {
142147
log.error('Running query with no peers')
@@ -186,12 +191,11 @@ export class QueryManager implements Startable {
186191
}
187192

188193
this.queries--
189-
this.components.metrics?.updateComponentMetric({
190-
system: 'libp2p',
191-
component: `kad-dht-${this.lan ? 'lan' : 'wan'}`,
192-
metric: METRIC_RUNNING_QUERIES,
193-
value: this.queries
194-
})
194+
this.metrics?.runningQueries.update(this.queries)
195+
196+
if (stopQueryTimer != null) {
197+
stopQueryTimer()
198+
}
195199

196200
cleanUp.dispatchEvent(new CustomEvent('cleanup'))
197201
log('query:done in %dms', Date.now() - startTime)

src/routing-table/index.ts

+19-35
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
88
import type { Startable } from '@libp2p/interfaces/startable'
99
import type { Logger } from '@libp2p/logger'
1010
import { PeerSet } from '@libp2p/peer-collections'
11-
import type { Metrics } from '@libp2p/interface-metrics'
11+
import type { Metric, Metrics } from '@libp2p/interface-metrics'
1212
import type { PeerStore } from '@libp2p/interface-peer-store'
1313
import 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-
6157
export 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

Comments
 (0)