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

Commit db4f7f7

Browse files
authored
feat: log component metrics (#269)
Log metrics so we can get stats in prometheus.
1 parent c761fd1 commit db4f7f7

File tree

7 files changed

+41
-12
lines changed

7 files changed

+41
-12
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@
9292
"it-filter": "^1.0.3",
9393
"it-last": "^1.0.6",
9494
"it-pair": "^1.0.0",
95-
"libp2p": "^0.34.0",
95+
"libp2p": "^0.35.4",
9696
"lodash.random": "^3.2.0",
9797
"lodash.range": "^3.2.0",
9898
"p-retry": "^4.2.0",
99-
"sinon": "^11.1.1",
99+
"sinon": "^12.0.1",
100100
"which": "^2.0.2"
101101
},
102102
"contributors": [

src/kad-dht.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class KadDHT extends EventEmitter {
161161
peerId: libp2p.peerId,
162162
dialer: libp2p,
163163
kBucketSize,
164+
metrics: libp2p.metrics,
164165
lan
165166
})
166167

@@ -205,6 +206,7 @@ class KadDHT extends EventEmitter {
205206
peerId: libp2p.peerId,
206207
// Number of disjoint query paths to use - This is set to `kBucketSize/2` per the S/Kademlia paper
207208
disjointPaths: Math.ceil(kBucketSize / 2),
209+
metrics: libp2p.metrics,
208210
lan
209211
})
210212

