|
66 | 66 | * ```
|
67 | 67 | */
|
68 | 68 |
|
| 69 | +import { statfs } from 'node:fs/promises' |
| 70 | +import { totalmem } from 'node:os' |
69 | 71 | import { serviceCapabilities } from '@libp2p/interface'
|
70 | 72 | import each from 'it-foreach'
|
71 | 73 | import { collectDefaultMetrics, type DefaultMetricsCollectorConfiguration, register, type Registry, type RegistryContentType } from 'prom-client'
|
@@ -111,6 +113,14 @@ export interface PrometheusMetricsInit {
|
111 | 113 | * pass true here
|
112 | 114 | */
|
113 | 115 | preserveExistingMetrics?: boolean
|
| 116 | + |
| 117 | + /** |
| 118 | + * The current filesystem usage is reported as the metric |
| 119 | + * `nodejs_fs_usage_bytes` using the `statfs` function from `node:fs` - the |
| 120 | + * default location to stat is the current working directory, configured this |
| 121 | + * location here |
| 122 | + */ |
| 123 | + statfsLocation?: string |
114 | 124 | }
|
115 | 125 |
|
116 | 126 | export interface PrometheusCalculatedMetricOptions<T=number> extends CalculatedMetricOptions<T> {
|
@@ -178,6 +188,25 @@ class PrometheusMetrics implements Metrics {
|
178 | 188 | }
|
179 | 189 | }
|
180 | 190 | })
|
| 191 | + const totalMemoryMetric = this.registerMetric('nodejs_memory_total_bytes') |
| 192 | + totalMemoryMetric.update(totalmem()) |
| 193 | + |
| 194 | + this.log('Collecting filesystem metrics') |
| 195 | + this.registerMetricGroup('nodejs_fs_usage_bytes', { |
| 196 | + label: 'filesystem', |
| 197 | + calculate: async () => { |
| 198 | + const stats = await statfs(init?.statfsLocation ?? process.cwd()) |
| 199 | + const total = stats.bsize * stats.blocks |
| 200 | + const available = stats.bsize * stats.bavail |
| 201 | + |
| 202 | + return { |
| 203 | + total, |
| 204 | + free: stats.bsize * stats.bfree, |
| 205 | + available, |
| 206 | + used: (available / total) * 100 |
| 207 | + } |
| 208 | + } |
| 209 | + }) |
181 | 210 | }
|
182 | 211 |
|
183 | 212 | readonly [Symbol.toStringTag] = '@libp2p/metrics-prometheus'
|
|
0 commit comments