Skip to content

Commit be75e97

Browse files
feat/added data point type to default metrics
1 parent fb7af21 commit be75e97

6 files changed

Lines changed: 34 additions & 18 deletions

File tree

packages/node-core/src/types/opentelemetry.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export interface SumMetricData extends BaseMetricData {
115115
readonly dataPoints: DataPoint<number>[];
116116
readonly isMonotonic: boolean;
117117
}
118+
export interface TimeSeriesMetricData extends BaseMetricData {
119+
readonly dataPointType: DataPointType.TIME_SERIES;
120+
readonly dataPoints: DataPoint<number>[];
121+
}
118122
export interface GaugeMetricData extends BaseMetricData {
119123
readonly dataPointType: DataPointType.GAUGE;
120124
readonly dataPoints: DataPoint<number>[];
@@ -136,7 +140,7 @@ export interface ExponentialHistogramMetricData extends BaseMetricData {
136140
/**
137141
* Represents an aggregated metric data.
138142
*/
139-
export type MetricData = SumMetricData | GaugeMetricData | HistogramMetricData | ExponentialHistogramMetricData;
143+
export type MetricData = SumMetricData | GaugeMetricData | TimeSeriesMetricData | HistogramMetricData | ExponentialHistogramMetricData;
140144
export interface ScopeMetrics {
141145
scope: InstrumentationScope;
142146
metrics: MetricData[];
@@ -167,7 +171,11 @@ export enum DataPointType {
167171
* A sum metric data point has a single numeric value and a
168172
* monotonicity-indicator.
169173
*/
170-
SUM = 3
174+
SUM = 3,
175+
/**
176+
* A value correlated to time when occur
177+
*/
178+
TIME_SERIES = 4
171179
}
172180
/**
173181
* Represents an aggregated point data with start time, end time and their

packages/node/src/metrics/default/cpu-usage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as os from "os";
2-
import { IMetrics, AverageCpuMetricType, InstrumentType, ValueType, MetricType } from "@traceo-sdk/node-core";
2+
import { IMetrics, AverageCpuMetricType, InstrumentType, ValueType, MetricType, utils, DataPointType } from "@traceo-sdk/node-core";
33

44
export class CpuUsageMetrics implements IMetrics {
55
private measureStart: AverageCpuMetricType;
@@ -22,7 +22,8 @@ export class CpuUsageMetrics implements IMetrics {
2222
type: InstrumentType.TIME_SERIES,
2323
valueType: ValueType.DOUBLE
2424
},
25-
dataPoints: [{ value: cpuUsage }]
25+
dataPointType: DataPointType.TIME_SERIES,
26+
dataPoints: [{ value: cpuUsage, startTime: [utils.currentUnix()] }]
2627
}];
2728
}
2829

packages/node/src/metrics/default/event-loop.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utils, IMetrics, EventLoopMetricType, MetricType, InstrumentType, ValueType } from "@traceo-sdk/node-core";
1+
import { utils, IMetrics, EventLoopMetricType, MetricType, InstrumentType, ValueType, DataPointType } from "@traceo-sdk/node-core";
22

33
let perf_hooks;
44
try {
@@ -38,7 +38,8 @@ export class EventLoopMetrics implements IMetrics {
3838
type: InstrumentType.TIME_SERIES,
3939
valueType: ValueType.DOUBLE
4040
},
41-
dataPoints: [{ value: data[metric] }]
41+
dataPointType: DataPointType.TIME_SERIES,
42+
dataPoints: [{ value: data[metric], startTime: [utils.currentUnix()] }]
4243
}));
4344

4445
return response;

packages/node/src/metrics/default/heap.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utils, IMetrics, MetricType, InstrumentType } from "@traceo-sdk/node-core";
1+
import { utils, IMetrics, MetricType, InstrumentType, DataPointType } from "@traceo-sdk/node-core";
22

33
/**
44
* https://www.geeksforgeeks.org/node-js-v8-getheapstatistics-method/
@@ -13,15 +13,18 @@ export class HeapMetrics implements IMetrics {
1313
return [
1414
{
1515
descriptor: { name: "heap_used", type: InstrumentType.TIME_SERIES },
16-
dataPoints: [{ value: this.usedHeap }]
16+
dataPointType: DataPointType.TIME_SERIES,
17+
dataPoints: [{ value: this.usedHeap, startTime: [utils.currentUnix()] }]
1718
},
1819
{
1920
descriptor: { name: "heap_total", type: InstrumentType.TIME_SERIES },
20-
dataPoints: [{ value: this.totalHeap }]
21+
dataPointType: DataPointType.TIME_SERIES,
22+
dataPoints: [{ value: this.totalHeap, startTime: [utils.currentUnix()] }]
2123
},
2224
{
2325
descriptor: { name: "heap_rss", type: InstrumentType.TIME_SERIES },
24-
dataPoints: [{ value: this.rss }]
26+
dataPointType: DataPointType.TIME_SERIES,
27+
dataPoints: [{ value: this.rss, startTime: [utils.currentUnix()] }]
2528
},
2629
];
2730
}

packages/node/src/metrics/default/memory-usage.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import * as os from "os";
2-
import { IMetrics, utils, MetricType, InstrumentType } from "@traceo-sdk/node-core";
2+
import { IMetrics, utils, MetricType, InstrumentType, DataPointType } from "@traceo-sdk/node-core";
33

44
export class MemoryUsageMetrics implements IMetrics {
55
constructor() { }
66

77
public collect(): MetricType {
88
return [
99
{
10-
descriptor: { name: "memory_usage_mb", type: InstrumentType.TIME_SERIES, unit: "mb", },
11-
dataPoints: [{ value: this.usedMemory }]
10+
descriptor: { name: "memory_usage_mb", type: InstrumentType.TIME_SERIES, unit: "mb" },
11+
dataPointType: DataPointType.TIME_SERIES,
12+
dataPoints: [{ value: this.usedMemory, startTime: [utils.currentUnix()] }]
1213
},
1314
{
14-
descriptor: { name: "memory_usage_percentage", type: InstrumentType.TIME_SERIES, unit: "mb", },
15-
dataPoints: [{ value: this.percentageUsage }]
15+
descriptor: { name: "memory_usage_percentage", type: InstrumentType.TIME_SERIES, unit: "mb" },
16+
dataPointType: DataPointType.TIME_SERIES,
17+
dataPoints: [{ value: this.percentageUsage, startTime: [utils.currentUnix()] }]
1618
}
1719
];
1820
}

packages/node/src/metrics/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CpuUsageMetrics } from "./default/cpu-usage";
22
import { EventLoopMetrics } from "./default/event-loop";
33
import { HeapMetrics } from "./default/heap";
44
import { MemoryUsageMetrics } from "./default/memory-usage";
5-
import { transport, CAPTURE_ENDPOINT, utils, MetricType, InstrumentType, ValueType, INodeClient } from "@traceo-sdk/node-core";
5+
import { transport, CAPTURE_ENDPOINT, utils, MetricType, InstrumentType, ValueType, INodeClient, DataPointType } from "@traceo-sdk/node-core";
66
import * as os from "os";
77

88
/**
@@ -79,9 +79,10 @@ export class MetricsRunner {
7979
descriptor: {
8080
name: "load_avg",
8181
type: InstrumentType.OBSERVABLE_GAUGE,
82-
valueType: ValueType.DOUBLE
82+
valueType: ValueType.DOUBLE,
8383
},
84-
dataPoints: [{ value: load }]
84+
dataPointType: DataPointType.GAUGE,
85+
dataPoints: [{ value: load, startTime: [utils.currentUnix()] }]
8586
}]
8687
}
8788
}

0 commit comments

Comments
 (0)