From 3182cb0dbcfa8eca0ceb243db53c43eed14c1af8 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Sat, 5 Nov 2022 18:35:11 +0000 Subject: [PATCH] fix!: update to metrics v4 (#398) Updates to latest metrics interface. BREAKING CHANGE: requires @libp2p/interface-metrics v4 --- package.json | 4 +-- src/query/manager.ts | 34 +++++++++++++----------- src/routing-table/index.ts | 54 ++++++++++++++------------------------ 3 files changed, 40 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 5b60460d..3ee135fc 100644 --- a/package.json +++ b/package.json @@ -142,7 +142,7 @@ "@libp2p/interface-connection": "^3.0.2", "@libp2p/interface-connection-manager": "^1.1.1", "@libp2p/interface-dht": "^1.0.1", - "@libp2p/interface-metrics": "^3.0.0", + "@libp2p/interface-metrics": "^4.0.0", "@libp2p/interface-peer-discovery": "^1.0.1", "@libp2p/interface-peer-id": "^1.0.4", "@libp2p/interface-peer-info": "^1.0.3", @@ -205,7 +205,7 @@ "protons": "^6.0.0", "sinon": "^14.0.0", "ts-sinon": "^2.0.2", - "which": "^2.0.2" + "which": "^3.0.0" }, "browser": { "./dist/src/routing-table/generated-prefix-list.js": "./dist/src/routing-table/generated-prefix-list-browser.js" diff --git a/src/query/manager.ts b/src/query/manager.ts index 7ddb3741..a09ecbd6 100644 --- a/src/query/manager.ts +++ b/src/query/manager.ts @@ -14,9 +14,7 @@ import type { Startable } from '@libp2p/interfaces/startable' import type { QueryFunc } from './types.js' import type { QueryOptions } from '@libp2p/interface-dht' import { PeerSet } from '@libp2p/peer-collections' -import type { Metrics } from '@libp2p/interface-metrics' - -const METRIC_RUNNING_QUERIES = 'running-queries' +import type { Metric, Metrics } from '@libp2p/interface-metrics' export interface CleanUpEvents { 'cleanup': CustomEvent @@ -44,6 +42,10 @@ export class QueryManager implements Startable { private readonly controllers: Set private running: boolean private queries: number + private metrics?: { + runningQueries: Metric + queryTime: Metric + } constructor (components: QueryManagerComponents, init: QueryManagerInit) { const { lan = false, disjointPaths = K, alpha = ALPHA } = init @@ -66,6 +68,13 @@ export class QueryManager implements Startable { */ async start () { this.running = true + + if (this.components.metrics != null && this.metrics == null) { + this.metrics = { + runningQueries: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_running_queries`), + queryTime: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_query_time_seconds`) + } + } } /** @@ -86,6 +95,7 @@ export class QueryManager implements Startable { throw new Error('QueryManager not started') } + const stopQueryTimer = this.metrics?.queryTime.timer() let timeoutController if (options.signal == null) { @@ -131,12 +141,7 @@ export class QueryManager implements Startable { try { log('query:start') this.queries++ - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_RUNNING_QUERIES, - value: this.queries - }) + this.metrics?.runningQueries.update(this.queries) if (peers.length === 0) { log.error('Running query with no peers') @@ -186,12 +191,11 @@ export class QueryManager implements Startable { } this.queries-- - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_RUNNING_QUERIES, - value: this.queries - }) + this.metrics?.runningQueries.update(this.queries) + + if (stopQueryTimer != null) { + stopQueryTimer() + } cleanUp.dispatchEvent(new CustomEvent('cleanup')) log('query:done in %dms', Date.now() - startTime) diff --git a/src/routing-table/index.ts b/src/routing-table/index.ts index f3911591..47a9b574 100644 --- a/src/routing-table/index.ts +++ b/src/routing-table/index.ts @@ -8,7 +8,7 @@ import type { PeerId } from '@libp2p/interface-peer-id' import type { Startable } from '@libp2p/interfaces/startable' import type { Logger } from '@libp2p/logger' import { PeerSet } from '@libp2p/peer-collections' -import type { Metrics } from '@libp2p/interface-metrics' +import type { Metric, Metrics } from '@libp2p/interface-metrics' import type { PeerStore } from '@libp2p/interface-peer-store' import type { ConnectionManager } from '@libp2p/interface-connection-manager' @@ -54,10 +54,6 @@ export interface KBucketTree { toIterable: () => Iterable } -const METRIC_ROUTING_TABLE_SIZE = 'routing-table-size' -const METRIC_PING_QUEUE_SIZE = 'ping-queue-size' -const METRIC_PING_RUNNING = 'ping-running' - export interface RoutingTableInit { lan: boolean protocol: string @@ -93,6 +89,11 @@ export class RoutingTable implements Startable { private readonly protocol: string private readonly tagName: string private readonly tagValue: number + private metrics?: { + routingTableSize: Metric + pingQueueSize: Metric + pingRunning: Metric + } constructor (components: RoutingTableComponents, init: RoutingTableInit) { const { kBucketSize, pingTimeout, lan, pingConcurrency, protocol, tagName, tagValue } = init @@ -109,18 +110,8 @@ export class RoutingTable implements Startable { this.tagValue = tagValue ?? KAD_CLOSE_TAG_VALUE const updatePingQueueSizeMetric = () => { - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_PING_QUEUE_SIZE, - value: this.pingQueue.size - }) - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_PING_RUNNING, - value: this.pingQueue.pending - }) + this.metrics?.pingQueueSize.update(this.pingQueue.size) + this.metrics?.pingRunning.update(this.pingQueue.pending) } this.pingQueue = new Queue({ concurrency: this.pingConcurrency }) @@ -137,6 +128,14 @@ export class RoutingTable implements Startable { async start () { this.running = true + if (this.components.metrics != null) { + this.metrics = { + routingTableSize: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_routing_table_size`), + pingQueueSize: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_ping_queue_size`), + pingRunning: this.components.metrics.registerMetric(`libp2p_kad_dht_${this.lan ? 'lan' : 'wan'}_ping_running`) + } + } + const kBuck: KBucketTree = new KBuck({ localNodeId: await utils.convertPeerId(this.components.peerId), numberOfNodesPerKBucket: this.kBucketSize, @@ -250,12 +249,7 @@ export class RoutingTable implements Startable { timeoutController.clear() } - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_ROUTING_TABLE_SIZE, - value: this.size - }) + this.metrics?.routingTableSize.update(this.size) } }) ) @@ -340,12 +334,7 @@ export class RoutingTable implements Startable { this.log('added %p with kad id %b', peer, id) - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_ROUTING_TABLE_SIZE, - value: this.size - }) + this.metrics?.routingTableSize.update(this.size) } /** @@ -360,11 +349,6 @@ export class RoutingTable implements Startable { this.kb.remove(id) - this.components.metrics?.updateComponentMetric({ - system: 'libp2p', - component: `kad-dht-${this.lan ? 'lan' : 'wan'}`, - metric: METRIC_ROUTING_TABLE_SIZE, - value: this.size - }) + this.metrics?.routingTableSize.update(this.size) } }