( preference: 'auto' | 'host' | 'cgroup', fileExists: FileExists = defaultFileExists, )
| 333 | ]; |
| 334 | |
| 335 | export function detectMachineStatsSource( |
| 336 | preference: 'auto' | 'host' | 'cgroup', |
| 337 | fileExists: FileExists = defaultFileExists, |
| 338 | ): MachineStatsSource { |
| 339 | if (preference === 'host') return new HostSource(); |
| 340 | |
| 341 | const hasAll = (paths: string[]) => paths.every((p) => fileExists(p)); |
| 342 | const hasV2 = hasAll(CGROUP_V2_REQUIRED); |
| 343 | const hasV1 = !hasV2 && hasAll(CGROUP_V1_REQUIRED); |
| 344 | |
| 345 | if (preference === 'cgroup') { |
| 346 | if (hasV2) return new CgroupV2Source(); |
| 347 | if (hasV1) return new CgroupV1Source(); |
| 348 | throw new Error( |
| 349 | 'MACHINE_STATS_SOURCE=cgroup but required cgroup stat files are missing.', |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | // 'auto' |
| 354 | if (hasV2) return new CgroupV2Source(); |
| 355 | if (hasV1) return new CgroupV1Source(); |
| 356 | return new HostSource(); |
| 357 | } |
| 358 | |
| 359 | export class Monitoring extends EventEmitter { |
| 360 | protected log = new Logger('hardware'); |
no test coverage detected