|
| 1 | +import { type MeterProvider } from "@opentelemetry/sdk-metrics"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as fsPromises from "node:fs/promises"; |
| 4 | + |
| 5 | +const VIRTUAL_FS_TYPES = new Set([ |
| 6 | + "proc", |
| 7 | + "sysfs", |
| 8 | + "devpts", |
| 9 | + "tmpfs", |
| 10 | + "devtmpfs", |
| 11 | + "cgroup", |
| 12 | + "cgroup2", |
| 13 | + "squashfs", |
| 14 | + "autofs", |
| 15 | + "debugfs", |
| 16 | + "securityfs", |
| 17 | + "pstore", |
| 18 | + "bpf", |
| 19 | + "tracefs", |
| 20 | + "hugetlbfs", |
| 21 | + "mqueue", |
| 22 | + "fusectl", |
| 23 | + "configfs", |
| 24 | + "binfmt_misc", |
| 25 | +]); |
| 26 | + |
| 27 | +type MountEntry = { |
| 28 | + device: string; |
| 29 | + mountpoint: string; |
| 30 | + fsType: string; |
| 31 | + options: string; |
| 32 | +}; |
| 33 | + |
| 34 | +function parseProcMounts(content: string): MountEntry[] { |
| 35 | + const entries: MountEntry[] = []; |
| 36 | + |
| 37 | + for (const line of content.split("\n")) { |
| 38 | + if (!line.trim()) continue; |
| 39 | + |
| 40 | + const parts = line.split(" "); |
| 41 | + if (parts.length < 4) continue; |
| 42 | + |
| 43 | + const fsType = parts[2]!; |
| 44 | + if (VIRTUAL_FS_TYPES.has(fsType)) continue; |
| 45 | + |
| 46 | + entries.push({ |
| 47 | + device: parts[0]!, |
| 48 | + mountpoint: unescapeMountPath(parts[1]!), |
| 49 | + fsType, |
| 50 | + options: parts[3]!, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + return entries; |
| 55 | +} |
| 56 | + |
| 57 | +function unescapeMountPath(path: string): string { |
| 58 | + return path.replace(/\\040/g, " ").replace(/\\011/g, "\t"); |
| 59 | +} |
| 60 | + |
| 61 | +export function startFilesystemMetrics(meterProvider: MeterProvider) { |
| 62 | + try { |
| 63 | + fs.accessSync("/proc/mounts", fs.constants.R_OK); |
| 64 | + } catch { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (typeof fsPromises.statfs !== "function") { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const meter = meterProvider.getMeter("system-filesystem", "1.0.0"); |
| 73 | + |
| 74 | + const usageCounter = meter.createObservableUpDownCounter("system.filesystem.usage", { |
| 75 | + description: "Filesystem bytes used, free, and reserved per mountpoint", |
| 76 | + unit: "By", |
| 77 | + }); |
| 78 | + |
| 79 | + const utilizationGauge = meter.createObservableGauge("system.filesystem.utilization", { |
| 80 | + description: "Fraction of filesystem space used (0-1)", |
| 81 | + unit: "1", |
| 82 | + }); |
| 83 | + |
| 84 | + meter.addBatchObservableCallback( |
| 85 | + async (obs) => { |
| 86 | + try { |
| 87 | + const mountsContent = await fsPromises.readFile("/proc/mounts", "utf-8"); |
| 88 | + const mounts = parseProcMounts(mountsContent); |
| 89 | + |
| 90 | + for (const mount of mounts) { |
| 91 | + try { |
| 92 | + const stats = await fsPromises.statfs(mount.mountpoint); |
| 93 | + const bsize = stats.bsize; |
| 94 | + const total = stats.blocks * bsize; |
| 95 | + const free = stats.bavail * bsize; |
| 96 | + const reserved = (stats.bfree - stats.bavail) * bsize; |
| 97 | + const used = total - stats.bfree * bsize; |
| 98 | + |
| 99 | + const mode = mount.options.startsWith("ro") ? "ro" : "rw"; |
| 100 | + |
| 101 | + const baseAttrs = { |
| 102 | + "system.device": mount.device, |
| 103 | + "system.filesystem.type": mount.fsType, |
| 104 | + "system.filesystem.mountpoint": mount.mountpoint, |
| 105 | + "system.filesystem.mode": mode, |
| 106 | + }; |
| 107 | + |
| 108 | + obs.observe(usageCounter, used, { |
| 109 | + ...baseAttrs, |
| 110 | + "system.filesystem.state": "used", |
| 111 | + }); |
| 112 | + obs.observe(usageCounter, free, { |
| 113 | + ...baseAttrs, |
| 114 | + "system.filesystem.state": "free", |
| 115 | + }); |
| 116 | + obs.observe(usageCounter, reserved, { |
| 117 | + ...baseAttrs, |
| 118 | + "system.filesystem.state": "reserved", |
| 119 | + }); |
| 120 | + |
| 121 | + if (total > 0) { |
| 122 | + obs.observe(utilizationGauge, used / total, baseAttrs); |
| 123 | + } |
| 124 | + } catch { |
| 125 | + // Skip this mount on statfs failure |
| 126 | + } |
| 127 | + } |
| 128 | + } catch { |
| 129 | + // Skip entire cycle on failure |
| 130 | + } |
| 131 | + }, |
| 132 | + [usageCounter, utilizationGauge] |
| 133 | + ); |
| 134 | +} |
0 commit comments