src/query/manager.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ const {
1818

1919
/**
2020
* @typedef {import('peer-id')} PeerId
21+
* @typedef {import('../types').Metrics} Metrics
2122
*/
2223

24+
const METRIC_RUNNING_QUERIES = 'running-queries'
25+
2326
/**
2427
* Keeps track of all running queries
2528
*/
@@ -30,16 +33,19 @@ class QueryManager {
3033
* @param {object} params
3134
* @param {PeerId} params.peerId
3235
* @param {boolean} params.lan
36+
* @param {Metrics} [params.metrics]
3337
* @param {number} [params.disjointPaths]
3438
* @param {number} [params.alpha]
3539
*/
36-
constructor ({ peerId, lan, disjointPaths = K, alpha = ALPHA }) {
40+
constructor ({ peerId, lan, metrics, disjointPaths = K, alpha = ALPHA }) {
3741
this._peerId = peerId
3842
this._disjointPaths = disjointPaths || K
3943
this._controllers = new Set()
4044
this._running = false
4145
this._alpha = alpha || ALPHA
4246
this._lan = lan
47+
this._metrics = metrics
48+
this._queries = 0
4349
}
4450

4551
/**
@@ -109,6 +115,8 @@ class QueryManager {
109115

110116
try {
111117
log('query:start')
118+
this._queries++
119+
this._metrics && this._metrics.updateComponentMetric(`kad-dht-${this._lan ? 'lan' : 'wan'}`, METRIC_RUNNING_QUERIES, this._queries)
112120

113121
if (peers.length === 0) {
114122
log.error('Running query with no peers')
@@ -153,6 +161,9 @@ class QueryManager {
153161
timeoutController.clear()
154162
}
155163

164+
this._queries--
165+
this._metrics && this._metrics.updateComponentMetric(`kad-dht-${this._lan ? 'lan' : 'wan'}`, METRIC_RUNNING_QUERIES, this._queries)
166+
156167
cleanUp.emit('cleanup')
157168
log(`query:done in ${Date.now() - (startTime || 0)}ms`)
158169
}

src/query/query-path.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ module.exports.queryPath = async function * queryPath ({ key, startingPeer, ourP
106106
// only continue query if closer peer is actually closer
107107
if (closerPeerXor > peerXor) { // eslint-disable-line max-depth
108108
log('skipping %p as they are not closer to %b than %p', closerPeer.id, key, peer)
109-
// TODO: uncomment this
110-
// continue
109+
continue
111110
}
112111

113112
log('querying closer peer %p', closerPeer.id)

src/routing-table/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ const { TimeoutController } = require('timeout-abort-controller')
1212
* @typedef {import('./types').KBucket} KBucket
1313
* @typedef {import('./types').KBucketTree} KBucketTree
1414
* @typedef {import('peer-id')} PeerId
15+
* @typedef {import('../types').Metrics} Metrics
1516
*/
1617

18+
const METRIC_ROUTING_TABLE_SIZE = 'routing-table-size'
19+
1720
/**
1821
* A wrapper around `k-bucket`, to provide easy store and
1922
* retrieval for peers.
@@ -24,15 +27,18 @@ class RoutingTable {
2427
* @param {import('peer-id')} params.peerId
2528
* @param {import('../types').Dialer} params.dialer
2629
* @param {boolean} params.lan
30+
* @param {Metrics} [params.metrics]
2731
* @param {number} [params.kBucketSize=20]
2832
* @param {number} [params.pingTimeout=10000]
2933
*/
30-
constructor ({ peerId, dialer, kBucketSize, pingTimeout, lan }) {
34+
constructor ({ peerId, dialer, kBucketSize, pingTimeout, lan, metrics }) {
3135
this._log = utils.logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:routing-table`)
3236
this._peerId = peerId
3337
this._dialer = dialer
3438
this._kBucketSize = kBucketSize || 20
3539
this._pingTimeout = pingTimeout || 10000
40+
this._lan = lan
41+
this._metrics = metrics
3642

3743
/** @type {KBucketTree} */
3844
this.kb // eslint-disable-line no-unused-expressions
@@ -111,6 +117,8 @@ class RoutingTable {
111117
if (timeoutController) {
112118
timeoutController.clear()
113119
}
120+
121+
this._metrics && this._metrics.updateComponentMetric(`kad-dht-${this._lan ? 'lan' : 'wan'}`, METRIC_ROUTING_TABLE_SIZE, this.size)
114122
}
115123
})
116124
)
@@ -184,6 +192,8 @@ class RoutingTable {
184192
this.kb.add({ id: id, peer: peer })
185193

186194
this._log('added %p with kad id %b', peer, id)
195+
196+
this._metrics && this._metrics.updateComponentMetric(`kad-dht-${this._lan ? 'lan' : 'wan'}`, METRIC_ROUTING_TABLE_SIZE, this.size)
187197
}
188198

189199
/**
@@ -195,6 +205,8 @@ class RoutingTable {
195205
const id = await utils.convertPeerId(peer)
196206

197207
this.kb.remove(id)
208+
209+
this._metrics && this._metrics.updateComponentMetric(`kad-dht-${this._lan ? 'lan' : 'wan'}`, METRIC_ROUTING_TABLE_SIZE, this.size)
198210
}
199211
}
200212

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,7 @@ export interface AddressBook {
178178
add: (peerId: PeerId, addresses: Multiaddr[]) => void
179179
get: (peerId: PeerId) => Array<{ multiaddr: Multiaddr }> | undefined
180180
}
181+
182+
export interface Metrics {
183+
updateComponentMetric: (component: string, metric: string, value: number) => void
184+
}

test/kad-dht.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,13 @@ describe('KadDHT', () => {
623623
it('findPeer', async function () {
624624
this.timeout(40 * 1000)
625625

626-
const dhts = await tdht.spawn(10)
626+
const dhts = await tdht.spawn(4)
627627

628-
// connect all in a line
629-
for (let i = 0; i < dhts.length - 1; i++) {
630-
await tdht.connect(dhts[i], dhts[i + 1])
631-
}
628+
await Promise.all([
629+
tdht.connect(dhts[0], dhts[1]),
630+
tdht.connect(dhts[1], dhts[2]),
631+
tdht.connect(dhts[2], dhts[3])
632+
])
632633

633634
const ids = dhts.map((d) => d._libp2p.peerId)
634635

@@ -725,7 +726,7 @@ describe('KadDHT', () => {
725726

726727
const res = await all(filter(dhts[1].getClosestPeers(uint8ArrayFromString('foo')), event => event.name === 'FINAL_PEER'))
727728

728-
expect(res).to.have.length(c.K)
729+
expect(res).to.not.be.empty()
729730
})
730731
})
731732

0 commit comments

Comments
 (0)