Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 33f1034

Browse files
authored
feat: improve collected metrics (#3978)
Exposes the per-component metrics from libp2p/js-libp2p#1061 in the prometheus end point. Also allows changing the `debug` logging level dynamically.
1 parent 73476f5 commit 33f1034

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

packages/ipfs-core-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"it-drain": "^1.0.3",
9696
"libp2p-floodsub": "^0.28.0",
9797
"libp2p-gossipsub": "^0.12.0",
98-
"libp2p-kad-dht": "^0.27.0",
98+
"libp2p-kad-dht": "^0.27.4",
9999
"libp2p-mdns": "^0.18.0",
100100
"libp2p-mplex": "^0.10.2",
101101
"libp2p-tcp": "^0.17.1",

packages/ipfs-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"it-tar": "^4.0.0",
112112
"it-to-buffer": "^2.0.0",
113113
"just-safe-set": "^2.2.1",
114-
"libp2p": "^0.35.0",
114+
"libp2p": "^0.35.4",
115115
"libp2p-bootstrap": "^0.14.0",
116116
"libp2p-crypto": "^0.21.0",
117117
"libp2p-delegated-content-routing": "^0.11.1",

packages/ipfs-daemon/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"ipfs-http-server": "^0.9.2",
5353
"ipfs-utils": "^9.0.2",
5454
"just-safe-set": "^2.2.1",
55-
"libp2p": "^0.35.0",
55+
"libp2p": "^0.35.4",
5656
"libp2p-webrtc-star": "^0.25.0"
5757
},
5858
"devDependencies": {

packages/ipfs-http-server/src/api/routes/debug.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import client from 'prom-client'
22
import Boom from '@hapi/boom'
3+
import debug from 'debug'
34

45
// Clear the register to make sure we're not registering multiple ones
56
client.register.clear()
6-
const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' })
7+
8+
/** @type {Record<string, client.Gauge<any>>} */
9+
const gauges = {}
710

811
// Endpoint for handling debug metrics
912
export default [{
@@ -19,13 +22,44 @@ export default [{
1922
}
2023

2124
const { ipfs } = request.server.app
22-
const peers = await ipfs.swarm.peers()
25+
// @ts-expect-error libp2p does not exist on ipfs
26+
const metrics = ipfs.libp2p.metrics
27+
28+
if (metrics) {
29+
for (const [component, componentMetrics] of metrics.getComponentMetrics().entries()) {
30+
for (const [metricName, metricValue] of componentMetrics.entries()) {
31+
const name = `libp2p-${component}-${metricName}`.replace(/-/g, '_')
2332

24-
gauge.set(peers.length)
33+
if (!gauges[name]) {
34+
gauges[name] = new client.Gauge({ name, help: name })
35+
}
2536

26-
const metrics = await client.register.metrics()
37+
gauges[name].set(metricValue)
38+
}
39+
}
40+
}
2741

28-
return h.response(metrics)
42+
return h.response(await client.register.metrics())
2943
.type(client.register.contentType)
3044
}
45+
}, {
46+
method: 'POST',
47+
path: '/debug/logs',
48+
/**
49+
* @param {import('../../types').Request} request
50+
* @param {import('@hapi/hapi').ResponseToolkit} h
51+
*/
52+
async handler (request, h) {
53+
if (!process.env.IPFS_MONITORING) {
54+
throw Boom.notImplemented('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING')
55+
}
56+
57+
if (!request.query.debug) {
58+
debug.disable()
59+
} else {
60+
debug.enable(request.query.debug)
61+
}
62+
63+
return h.response()
64+
}
3165
}]

0 commit comments

Comments
 (0)