Skip to content

Commit 184b212

Browse files
committed
chore(instrumentation-runtime-node): fetch other metrics to convention
1 parent 2cd9093 commit 184b212

File tree

8 files changed

+99
-147
lines changed

8 files changed

+99
-147
lines changed

plugins/node/instrumentation-runtime-node/src/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const DEFAULT_CONFIG: RuntimeNodeInstrumentationConfig = {
2828
monitoringPrecision: 5000,
2929
};
3030

31-
const namePrefix = 'nodejs';
31+
const namePrefix = 'jsruntime';
3232

3333
export class RuntimeNodeInstrumentation extends InstrumentationBase {
3434
private _collectors: MetricCollector[] = [];

plugins/node/instrumentation-runtime-node/src/metrics/eventLoopLagCollector.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import {RuntimeNodeInstrumentationConfig} from '../types';
1717
import {Meter} from '@opentelemetry/api';
1818
import * as perf_hooks from 'node:perf_hooks';
19+
import {version} from 'node:process';
1920
import {IntervalHistogram} from 'node:perf_hooks';
2021
import {BaseCollector} from './baseCollector';
2122
import {NODE_JS_VERSION_ATTRIBUTE} from "../consts/attributes";
2223

2324
const NODEJS_EVENTLOOP_LAG = 'eventloop.lag';
24-
const NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE = 'nodejs.eventloop.lag.type';
25+
const NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE = 'eventloop.lag.type';
2526

2627

2728
export interface EventLoopLagInformation {
@@ -54,32 +55,31 @@ export class EventLoopLagCollector extends BaseCollector<EventLoopLagInformation
5455
description: "Event loop lag.",
5556
unit: 's'
5657
},
57-
)
58-
.addCallback(async observableResult => {
59-
if (this._scrapeQueue.length === 0) return;
60-
61-
const data = this._scrapeQueue.shift();
62-
if (data === undefined) return;
63-
64-
const start = process.hrtime();
65-
const lagResult = await new Promise<number>(res => {
66-
setImmediate((start: [number, number]) => {
67-
res(this._reportEventloopLag(start));
68-
}, start);
69-
});
58+
).addCallback(async observableResult => {
59+
if (this._scrapeQueue.length === 0) return;
7060

71-
observableResult.observe(lagResult, {
72-
[NODE_JS_VERSION_ATTRIBUTE]: process.version
73-
});
61+
const data = this._scrapeQueue.shift();
62+
if (data === undefined) return;
7463

75-
for(const [value, attributeType] of Object.keys(data).entries()) {
76-
observableResult.observe(value, {
77-
[NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE]: attributeType,
78-
[NODE_JS_VERSION_ATTRIBUTE]: process.version
79-
});
80-
}
64+
const start = process.hrtime();
65+
const lagResult = await new Promise<number>(res => {
66+
setImmediate((start: [number, number]) => {
67+
res(this._reportEventloopLag(start));
68+
}, start);
69+
});
8170

71+
observableResult.observe(lagResult, {
72+
[NODE_JS_VERSION_ATTRIBUTE]: version
8273
});
74+
75+
for (const [value, attributeType] of Object.keys(data).entries()) {
76+
observableResult.observe(value, {
77+
[NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE]: attributeType,
78+
[`${this.namePrefix}.${NODEJS_EVENTLOOP_LAG_ATTRIBUTE_TYPE}`]: version
79+
});
80+
}
81+
82+
});
8383
}
8484

8585
internalEnable(): void {

plugins/node/instrumentation-runtime-node/src/metrics/eventLoopUtilizationCollector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BaseCollector } from './baseCollector';
2020

2121
const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;
2222

23-
const NODEJS_EVENT_LOOP_UTILIZATION = 'event_loop.utilization';
23+
const NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';
2424

