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

Commit 83db12d

Browse files
committed
feat: improve collected metrics
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 adde8c1 commit 83db12d

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-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

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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+
const gauges = {}
79

810
// Endpoint for handling debug metrics
911
export default [{
@@ -19,13 +21,43 @@ export default [{
1921
}
2022

2123
const { ipfs } = request.server.app
22-
const peers = await ipfs.swarm.peers()
24+
const metrics = ipfs.libp2p.metrics
25+
26+
if (metrics) {
27+
for (const [component, componentMetrics] of metrics.getComponentMetrics().entries()) {
28+
for (const [metricName, metricValue] of componentMetrics.entries()) {
29+
const name = `libp2p-${component}-${metricName}`.replace(/-/g, '_')
2330

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

26-
const metrics = await client.register.metrics()
35+
gauges[name].set(metricValue)
36+
}
37+
}
38+
}
2739

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

0 commit comments

Comments
 (0)