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

Commit

Permalink
feat: improve collected metrics (#3978)
Browse files Browse the repository at this point in the history
Exposes the per-component metrics from libp2p/js-libp2p#1061 in the prometheus end point.

Also allows changing the `debug` logging level dynamically.
  • Loading branch information
achingbrain authored Dec 15, 2021
1 parent 73476f5 commit 33f1034
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/ipfs-core-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"it-drain": "^1.0.3",
"libp2p-floodsub": "^0.28.0",
"libp2p-gossipsub": "^0.12.0",
"libp2p-kad-dht": "^0.27.0",
"libp2p-kad-dht": "^0.27.4",
"libp2p-mdns": "^0.18.0",
"libp2p-mplex": "^0.10.2",
"libp2p-tcp": "^0.17.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"it-tar": "^4.0.0",
"it-to-buffer": "^2.0.0",
"just-safe-set": "^2.2.1",
"libp2p": "^0.35.0",
"libp2p": "^0.35.4",
"libp2p-bootstrap": "^0.14.0",
"libp2p-crypto": "^0.21.0",
"libp2p-delegated-content-routing": "^0.11.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"ipfs-http-server": "^0.9.2",
"ipfs-utils": "^9.0.2",
"just-safe-set": "^2.2.1",
"libp2p": "^0.35.0",
"libp2p": "^0.35.4",
"libp2p-webrtc-star": "^0.25.0"
},
"devDependencies": {
Expand Down
44 changes: 39 additions & 5 deletions packages/ipfs-http-server/src/api/routes/debug.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import client from 'prom-client'
import Boom from '@hapi/boom'
import debug from 'debug'

// Clear the register to make sure we're not registering multiple ones
client.register.clear()
const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' })

/** @type {Record<string, client.Gauge<any>>} */
const gauges = {}

// Endpoint for handling debug metrics
export default [{
Expand All @@ -19,13 +22,44 @@ export default [{
}

const { ipfs } = request.server.app
const peers = await ipfs.swarm.peers()
// @ts-expect-error libp2p does not exist on ipfs
const metrics = ipfs.libp2p.metrics

if (metrics) {
for (const [component, componentMetrics] of metrics.getComponentMetrics().entries()) {
for (const [metricName, metricValue] of componentMetrics.entries()) {
const name = `libp2p-${component}-${metricName}`.replace(/-/g, '_')

gauge.set(peers.length)
if (!gauges[name]) {
gauges[name] = new client.Gauge({ name, help: name })
}

const metrics = await client.register.metrics()
gauges[name].set(metricValue)
}
}
}

return h.response(metrics)
return h.response(await client.register.metrics())
.type(client.register.contentType)
}
}, {
method: 'POST',
path: '/debug/logs',
/**
* @param {import('../../types').Request} request
* @param {import('@hapi/hapi').ResponseToolkit} h
*/
async handler (request, h) {
if (!process.env.IPFS_MONITORING) {
throw Boom.notImplemented('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING')
}

if (!request.query.debug) {
debug.disable()
} else {
debug.enable(request.query.debug)
}

return h.response()
}
}]

0 comments on commit 33f1034

Please sign in to comment.