Skip to content

Commit 3403b8d

Browse files
committed
main options modification, gc metrics
1 parent ef71fc3 commit 3403b8d

7 files changed

Lines changed: 72 additions & 24 deletions

File tree

lib/node/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class Client {
4343
}
4444

4545
get isCollectMetrics(): boolean {
46-
return this.options?.metrics?.collect;
46+
return this.options?.collectMetrics;
4747
}
4848

4949
get isConnected(): boolean {
@@ -53,7 +53,7 @@ export class Client {
5353
private initSDK(): void {
5454
this.runtimeData.collect();
5555

56-
if (this.options.metrics.collect) {
56+
if (this.options.collectMetrics) {
5757
this.metricsProbe.register();
5858
}
5959
}

lib/node/metrics.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { EventLoopMetrics } from "./metrics/event-loop";
77
import { HeapMetrics } from "./metrics/heap";
88
import { MemoryUsageMetrics } from "./metrics/memory-usage";
99
import * as os from "os";
10+
import { GCObserver } from "./metrics/gc-observer";
1011

1112
const DEFAULT_INTERVAL = 30; //seconds
1213

@@ -20,19 +21,22 @@ export class MetricsProbe {
2021
private readonly heap: HeapMetrics;
2122
private readonly memoryUsage: MemoryUsageMetrics;
2223

24+
private readonly gcObserver: GCObserver;
25+
2326
constructor(options: TraceoOptions) {
24-
if (!options.metrics.collect) {
27+
if (!options?.collectMetrics) {
2528
return;
2629
}
2730

28-
this.interval = options.metrics.interval || DEFAULT_INTERVAL;
31+
this.interval = options.scrapMetricsInterval || DEFAULT_INTERVAL;
2932

3033
this.http = new HttpModule("/api/worker/metrics");
3134

3235
this.cpuUsage = new CpuUsageMetrics();
3336
this.eventLoop = new EventLoopMetrics();
3437
this.heap = new HeapMetrics();
3538
this.memoryUsage = new MemoryUsageMetrics();
39+
this.gcObserver = new GCObserver();
3640
}
3741

3842
public register() {
@@ -44,13 +48,15 @@ export class MetricsProbe {
4448
const eventLoop = this.eventLoop.collect();
4549
const heap = this.heap.collect();
4650
const memory = this.memoryUsage.collect();
51+
const gc = this.gcObserver.collect();
4752

4853
const metrics: Partial<Metrics> = {
4954
cpuUsage,
5055
eventLoopLag: eventLoop,
5156
heap,
5257
memory,
5358
loadAvg: this.loadAvg,
59+
gc,
5460
};
5561

5662
this.http.request({

lib/node/metrics/gc-observer.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { IMetrics } from "../../core/interfaces/IMetrics";
2+
import * as perf_hooks from "node:perf_hooks";
3+
import { toDecimalNumber } from "../helpers";
4+
5+
type GCObserverType = {
6+
duration: {
7+
total: number;
8+
average: number;
9+
};
10+
};
11+
12+
export class GCObserver implements IMetrics<GCObserverType> {
13+
private gcDurations: number[] = [];
14+
15+
// private GC_MAJOR: number = 0;
16+
// private GC_MINOR: number = 0;
17+
// private GC_INCREMENTAL: number = 0;
18+
// private GC_WEAKCB: number = 0;
19+
20+
constructor() {
21+
const obs = new perf_hooks.PerformanceObserver((list) => {
22+
const entry = list.getEntries()[0];
23+
// const kind = !entry.detail ? entry["kind"] : entry.detail["kind"];
24+
25+
this.gcDurations.push(entry.duration);
26+
});
27+
obs.observe({ entryTypes: ["gc"] });
28+
}
29+
30+
collect(): GCObserverType {
31+
const total = this.gcDurations.reduce((prev, curr) => prev + curr, 0);
32+
const average = total / this.gcDurations.length;
33+
34+
this.gcDurations = [];
35+
36+
return {
37+
duration: {
38+
total: toDecimalNumber(total),
39+
average: toDecimalNumber(average),
40+
},
41+
};
42+
}
43+
}

lib/transport/metrics.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ export interface Metrics {
1717
max: number;
1818
mean: number;
1919
stddev: number;
20-
// p50: number;
21-
// p90: number;
22-
// p99: number;
20+
};
21+
gc: {
22+
duration: {
23+
total: number;
24+
average: number;
25+
};
2326
};
2427
}

lib/transport/options.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ export interface TraceoOptions {
22
appId: number;
33
offline?: boolean;
44
url: string;
5-
metrics?: {
6-
/*
7-
Determining if Traceo should collect metrics from the application.
8-
*/
9-
collect?: boolean;
10-
/*
11-
The number of seconds that the metrics are downloaded.
12-
The minimum value for this field is 15. If the value is set below this value,
13-
then the data will be downloaded just for this time.
14-
*/
15-
interval?: number;
16-
};
5+
/*
6+
Determining if Traceo should collect metrics from the application.
7+
*/
8+
collectMetrics?: boolean;
9+
/*
10+
The number of seconds that the metrics are downloaded.
11+
The minimum value for this field is 15. If the value is set below this value,
12+
then the data will be downloaded just for this time.
13+
*/
14+
scrapMetricsInterval?: number;
1715
}
1816

1917
export interface ErrorMiddlewareOptions {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "traceo-sdk",
3-
"version": "0.3.21-beta",
3+
"version": "0.3.24-beta",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

test/client.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ describe("TraceClient", () => {
77
const clientOptions: TraceoOptions = {
88
appId: 35,
99
url: "/",
10-
metrics: {
11-
collect: true,
12-
interval: 15,
13-
},
10+
collectMetrics: true,
11+
scrapMetricsInterval: 15,
1412
};
1513

1614
beforeEach(() => {

0 commit comments

Comments
 (0)