Skip to content

Commit e5adbf1

Browse files
authored
chore(instrumentation-undici): update semconv usage to modern exports (#3074)
1 parent 5b67c4f commit e5adbf1

File tree

6 files changed

+94
-255
lines changed

6 files changed

+94
-255
lines changed

package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentation-undici/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
},
6363
"dependencies": {
6464
"@opentelemetry/core": "^2.0.0",
65-
"@opentelemetry/instrumentation": "^0.205.0"
65+
"@opentelemetry/instrumentation": "^0.205.0",
66+
"@opentelemetry/semantic-conventions": "^1.24.0"
6667
},
6768
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-undici#readme",
6869
"sideEffects": false

packages/instrumentation-undici/src/enums/SemanticAttributes.ts

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

packages/instrumentation-undici/src/undici.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ import {
3333
trace,
3434
ValueType,
3535
} from '@opentelemetry/api';
36+
import {
37+
hrTime,
38+
hrTimeDuration,
39+
hrTimeToMilliseconds,
40+
} from '@opentelemetry/core';
41+
import {
42+
ATTR_ERROR_TYPE,
43+
ATTR_HTTP_REQUEST_METHOD,
44+
ATTR_HTTP_REQUEST_METHOD_ORIGINAL,
45+
ATTR_HTTP_RESPONSE_STATUS_CODE,
46+
ATTR_NETWORK_PEER_ADDRESS,
47+
ATTR_NETWORK_PEER_PORT,
48+
ATTR_SERVER_ADDRESS,
49+
ATTR_SERVER_PORT,
50+
ATTR_URL_FULL,
51+
ATTR_URL_PATH,
52+
ATTR_URL_QUERY,
53+
ATTR_URL_SCHEME,
54+
ATTR_USER_AGENT_ORIGINAL,
55+
} from '@opentelemetry/semantic-conventions';
3656

3757
/** @knipignore */
3858
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
@@ -45,12 +65,6 @@ import {
4565
ResponseHeadersMessage,
4666
} from './internal-types';
4767
import { UndiciInstrumentationConfig, UndiciRequest } from './types';
48-
import { SemanticAttributes } from './enums/SemanticAttributes';
49-
import {
50-
hrTime,
51-
hrTimeDuration,
52-
hrTimeToMilliseconds,
53-
} from '@opentelemetry/core';
5468

5569
interface InstrumentationRecord {
5670
span: Span;
@@ -244,21 +258,21 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
244258
const urlScheme = requestUrl.protocol.replace(':', '');
245259
const requestMethod = this.getRequestMethod(request.method);
246260
const attributes: Attributes = {
247-
[SemanticAttributes.HTTP_REQUEST_METHOD]: requestMethod,
248-
[SemanticAttributes.HTTP_REQUEST_METHOD_ORIGINAL]: request.method,
249-
[SemanticAttributes.URL_FULL]: requestUrl.toString(),
250-
[SemanticAttributes.URL_PATH]: requestUrl.pathname,
251-
[SemanticAttributes.URL_QUERY]: requestUrl.search,
252-
[SemanticAttributes.URL_SCHEME]: urlScheme,
261+
[ATTR_HTTP_REQUEST_METHOD]: requestMethod,
262+
[ATTR_HTTP_REQUEST_METHOD_ORIGINAL]: request.method,
263+
[ATTR_URL_FULL]: requestUrl.toString(),
264+
[ATTR_URL_PATH]: requestUrl.pathname,
265+
[ATTR_URL_QUERY]: requestUrl.search,
266+
[ATTR_URL_SCHEME]: urlScheme,
253267
};
254268

255269
const schemePorts: Record<string, string> = { https: '443', http: '80' };
256270
const serverAddress = requestUrl.hostname;
257271
const serverPort = requestUrl.port || schemePorts[urlScheme];
258272

259-
attributes[SemanticAttributes.SERVER_ADDRESS] = serverAddress;
273+
attributes[ATTR_SERVER_ADDRESS] = serverAddress;
260274
if (serverPort && !isNaN(Number(serverPort))) {
261-
attributes[SemanticAttributes.SERVER_PORT] = Number(serverPort);
275+
attributes[ATTR_SERVER_PORT] = Number(serverPort);
262276
}
263277

264278
// Get user agent from headers
@@ -272,7 +286,7 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
272286
const userAgent = Array.isArray(userAgentValues)
273287
? userAgentValues[userAgentValues.length - 1]
274288
: userAgentValues;
275-
attributes[SemanticAttributes.USER_AGENT_ORIGINAL] = userAgent;
289+
attributes[ATTR_USER_AGENT_ORIGINAL] = userAgent;
276290
}
277291

278292
// Get attributes from the hook if present
@@ -355,8 +369,8 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
355369
const { span } = record;
356370
const { remoteAddress, remotePort } = socket;
357371
const spanAttributes: Attributes = {
358-
[SemanticAttributes.NETWORK_PEER_ADDRESS]: remoteAddress,
359-
[SemanticAttributes.NETWORK_PEER_PORT]: remotePort,
372+
[ATTR_NETWORK_PEER_ADDRESS]: remoteAddress,
373+
[ATTR_NETWORK_PEER_PORT]: remotePort,
360374
};
361375

362376
// After hooks have been processed (which may modify request headers)
@@ -393,7 +407,7 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
393407

394408
const { span, attributes } = record;
395409
const spanAttributes: Attributes = {
396-
[SemanticAttributes.HTTP_RESPONSE_STATUS_CODE]: response.statusCode,
410+
[ATTR_HTTP_RESPONSE_STATUS_CODE]: response.statusCode,
397411
};
398412

399413
const config = this.getConfig();
@@ -487,7 +501,7 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
487501
this._recordFromReq.delete(request);
488502

489503
// Record metrics (with the error)
490-
attributes[SemanticAttributes.ERROR_TYPE] = error.message;
504+
attributes[ATTR_ERROR_TYPE] = error.message;
491505
this.recordRequestDuration(attributes, startTime);
492506
}
493507

@@ -496,12 +510,12 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
496510
const metricsAttributes: Attributes = {};
497511
// Get the attribs already in span attributes
498512
const keysToCopy = [
499-
SemanticAttributes.HTTP_RESPONSE_STATUS_CODE,
500-
SemanticAttributes.HTTP_REQUEST_METHOD,
501-
SemanticAttributes.SERVER_ADDRESS,
502-
SemanticAttributes.SERVER_PORT,
503-
SemanticAttributes.URL_SCHEME,
504-
SemanticAttributes.ERROR_TYPE,
513+
ATTR_HTTP_RESPONSE_STATUS_CODE,
514+
ATTR_HTTP_REQUEST_METHOD,
515+
ATTR_SERVER_ADDRESS,
516+
ATTR_SERVER_PORT,
517+
ATTR_URL_SCHEME,
518+
ATTR_ERROR_TYPE,
505519
];
506520
keysToCopy.forEach(key => {
507521
if (key in attributes) {

0 commit comments

Comments
 (0)