2525
export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtilization> {
2626
constructor(
@@ -36,7 +36,7 @@ export class EventLoopUtilizationCollector extends BaseCollector<EventLoopUtiliz
3636
`${this.namePrefix}.${NODEJS_EVENT_LOOP_UTILIZATION}`,
3737
{
3838
description: 'Event loop utilization',
39-
unit: '1',
39+
unit: 's',
4040
}
4141
)
4242
.addCallback(async observableResult => {

plugins/node/instrumentation-runtime-node/src/metrics/gcCollector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BaseCollector } from './baseCollector';
2020
import * as perf_hooks from 'node:perf_hooks';
2121
import { PerformanceObserver } from 'node:perf_hooks';
2222

23-
const NODEJS_GC_DURATION_SECONDS = 'gc.duration_seconds';
23+
const NODEJS_GC_DURATION_SECONDS = 'gc.duration';
2424
const DEFAULT_GC_DURATION_BUCKETS = [0.001, 0.01, 0.1, 1, 2, 5];
2525

2626
const kinds: string[] = [];
@@ -59,7 +59,7 @@ export class GCCollector extends BaseCollector<null> {
5959
{
6060
description:
6161
'Garbage collection duration by kind, one of major, minor, incremental or weakcb.',
62-
unit: 'double',
62+
unit: 's',
6363
valueType: ValueType.DOUBLE,
6464
advice: {
6565
explicitBucketBoundaries: DEFAULT_GC_DURATION_BUCKETS,

plugins/node/instrumentation-runtime-node/src/metrics/heapSizeAndUsedCollector.ts

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,10 @@
1616
import { RuntimeNodeInstrumentationConfig } from '../types';
1717
import { Meter } from '@opentelemetry/api';
1818
import { BaseCollector } from './baseCollector';
19+
import {HeapSizes} from "../types/heapSizes";
1920

20-
const NODEJS_HEAP_SIZE_TOTAL = 'heap_size_total_bytes';
21-
const NODEJS_HEAP_SIZE_USED = 'heap_size_used_bytes';
22-
const NODEJS_EXTERNAL_MEMORY = 'external_memory_bytes';
23-
24-
export const metricNames = [
25-
{
26-
name: NODEJS_HEAP_SIZE_TOTAL,
27-
description: 'Process heap size from Node.js in bytes.',
28-
},
29-
{
30-
name: NODEJS_HEAP_SIZE_USED,
31-
description: 'Process heap size used from Node.js in bytes.',
32-
},
33-
{
34-
name: NODEJS_EXTERNAL_MEMORY,
35-
description: 'Node.js external memory size in bytes.',
36-
},
37-
];
21+
const NODEJS_HEAP_SIZE = 'heap.size';
22+
const NODEJS_HEAP_SIZE_STATE = 'heap.size.state';
3823

3924
export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage> {
4025
constructor(
@@ -45,42 +30,24 @@ export class HeapSizeAndUsedCollector extends BaseCollector<NodeJS.MemoryUsage>
4530
}
4631

4732
updateMetricInstruments(meter: Meter): void {
48-
const heapSizeTotal = meter.createObservableGauge(
49-
`${this.namePrefix}.${metricNames[0].name}`,
50-
{
51-
description: metricNames[0].description,
52-
unit: '1',
53-
}
54-
);
55-
const heapSizeUsed = meter.createObservableGauge(
56-
`${this.namePrefix}.${metricNames[1].name}`,
33+
meter.createObservableGauge(
34+
`${this.namePrefix}.${NODEJS_HEAP_SIZE}`,
5735
{
58-
description: metricNames[1].description,
59-
unit: '1',
36+
description: "Process heap size from Node.js in bytes.",
37+
unit: 'By',
6038
}
61-
);
62-
const externalMemUsed = meter.createObservableGauge(
63-
`${this.namePrefix}.${metricNames[2].name}`,
64-
{
65-
description: metricNames[2].description,
66-
unit: '1',
67-
}
68-
);
69-
70-
meter.addBatchObservableCallback(
71-
observableResult => {
72-
if (this._scrapeQueue.length === 0) return;
73-
74-
const data = this._scrapeQueue.shift();
75-
if (data === undefined) return;
76-
observableResult.observe(heapSizeTotal, data.heapTotal);
77-
observableResult.observe(heapSizeUsed, data.heapUsed);
78-
if (data.external !== undefined) {
79-
observableResult.observe(externalMemUsed, data.external);
80-
}
81-
},
82-
[heapSizeTotal, heapSizeUsed, externalMemUsed]
83-
);
39+
).addCallback(async observableResult => {
40+
if (this._scrapeQueue.length === 0) return;
41+
42+
const data = this._scrapeQueue.shift();
43+
if (data === undefined) return;
44+
observableResult.observe(data.heapTotal, {
45+
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Total
46+
});
47+
observableResult.observe(data.heapUsed, {
48+
[`${this.namePrefix}.${NODEJS_HEAP_SIZE_STATE}`]: HeapSizes.Used
49+
});
50+
});
8451
}
8552

8653
internalEnable(): void {}

plugins/node/instrumentation-runtime-node/src/metrics/heapSpacesSizeAndUsedCollector.ts

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { RuntimeNodeInstrumentationConfig } from '../types';
17-
import { Meter } from '@opentelemetry/api';
18-
import { BaseCollector } from './baseCollector';
16+
import {RuntimeNodeInstrumentationConfig} from '../types';
17+
import {Meter} from '@opentelemetry/api';
18+
import {BaseCollector} from './baseCollector';
1919
import * as v8 from 'node:v8';
20-
import { HeapSpaceInfo } from 'v8';
20+
import {HeapSpaceInfo} from 'v8';
21+
import {HeapSpaces} from "../types/heapSpaces";
2122

22-
const NODEJS_HEAP_SPACE_TOTAL = 'heap_space_total_bytes';
23-
const NODEJS_HEAP_SPACE_USED = 'heap_space_used_bytes';
24-
const NODEJS_HEAP_SPACE_AVAILABLE = 'heap_space_available_bytes';
23+
const NODEJS_HEAP_SPACE = 'heap.space';
24+
const NODEJS_HEAP_SPACE_STATE = 'heap.space.state';
25+
const NODEJS_HEAP_SPACE_SPACENAME = 'heap.space.spacename';
2526

26-
export const metricNames = [
27-
{
28-
name: NODEJS_HEAP_SPACE_TOTAL,
29-
description: 'Process heap space size total from Node.js in bytes.',
30-
},
31-
{
32-
name: NODEJS_HEAP_SPACE_USED,
33-
description: 'Process heap space size used from Node.js in bytes.',
34-
},
35-
{
36-
name: NODEJS_HEAP_SPACE_AVAILABLE,
37-
description: 'Process heap space size available from Node.js in bytes.',
38-
},
39-
];
4027

4128
export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
4229
HeapSpaceInfo[]
@@ -49,59 +36,48 @@ export class HeapSpacesSizeAndUsedCollector extends BaseCollector<
4936
}
5037

5138
updateMetricInstruments(meter: Meter): void {
52-
const heapSpaceTotal = meter.createObservableGauge(
53-
`${this.namePrefix}.${metricNames[0].name}`,
39+
meter.createObservableGauge(
40+
`${this.namePrefix}.${NODEJS_HEAP_SPACE}`,
5441
{
55-
description: metricNames[0].description,
56-
unit: 'bytes',
42+
description: "Process heap space size total from Node.js in bytes.",
43+
unit: 'By',
5744
}
58-
);
59-
const heapSpaceUsed = meter.createObservableGauge(
60-
`${this.namePrefix}.${metricNames[1].name}`,
61-
{
62-
description: metricNames[1].description,
63-
unit: 'bytes',
64-
}
65-
);
66-
const heapSpaceAvailable = meter.createObservableGauge(
67-
`${this.namePrefix}.${metricNames[2].name}`,
68-
{
69-
description: metricNames[2].description,
70-
unit: 'bytes',
71-
}
72-
);
45+
).addCallback(async observableResult => {
46+
if (this._scrapeQueue.length === 0) return;
7347

74-
meter.addBatchObservableCallback(
75-
observableResult => {
76-
if (this._scrapeQueue.length === 0) return;
48+
const data = this._scrapeQueue.shift();
49+
if (data === undefined) return;
50+
for (const space of data) {
51+
const spaceName = space.space_name.substring(
52+
0,
53+
space.space_name.indexOf('_space')
54+
);
55+
observableResult.observe(space.space_size, {
56+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_SPACENAME}`]: spaceName,
57+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_STATE}`]: HeapSpaces.Total
58+
});
59+
observableResult.observe(space.space_used_size, {
60+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_SPACENAME}`]: spaceName,
61+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_STATE}`]: HeapSpaces.Used
62+
63+
});
64+
observableResult.observe(
65+
space.space_available_size,
66+
{
67+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_SPACENAME}`]: spaceName,
68+
[`${this.namePrefix}.${NODEJS_HEAP_SPACE_STATE}`]: HeapSpaces.Availabe
69+
}
70+
);
71+
}
72+
});
7773

78-
const data = this._scrapeQueue.shift();
79-
if (data === undefined) return;
80-
for (const space of data) {
81-
const spaceName = space.space_name.substring(
82-
0,
83-
space.space_name.indexOf('_space')
84-
);
85-
observableResult.observe(heapSpaceTotal, space.space_size, {
86-
space: spaceName,
87-
});
88-
observableResult.observe(heapSpaceUsed, space.space_used_size, {
89-
space: spaceName,
90-
});
91-
observableResult.observe(
92-
heapSpaceAvailable,
93-
space.space_available_size,
94-
{ space: spaceName }
95-
);
96-
}
97-
},
98-
[heapSpaceTotal, heapSpaceUsed, heapSpaceAvailable]
99-
);
10074
}
10175

102-
internalEnable(): void {}
76+
internalEnable(): void {
77+
}
10378

104-
internalDisable(): void {}
79+
internalDisable(): void {
80+
}
10581

10682
protected scrape(): HeapSpaceInfo[] {
10783
return v8.getHeapSpaceStatistics();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum HeapSizes {
2+
Total = "total",
3+
Used = "used",
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum HeapSpaces {
2+
Total = "total",
3+
Used = "used",
4+
Availabe = "available"
5+
}

0 commit comments

Comments
 (0)