|
1 | 1 | import { HttpModule } from "../core/http"; |
2 | 2 | import { Metrics } from "../transport/metrics"; |
3 | 3 | import { TraceoOptions } from "../transport/options"; |
4 | | -import { cpu } from "./metrics/cpu-usage"; |
5 | | -import * as os from "node:os"; |
6 | | -import { memory } from "./metrics/memory-usage"; |
7 | | -import { heap } from "./metrics/heap"; |
8 | | -import { eventLoop } from "./metrics/event-loop"; |
9 | 4 | import { toDecimalNumber } from "./helpers"; |
| 5 | +import { CpuUsageMetrics } from "./metrics/cpu-usage"; |
| 6 | +import { EventLoopMetrics } from "./metrics/event-loop"; |
| 7 | +import { HeapMetrics } from "./metrics/heap"; |
| 8 | +import { MemoryUsageMetrics } from "./metrics/memory-usage"; |
| 9 | +import * as os from "os"; |
10 | 10 |
|
11 | 11 | const DEFAULT_INTERVAL = 30; //seconds |
12 | 12 |
|
13 | | -const collectMetrics = (options: TraceoOptions) => { |
14 | | - const { metrics } = options; |
| 13 | +export class MetricsProbe { |
| 14 | + private readonly interval: number; |
15 | 15 |
|
16 | | - const INTERVAL = |
17 | | - (metrics?.interval < 15 ? DEFAULT_INTERVAL : metrics?.interval) * 1000; |
| 16 | + private readonly http: HttpModule; |
18 | 17 |
|
19 | | - setInterval(() => { |
20 | | - const metrics: Metrics = { |
21 | | - cpuUsage: cpu.usage(), |
22 | | - memory: memory.usage(), |
23 | | - loadAvg: toDecimalNumber(os.loadavg()[0]), |
| 18 | + private readonly cpuUsage: CpuUsageMetrics; |
| 19 | + private readonly eventLoop: EventLoopMetrics; |
| 20 | + private readonly heap: HeapMetrics; |
| 21 | + private readonly memoryUsage: MemoryUsageMetrics; |
| 22 | + |
| 23 | + constructor(options: TraceoOptions) { |
| 24 | + if (!options.metrics.collect) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + this.interval = options.metrics.interval || DEFAULT_INTERVAL; |
| 29 | + |
| 30 | + this.http = new HttpModule("/api/worker/metrics"); |
| 31 | + |
| 32 | + this.cpuUsage = new CpuUsageMetrics(); |
| 33 | + this.eventLoop = new EventLoopMetrics(); |
| 34 | + this.heap = new HeapMetrics(); |
| 35 | + this.memoryUsage = new MemoryUsageMetrics(); |
| 36 | + } |
| 37 | + |
| 38 | + public register() { |
| 39 | + setInterval(() => this.collectMetrics(), this.interval * 1000); |
| 40 | + } |
| 41 | + |
| 42 | + private collectMetrics() { |
| 43 | + const cpuUsage = this.cpuUsage.collect(); |
| 44 | + const eventLoop = this.eventLoop.collect(); |
| 45 | + const heap = this.heap.collect(); |
| 46 | + const memory = this.memoryUsage.collect(); |
| 47 | + |
| 48 | + const metrics: Partial<Metrics> = { |
| 49 | + cpuUsage, |
| 50 | + eventLoopLag: eventLoop, |
24 | 51 | heap, |
25 | | - eventLoopLag: eventLoop.collect(), |
| 52 | + memory, |
| 53 | + loadAvg: this.loadAvg, |
26 | 54 | }; |
27 | 55 |
|
28 | | - const httpModule = new HttpModule("/api/worker/metrics", metrics); |
29 | | - httpModule.request(); |
30 | | - }, INTERVAL); |
31 | | -}; |
| 56 | + this.http.request({ |
| 57 | + body: metrics, |
| 58 | + onError: (error: Error) => { |
| 59 | + console.error( |
| 60 | + `Traceo Error. Something went wrong while sending new Log to Traceo. Please report this issue.` |
| 61 | + ); |
| 62 | + console.error(`Caused by: ${error.message}`); |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
32 | 66 |
|
33 | | -export const metrics = { |
34 | | - collectMetrics, |
35 | | -}; |
| 67 | + private get loadAvg() { |
| 68 | + return toDecimalNumber(os.loadavg()[0]); |
| 69 | + } |
| 70 | +} |
0 commit comments