This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
/
debug.js
101 lines (87 loc) · 3.09 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable max-depth */
import client from 'prom-client'
import Boom from '@hapi/boom'
import { disable, enable } from '@libp2p/logger'
// Clear the register to make sure we're not registering multiple ones
client.register.clear()
/** @type {Record<string, client.Gauge<any>>} */
const gauges = {
nodejs_memory_usage: new client.Gauge({
name: 'nodejs_memory_usage',
help: 'nodejs_memory_usage',
labelNames: Object.keys(process.memoryUsage())
})
}
// Endpoint for handling debug metrics
export default [{
method: 'GET',
path: '/debug/metrics/prometheus',
/**
* @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')
}
Object.entries(process.memoryUsage()).forEach(([key, value]) => {
gauges.nodejs_memory_usage.set({ [key]: key }, value)
})
const { ipfs } = request.server.app
// @ts-expect-error libp2p does not exist on ipfs
const metrics = ipfs.libp2p.metrics
if (metrics) {
for (const [system, components] of metrics.getComponentMetrics().entries()) {
for (const [component, componentMetrics] of components.entries()) {
for (const [metricName, trackedMetric] of componentMetrics.entries()) {
const name = `${system}-${component}-${metricName}`.replace(/-/g, '_')
const labelName = trackedMetric.label ?? metricName.replace(/-/g, '_')
const help = trackedMetric.help ?? metricName.replace(/-/g, '_')
/** @type {client.GaugeConfiguration<any>} */
const gaugeOptions = { name, help }
const metricValue = await trackedMetric.calculate()
if (typeof metricValue !== 'number') {
// metric group
gaugeOptions.labelNames = [
labelName
]
}
if (!gauges[name]) {
// create metric if it's not been seen before
gauges[name] = new client.Gauge(gaugeOptions)
}
if (typeof metricValue !== 'number') {
// metric group
Object.entries(metricValue).forEach(([key, value]) => {
gauges[name].set({ [labelName]: key }, value)
})
} else {
// metric value
gauges[name].set(metricValue)
}
}
}
}
}
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) {
disable()
} else {
enable(request.query.debug)
}
return h.response()
}
}]