Skip to content

Commit e72c1b3

Browse files
authored
chore(instrumentation-runtime-node): update semconv usage to modern exports (#3130)
Refs: #2377 Refs: open-telemetry/opentelemetry-js#5956
1 parent ffe8485 commit e72c1b3

14 files changed

+315
-268
lines changed

packages/instrumentation-runtime-node/src/consts/attributes.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { EventLoopUtilizationCollector } from './metrics/eventLoopUtilizationCol
2121
import { EventLoopDelayCollector } from './metrics/eventLoopDelayCollector';
2222
import { GCCollector } from './metrics/gcCollector';
2323
import { HeapSpacesSizeAndUsedCollector } from './metrics/heapSpacesSizeAndUsedCollector';
24-
import { ConventionalNamePrefix } from './types/ConventionalNamePrefix';
2524
import { EventLoopTimeCollector } from './metrics/eventLoopTimeCollector';
2625
/** @knipignore */
2726
import { PACKAGE_VERSION, PACKAGE_NAME } from './version';
@@ -40,17 +39,11 @@ export class RuntimeNodeInstrumentation extends InstrumentationBase<RuntimeNodeI
4039
Object.assign({}, DEFAULT_CONFIG, config)
4140
);
4241
this._collectors = [
43-
new EventLoopUtilizationCollector(
44-
this._config,
45-
ConventionalNamePrefix.NodeJs
46-
),
47-
new EventLoopTimeCollector(this._config, ConventionalNamePrefix.NodeJs),
48-
new EventLoopDelayCollector(this._config, ConventionalNamePrefix.NodeJs),
49-
new GCCollector(this._config, ConventionalNamePrefix.V8js),
50-
new HeapSpacesSizeAndUsedCollector(
51-
this._config,
52-
ConventionalNamePrefix.V8js
53-
),
42+
new EventLoopUtilizationCollector(this._config),
43+
new EventLoopTimeCollector(this._config),
44+
new EventLoopDelayCollector(this._config),
45+
new GCCollector(this._config),
46+
new HeapSpacesSizeAndUsedCollector(this._config),
5447
];
5548
if (this._config.enabled) {
5649
for (const collector of this._collectors) {

packages/instrumentation-runtime-node/src/metrics/baseCollector.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@ import { RuntimeNodeInstrumentationConfig } from '../types';
2020
export abstract class BaseCollector implements MetricCollector {
2121
protected _config: RuntimeNodeInstrumentationConfig = {};
2222

23-
protected namePrefix: string;
24-
25-
protected constructor(
26-
config: RuntimeNodeInstrumentationConfig = {},
27-
namePrefix: string
28-
) {
23+
constructor(config: RuntimeNodeInstrumentationConfig = {}) {
2924
this._config = config;
30-
this.namePrefix = namePrefix;
3125
}
3226

3327
public disable(): void {

packages/instrumentation-runtime-node/src/metrics/eventLoopDelayCollector.ts

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,21 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { RuntimeNodeInstrumentationConfig } from '../types';
1716
import { Meter } from '@opentelemetry/api';
1817
import * as perf_hooks from 'node:perf_hooks';
1918
import { IntervalHistogram } from 'node:perf_hooks';
20-
import { BaseCollector } from './baseCollector';
21-
22-
enum NodeJsEventLoopDelayAttributes {
23-
min = 'eventloop.delay.min',
24-
max = 'eventloop.delay.max',
25-
mean = 'eventloop.delay.mean',
26-
stddev = 'eventloop.delay.stddev',
27-
p50 = 'eventloop.delay.p50',
28-
p90 = 'eventloop.delay.p90',
29-
p99 = 'eventloop.delay.p99',
30-
}
19+
import {
20+
METRIC_NODEJS_EVENTLOOP_DELAY_MAX,
21+
METRIC_NODEJS_EVENTLOOP_DELAY_MEAN,
22+
METRIC_NODEJS_EVENTLOOP_DELAY_MIN,
23+
METRIC_NODEJS_EVENTLOOP_DELAY_P50,
24+
METRIC_NODEJS_EVENTLOOP_DELAY_P90,
25+
METRIC_NODEJS_EVENTLOOP_DELAY_P99,
26+
METRIC_NODEJS_EVENTLOOP_DELAY_STDDEV,
27+
} from '../semconv';
3128

32-
export const metricNames: Record<
33-
NodeJsEventLoopDelayAttributes,
34-
{ description: string }
35-
> = {
36-
[NodeJsEventLoopDelayAttributes.min]: {
37-
description: 'Event loop minimum delay.',
38-
},
39-
[NodeJsEventLoopDelayAttributes.max]: {
40-
description: 'Event loop maximum delay.',
41-
},
42-
[NodeJsEventLoopDelayAttributes.mean]: {
43-
description: 'Event loop mean delay.',
44-
},
45-
[NodeJsEventLoopDelayAttributes.stddev]: {
46-
description: 'Event loop standard deviation delay.',
47-
},
48-
[NodeJsEventLoopDelayAttributes.p50]: {
49-
description: 'Event loop 50 percentile delay.',
50-
},
51-
[NodeJsEventLoopDelayAttributes.p90]: {
52-
description: 'Event loop 90 percentile delay.',
53-
},
54-
[NodeJsEventLoopDelayAttributes.p99]: {
55-
description: 'Event loop 99 percentile delay.',
56-
},
57-
};
29+
import type { RuntimeNodeInstrumentationConfig } from '../types';
30+
import { BaseCollector } from './baseCollector';
5831

5932
export interface EventLoopLagInformation {
6033
min: number;
@@ -69,70 +42,60 @@ export interface EventLoopLagInformation {
6942
export class EventLoopDelayCollector extends BaseCollector {
7043
private _histogram: IntervalHistogram;
7144

72-
constructor(
73-
config: RuntimeNodeInstrumentationConfig = {},
74-
namePrefix: string
75-
) {
76-
super(config, namePrefix);
45+
constructor(config: RuntimeNodeInstrumentationConfig = {}) {
46+
super(config);
7747
this._histogram = perf_hooks.monitorEventLoopDelay({
7848
resolution: config.monitoringPrecision,
7949
});
8050
}
8151

8252
updateMetricInstruments(meter: Meter): void {
8353
const delayMin = meter.createObservableGauge(
84-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.min}`,
54+
METRIC_NODEJS_EVENTLOOP_DELAY_MIN,
8555
{
86-
description:
87-
metricNames[NodeJsEventLoopDelayAttributes.min].description,
56+
description: 'Event loop minimum delay.',
8857
unit: 's',
8958
}
9059
);
9160
const delayMax = meter.createObservableGauge(
92-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.max}`,
61+
METRIC_NODEJS_EVENTLOOP_DELAY_MAX,
9362
{
94-
description:
95-
metricNames[NodeJsEventLoopDelayAttributes.max].description,
63+
description: 'Event loop maximum delay.',
9664
unit: 's',
9765
}
9866
);
9967
const delayMean = meter.createObservableGauge(
100-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.mean}`,
68+
METRIC_NODEJS_EVENTLOOP_DELAY_MEAN,
10169
{
102-
description:
103-
metricNames[NodeJsEventLoopDelayAttributes.mean].description,
70+
description: 'Event loop mean delay.',
10471
unit: 's',
10572
}
10673
);
10774
const delayStddev = meter.createObservableGauge(
108-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.stddev}`,
75+
METRIC_NODEJS_EVENTLOOP_DELAY_STDDEV,
10976
{
110-
description:
111-
metricNames[NodeJsEventLoopDelayAttributes.stddev].description,
77+
description: 'Event loop standard deviation delay.',
11278
unit: 's',
11379
}
11480
);
11581
const delayp50 = meter.createObservableGauge(
116-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.p50}`,
82+
METRIC_NODEJS_EVENTLOOP_DELAY_P50,
11783
{
118-
description:
119-
metricNames[NodeJsEventLoopDelayAttributes.p50].description,
84+
description: 'Event loop 50 percentile delay.',
12085
unit: 's',
12186
}
12287
);
12388
const delayp90 = meter.createObservableGauge(
124-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.p90}`,
89+
METRIC_NODEJS_EVENTLOOP_DELAY_P90,
12590
{
126-
description:
127-
metricNames[NodeJsEventLoopDelayAttributes.p90].description,
91+
description: 'Event loop 90 percentile delay.',
12892
unit: 's',
12993
}
13094
);
13195
const delayp99 = meter.createObservableGauge(
132-
`${this.namePrefix}.${NodeJsEventLoopDelayAttributes.p99}`,
96+
METRIC_NODEJS_EVENTLOOP_DELAY_P99,
13397
{
134-
description:
135-
metricNames[NodeJsEventLoopDelayAttributes.p99].description,
98+
description: 'Event loop 99 percentile delay.',
13699
unit: 's',
137100
}
138101
);

packages/instrumentation-runtime-node/src/metrics/eventLoopTimeCollector.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import { EventLoopUtilization, performance } from 'node:perf_hooks';
17-
import { RuntimeNodeInstrumentationConfig } from '../types';
1818
import { Meter } from '@opentelemetry/api';
1919
import { BaseCollector } from './baseCollector';
20+
import {
21+
METRIC_NODEJS_EVENTLOOP_TIME,
22+
ATTR_NODEJS_EVENTLOOP_STATE,
23+
NODEJS_EVENTLOOP_STATE_VALUE_ACTIVE,
24+
NODEJS_EVENTLOOP_STATE_VALUE_IDLE,
25+
} from '../semconv';
2026

2127
const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;
2228

23-
export const ATTR_NODEJS_EVENT_LOOP_TIME = 'eventloop.time';
24-
2529
export class EventLoopTimeCollector extends BaseCollector {
26-
constructor(
27-
config: RuntimeNodeInstrumentationConfig = {},
28-
namePrefix: string
29-
) {
30-
super(config, namePrefix);
31-
}
32-
3330
public updateMetricInstruments(meter: Meter): void {
3431
const timeCounter = meter.createObservableCounter(
35-
`${this.namePrefix}.${ATTR_NODEJS_EVENT_LOOP_TIME}`,
32+
METRIC_NODEJS_EVENTLOOP_TIME,
3633
{
3734
description:
3835
'Cumulative duration of time the event loop has been in each state.',
@@ -48,10 +45,10 @@ export class EventLoopTimeCollector extends BaseCollector {
4845
if (data === undefined) return;
4946

5047
observableResult.observe(timeCounter, data.active / 1000, {
51-
[`${this.namePrefix}.eventloop.state`]: 'active',
48+
[ATTR_NODEJS_EVENTLOOP_STATE]: NODEJS_EVENTLOOP_STATE_VALUE_ACTIVE,
5249
});
5350
observableResult.observe(timeCounter, data.idle / 1000, {
54-
[`${this.namePrefix}.eventloop.state`]: 'idle',
51+
[ATTR_NODEJS_EVENTLOOP_STATE]: NODEJS_EVENTLOOP_STATE_VALUE_IDLE,
5552
});
5653
},
5754
[timeCounter]

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import { EventLoopUtilization, performance } from 'node:perf_hooks';
17-
import { RuntimeNodeInstrumentationConfig } from '../types';
1818
import { Meter } from '@opentelemetry/api';
1919
import { BaseCollector } from './baseCollector';
20+
import { METRIC_NODEJS_EVENTLOOP_UTILIZATION } from '../semconv';
2021

2122
const { eventLoopUtilization: eventLoopUtilizationCollector } = performance;
2223

23-
export const ATTR_NODEJS_EVENT_LOOP_UTILIZATION = 'eventloop.utilization';
24-
2524
export class EventLoopUtilizationCollector extends BaseCollector {
2625
private _lastValue?: EventLoopUtilization;
2726

28-
constructor(
29-
config: RuntimeNodeInstrumentationConfig = {},
30-
namePrefix: string
31-
) {
32-
super(config, namePrefix);
33-
}
34-
3527
public updateMetricInstruments(meter: Meter): void {
3628
meter
37-
.createObservableGauge(
38-
`${this.namePrefix}.${ATTR_NODEJS_EVENT_LOOP_UTILIZATION}`,
39-
{
40-
description: 'Event loop utilization',
41-
unit: '1',
42-
}
43-
)
29+
.createObservableGauge(METRIC_NODEJS_EVENTLOOP_UTILIZATION, {
30+
description: 'Event loop utilization',
31+
unit: '1',
32+
})
4433
.addCallback(async observableResult => {
4534
if (!this._config.enabled) return;
4635

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { RuntimeNodeInstrumentationConfig } from '../types';
16+
17+
import * as perf_hooks from 'node:perf_hooks';
18+
import { PerformanceObserver } from 'node:perf_hooks';
1719
import { Meter } from '@opentelemetry/api';
20+
import { RuntimeNodeInstrumentationConfig } from '../types';
1821
import { Histogram, ValueType } from '@opentelemetry/api';
1922
import { BaseCollector } from './baseCollector';
20-
import * as perf_hooks from 'node:perf_hooks';
21-
import { PerformanceObserver } from 'node:perf_hooks';
23+
import { ATTR_V8JS_GC_TYPE, METRIC_V8JS_GC_DURATION } from '../semconv';
2224

23-
const ATTR_NODEJS_GC_DURATION_SECONDS = 'gc.duration';
2425
const DEFAULT_GC_DURATION_BUCKETS = [0.01, 0.1, 1, 10];
2526

2627
const kinds: string[] = [];
@@ -33,11 +34,8 @@ export class GCCollector extends BaseCollector {
3334
private _gcDurationByKindHistogram?: Histogram;
3435
private _observer: PerformanceObserver;
3536

36-
constructor(
37-
config: RuntimeNodeInstrumentationConfig = {},
38-
namePrefix: string
39-
) {
40-
super(config, namePrefix);
37+
constructor(config: RuntimeNodeInstrumentationConfig = {}) {
38+
super(config);
4139
this._observer = new perf_hooks.PerformanceObserver(list => {
4240
if (!this._config.enabled) return;
4341

@@ -48,16 +46,15 @@ export class GCCollector extends BaseCollector {
4846
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4947
// @ts-ignore
5048
const kind = entry.detail ? kinds[entry.detail.kind] : kinds[entry.kind];
51-
this._gcDurationByKindHistogram?.record(
52-
entry.duration / 1000,
53-
Object.assign({ [`${this.namePrefix}.gc.type`]: kind })
54-
);
49+
this._gcDurationByKindHistogram?.record(entry.duration / 1000, {
50+
[ATTR_V8JS_GC_TYPE]: kind,
51+
});
5552
});
5653
}
5754

5855
updateMetricInstruments(meter: Meter): void {
5956
this._gcDurationByKindHistogram = meter.createHistogram(
60-
`${this.namePrefix}.${ATTR_NODEJS_GC_DURATION_SECONDS}`,
57+
METRIC_V8JS_GC_DURATION,
6158
{
6259
description:
6360
'Garbage collection duration by kind, one of major, minor, incremental or weakcb.',

0 commit comments

Comments
 (0)