Skip to content

Commit

Permalink
fix!: update to metrics v4 (libp2p#398)
Browse files Browse the repository at this point in the history
Updates to latest metrics interface.

BREAKING CHANGE: requires @libp2p/interface-metrics v4
  • Loading branch information
achingbrain authored Nov 5, 2022
1 parent ee333ca commit 3182cb0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
34 changes: 19 additions & 15 deletions src/query/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,6 +42,10 @@ export class QueryManager implements Startable {
private readonly controllers: Set<AbortController>
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
Expand All @@ -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`)
}
}
}

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 19 additions & 35 deletions src/routing-table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -54,10 +54,6 @@ export interface KBucketTree {
toIterable: () => Iterable<KBucket>
}

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
Expand Down Expand Up @@ -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
Expand All @@ -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 })
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
})
)
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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)
}
}

0 comments on commit 3182cb0

Please sign in to comment.