Skip to content

Commit f9a0d3e

Browse files
refactor(instr-http): use exported strings for semconv (#4573)
* chore(instr-http): use exported strings for semconv * add changelog entry
1 parent 6547440 commit f9a0d3e

File tree

10 files changed

+211
-192
lines changed

10 files changed

+211
-192
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ All notable changes to experimental packages in this project will be documented
2222

2323
### :rocket: (Enhancement)
2424

25+
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
26+
2527
### :bug: (Bug Fix)
2628

2729
* fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540

experimental/packages/opentelemetry-instrumentation-http/src/http.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import {
5858
} from '@opentelemetry/instrumentation';
5959
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
6060
import { errorMonitor } from 'events';
61-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
61+
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
6262

6363
/**
6464
* Http instrumentation instrumentation for Opentelemetry
@@ -746,7 +746,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
746746
code: utils.parseResponseStatus(SpanKind.SERVER, response.statusCode),
747747
});
748748

749-
const route = attributes[SemanticAttributes.HTTP_ROUTE];
749+
const route = attributes[SEMATTRS_HTTP_ROUTE];
750750
if (route) {
751751
span.updateName(`${request.method || 'GET'} ${route}`);
752752
}

experimental/packages/opentelemetry-instrumentation-http/src/utils.ts

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@ import {
2222
SpanKind,
2323
} from '@opentelemetry/api';
2424
import {
25-
NetTransportValues,
26-
SemanticAttributes,
25+
NETTRANSPORTVALUES_IP_TCP,
26+
NETTRANSPORTVALUES_IP_UDP,
27+
SEMATTRS_HTTP_CLIENT_IP,
28+
SEMATTRS_HTTP_FLAVOR,
29+
SEMATTRS_HTTP_HOST,
30+
SEMATTRS_HTTP_METHOD,
31+
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH,
32+
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED,
33+
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH,
34+
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
35+
SEMATTRS_HTTP_ROUTE,
36+
SEMATTRS_HTTP_SCHEME,
37+
SEMATTRS_HTTP_SERVER_NAME,
38+
SEMATTRS_HTTP_STATUS_CODE,
39+
SEMATTRS_HTTP_TARGET,
40+
SEMATTRS_HTTP_URL,
41+
SEMATTRS_HTTP_USER_AGENT,
42+
SEMATTRS_NET_HOST_IP,
43+
SEMATTRS_NET_HOST_NAME,
44+
SEMATTRS_NET_HOST_PORT,
45+
SEMATTRS_NET_PEER_IP,
46+
SEMATTRS_NET_PEER_NAME,
47+
SEMATTRS_NET_PEER_PORT,
48+
SEMATTRS_NET_TRANSPORT,
2749
} from '@opentelemetry/semantic-conventions';
2850
import {
2951
IncomingHttpHeaders,
@@ -167,10 +189,9 @@ export const setRequestContentLengthAttribute = (
167189
if (length === null) return;
168190

169191
if (isCompressed(request.headers)) {
170-
attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH] = length;
192+
attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH] = length;
171193
} else {
172-
attributes[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] =
173-
length;
194+
attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length;
174195
}
175196
};
176197

@@ -187,10 +208,9 @@ export const setResponseContentLengthAttribute = (
187208
if (length === null) return;
188209

189210
if (isCompressed(response.headers)) {
190-
attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH] = length;
211+
attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH] = length;
191212
} else {
192-
attributes[SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] =
193-
length;
213+
attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED] = length;
194214
}
195215
};
196216

@@ -343,20 +363,19 @@ export const getOutgoingRequestAttributes = (
343363
const headers = requestOptions.headers || {};
344364
const userAgent = headers['user-agent'];
345365
const attributes: SpanAttributes = {
346-
[SemanticAttributes.HTTP_URL]: getAbsoluteUrl(
366+
[SEMATTRS_HTTP_URL]: getAbsoluteUrl(
347367
requestOptions,
348368
headers,
349369
`${options.component}:`
350370
),
351-
[SemanticAttributes.HTTP_METHOD]: method,
352-
[SemanticAttributes.HTTP_TARGET]: requestOptions.path || '/',
353-
[SemanticAttributes.NET_PEER_NAME]: hostname,
354-
[SemanticAttributes.HTTP_HOST]:
355-
requestOptions.headers?.host ?? `${hostname}:${port}`,
371+
[SEMATTRS_HTTP_METHOD]: method,
372+
[SEMATTRS_HTTP_TARGET]: requestOptions.path || '/',
373+
[SEMATTRS_NET_PEER_NAME]: hostname,
374+
[SEMATTRS_HTTP_HOST]: requestOptions.headers?.host ?? `${hostname}:${port}`,
356375
};
357376

358377
if (userAgent !== undefined) {
359-
attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent;
378+
attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent;
360379
}
361380
return Object.assign(attributes, options.hookAttributes);
362381
};
@@ -369,10 +388,9 @@ export const getOutgoingRequestMetricAttributes = (
369388
spanAttributes: SpanAttributes
370389
): MetricAttributes => {
371390
const metricAttributes: MetricAttributes = {};
372-
metricAttributes[SemanticAttributes.HTTP_METHOD] =
373-
spanAttributes[SemanticAttributes.HTTP_METHOD];
374-
metricAttributes[SemanticAttributes.NET_PEER_NAME] =
375-
spanAttributes[SemanticAttributes.NET_PEER_NAME];
391+
metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD];
392+
metricAttributes[SEMATTRS_NET_PEER_NAME] =
393+
spanAttributes[SEMATTRS_NET_PEER_NAME];
376394
//TODO: http.url attribute, it should substitute any parameters to avoid high cardinality.
377395
return metricAttributes;
378396
};
@@ -384,11 +402,11 @@ export const getOutgoingRequestMetricAttributes = (
384402
export const getAttributesFromHttpKind = (kind?: string): SpanAttributes => {
385403
const attributes: SpanAttributes = {};
386404
if (kind) {
387-
attributes[SemanticAttributes.HTTP_FLAVOR] = kind;
405+
attributes[SEMATTRS_HTTP_FLAVOR] = kind;
388406
if (kind.toUpperCase() !== 'QUIC') {
389-
attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_TCP;
407+
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_TCP;
390408
} else {
391-
attributes[SemanticAttributes.NET_TRANSPORT] = NetTransportValues.IP_UDP;
409+
attributes[SEMATTRS_NET_TRANSPORT] = NETTRANSPORTVALUES_IP_UDP;
392410
}
393411
}
394412
return attributes;
@@ -406,13 +424,13 @@ export const getOutgoingRequestAttributesOnResponse = (
406424
const attributes: SpanAttributes = {};
407425
if (socket) {
408426
const { remoteAddress, remotePort } = socket;
409-
attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress;
410-
attributes[SemanticAttributes.NET_PEER_PORT] = remotePort;
427+
attributes[SEMATTRS_NET_PEER_IP] = remoteAddress;
428+
attributes[SEMATTRS_NET_PEER_PORT] = remotePort;
411429
}
412430
setResponseContentLengthAttribute(response, attributes);
413431

414432
if (statusCode) {
415-
attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode;
433+
attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode;
416434
attributes[AttributeNames.HTTP_STATUS_TEXT] = (
417435
statusMessage || ''
418436
).toUpperCase();
@@ -430,12 +448,11 @@ export const getOutgoingRequestMetricAttributesOnResponse = (
430448
spanAttributes: SpanAttributes
431449
): MetricAttributes => {
432450
const metricAttributes: MetricAttributes = {};
433-
metricAttributes[SemanticAttributes.NET_PEER_PORT] =
434-
spanAttributes[SemanticAttributes.NET_PEER_PORT];
435-
metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] =
436-
spanAttributes[SemanticAttributes.HTTP_STATUS_CODE];
437-
metricAttributes[SemanticAttributes.HTTP_FLAVOR] =
438-
spanAttributes[SemanticAttributes.HTTP_FLAVOR];
451+
metricAttributes[SEMATTRS_NET_PEER_PORT] =
452+
spanAttributes[SEMATTRS_NET_PEER_PORT];
453+
metricAttributes[SEMATTRS_HTTP_STATUS_CODE] =
454+
spanAttributes[SEMATTRS_HTTP_STATUS_CODE];
455+
metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR];
439456
return metricAttributes;
440457
};
441458

@@ -465,31 +482,31 @@ export const getIncomingRequestAttributes = (
465482
'localhost';
466483
const serverName = options.serverName;
467484
const attributes: SpanAttributes = {
468-
[SemanticAttributes.HTTP_URL]: getAbsoluteUrl(
485+
[SEMATTRS_HTTP_URL]: getAbsoluteUrl(
469486
requestUrl,
470487
headers,
471488
`${options.component}:`
472489
),
473-
[SemanticAttributes.HTTP_HOST]: host,
474-
[SemanticAttributes.NET_HOST_NAME]: hostname,
475-
[SemanticAttributes.HTTP_METHOD]: method,
476-
[SemanticAttributes.HTTP_SCHEME]: options.component,
490+
[SEMATTRS_HTTP_HOST]: host,
491+
[SEMATTRS_NET_HOST_NAME]: hostname,
492+
[SEMATTRS_HTTP_METHOD]: method,
493+
[SEMATTRS_HTTP_SCHEME]: options.component,
477494
};
478495

479496
if (typeof ips === 'string') {
480-
attributes[SemanticAttributes.HTTP_CLIENT_IP] = ips.split(',')[0];
497+
attributes[SEMATTRS_HTTP_CLIENT_IP] = ips.split(',')[0];
481498
}
482499

483500
if (typeof serverName === 'string') {
484-
attributes[SemanticAttributes.HTTP_SERVER_NAME] = serverName;
501+
attributes[SEMATTRS_HTTP_SERVER_NAME] = serverName;
485502
}
486503

487504
if (requestUrl) {
488-
attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.path || '/';
505+
attributes[SEMATTRS_HTTP_TARGET] = requestUrl.path || '/';
489506
}
490507

491508
if (userAgent !== undefined) {
492-
attributes[SemanticAttributes.HTTP_USER_AGENT] = userAgent;
509+
attributes[SEMATTRS_HTTP_USER_AGENT] = userAgent;
493510
}
494511
setRequestContentLengthAttribute(request, attributes);
495512

@@ -506,14 +523,11 @@ export const getIncomingRequestMetricAttributes = (
506523
spanAttributes: SpanAttributes
507524
): MetricAttributes => {
508525
const metricAttributes: MetricAttributes = {};
509-
metricAttributes[SemanticAttributes.HTTP_SCHEME] =
510-
spanAttributes[SemanticAttributes.HTTP_SCHEME];
511-
metricAttributes[SemanticAttributes.HTTP_METHOD] =
512-
spanAttributes[SemanticAttributes.HTTP_METHOD];
513-
metricAttributes[SemanticAttributes.NET_HOST_NAME] =
514-
spanAttributes[SemanticAttributes.NET_HOST_NAME];
515-
metricAttributes[SemanticAttributes.HTTP_FLAVOR] =
516-
spanAttributes[SemanticAttributes.HTTP_FLAVOR];
526+
metricAttributes[SEMATTRS_HTTP_SCHEME] = spanAttributes[SEMATTRS_HTTP_SCHEME];
527+
metricAttributes[SEMATTRS_HTTP_METHOD] = spanAttributes[SEMATTRS_HTTP_METHOD];
528+
metricAttributes[SEMATTRS_NET_HOST_NAME] =
529+
spanAttributes[SEMATTRS_NET_HOST_NAME];
530+
metricAttributes[SEMATTRS_HTTP_FLAVOR] = spanAttributes[SEMATTRS_HTTP_FLAVOR];
517531
//TODO: http.target attribute, it should substitute any parameters to avoid high cardinality.
518532
return metricAttributes;
519533
};
@@ -535,18 +549,18 @@ export const getIncomingRequestAttributesOnResponse = (
535549
const attributes: SpanAttributes = {};
536550
if (socket) {
537551
const { localAddress, localPort, remoteAddress, remotePort } = socket;
538-
attributes[SemanticAttributes.NET_HOST_IP] = localAddress;
539-
attributes[SemanticAttributes.NET_HOST_PORT] = localPort;
540-
attributes[SemanticAttributes.NET_PEER_IP] = remoteAddress;
541-
attributes[SemanticAttributes.NET_PEER_PORT] = remotePort;
552+
attributes[SEMATTRS_NET_HOST_IP] = localAddress;
553+
attributes[SEMATTRS_NET_HOST_PORT] = localPort;
554+
attributes[SEMATTRS_NET_PEER_IP] = remoteAddress;
555+
attributes[SEMATTRS_NET_PEER_PORT] = remotePort;
542556
}
543-
attributes[SemanticAttributes.HTTP_STATUS_CODE] = statusCode;
557+
attributes[SEMATTRS_HTTP_STATUS_CODE] = statusCode;
544558
attributes[AttributeNames.HTTP_STATUS_TEXT] = (
545559
statusMessage || ''
546560
).toUpperCase();
547561

548562
if (rpcMetadata?.type === RPCType.HTTP && rpcMetadata.route !== undefined) {
549-
attributes[SemanticAttributes.HTTP_ROUTE] = rpcMetadata.route;
563+
attributes[SEMATTRS_HTTP_ROUTE] = rpcMetadata.route;
550564
}
551565
return attributes;
552566
};
@@ -559,13 +573,12 @@ export const getIncomingRequestMetricAttributesOnResponse = (
559573
spanAttributes: SpanAttributes
560574
): MetricAttributes => {
561575
const metricAttributes: MetricAttributes = {};
562-
metricAttributes[SemanticAttributes.HTTP_STATUS_CODE] =
563-
spanAttributes[SemanticAttributes.HTTP_STATUS_CODE];
564-
metricAttributes[SemanticAttributes.NET_HOST_PORT] =
565-
spanAttributes[SemanticAttributes.NET_HOST_PORT];
566-
if (spanAttributes[SemanticAttributes.HTTP_ROUTE] !== undefined) {
567-
metricAttributes[SemanticAttributes.HTTP_ROUTE] =
568-
spanAttributes[SemanticAttributes.HTTP_ROUTE];
576+
metricAttributes[SEMATTRS_HTTP_STATUS_CODE] =
577+
spanAttributes[SEMATTRS_HTTP_STATUS_CODE];
578+
metricAttributes[SEMATTRS_NET_HOST_PORT] =
579+
spanAttributes[SEMATTRS_NET_HOST_PORT];
580+
if (spanAttributes[SEMATTRS_HTTP_ROUTE] !== undefined) {
581+
metricAttributes[SEMATTRS_HTTP_ROUTE] = spanAttributes[SEMATTRS_HTTP_ROUTE];
569582
}
570583
return metricAttributes;
571584
};

experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ import {
3030
SimpleSpanProcessor,
3131
} from '@opentelemetry/sdk-trace-base';
3232
import {
33-
NetTransportValues,
34-
SemanticAttributes,
33+
NETTRANSPORTVALUES_IP_TCP,
34+
SEMATTRS_HTTP_CLIENT_IP,
35+
SEMATTRS_HTTP_FLAVOR,
36+
SEMATTRS_HTTP_ROUTE,
37+
SEMATTRS_HTTP_STATUS_CODE,
38+
SEMATTRS_NET_HOST_PORT,
39+
SEMATTRS_NET_PEER_PORT,
40+
SEMATTRS_NET_TRANSPORT,
3541
} from '@opentelemetry/semantic-conventions';
3642
import * as assert from 'assert';
3743
import * as nock from 'nock';
@@ -211,11 +217,11 @@ describe('HttpInstrumentation', () => {
211217
assertSpan(incomingSpan, SpanKind.SERVER, validations);
212218
assertSpan(outgoingSpan, SpanKind.CLIENT, validations);
213219
assert.strictEqual(
214-
incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT],
220+
incomingSpan.attributes[SEMATTRS_NET_HOST_PORT],
215221
serverPort
216222
);
217223
assert.strictEqual(
218-
outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT],
224+
outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT],
219225
serverPort
220226
);
221227
});
@@ -329,28 +335,25 @@ describe('HttpInstrumentation', () => {
329335

330336
assert.strictEqual(spans.length, 2);
331337
assert.strictEqual(
332-
incomingSpan.attributes[SemanticAttributes.HTTP_CLIENT_IP],
338+
incomingSpan.attributes[SEMATTRS_HTTP_CLIENT_IP],
333339
'<client>'
334340
);
335341
assert.strictEqual(
336-
incomingSpan.attributes[SemanticAttributes.NET_HOST_PORT],
342+
incomingSpan.attributes[SEMATTRS_NET_HOST_PORT],
337343
serverPort
338344
);
339345
assert.strictEqual(
340-
outgoingSpan.attributes[SemanticAttributes.NET_PEER_PORT],
346+
outgoingSpan.attributes[SEMATTRS_NET_PEER_PORT],
341347
serverPort
342348
);
343349
[
344350
{ span: incomingSpan, kind: SpanKind.SERVER },
345351
{ span: outgoingSpan, kind: SpanKind.CLIENT },
346352
].forEach(({ span, kind }) => {
353+
assert.strictEqual(span.attributes[SEMATTRS_HTTP_FLAVOR], '1.1');
347354
assert.strictEqual(
348-
span.attributes[SemanticAttributes.HTTP_FLAVOR],
349-
'1.1'
350-
);
351-
assert.strictEqual(
352-
span.attributes[SemanticAttributes.NET_TRANSPORT],
353-
NetTransportValues.IP_TCP
355+
span.attributes[SEMATTRS_NET_TRANSPORT],
356+
NETTRANSPORTVALUES_IP_TCP
354357
);
355358
assertSpan(span, kind, validations);
356359
});
@@ -363,10 +366,7 @@ describe('HttpInstrumentation', () => {
363366
const span = memoryExporter.getFinishedSpans()[0];
364367

365368
assert.strictEqual(span.kind, SpanKind.SERVER);
366-
assert.strictEqual(
367-
span.attributes[SemanticAttributes.HTTP_ROUTE],
368-
'TheRoute'
369-
);
369+
assert.strictEqual(span.attributes[SEMATTRS_HTTP_ROUTE], 'TheRoute');
370370
assert.strictEqual(span.name, 'GET TheRoute');
371371
});
372372

@@ -796,10 +796,7 @@ describe('HttpInstrumentation', () => {
796796
const [span] = spans;
797797
assert.strictEqual(spans.length, 1);
798798
assert.ok(Object.keys(span.attributes).length > 6);
799-
assert.strictEqual(
800-
span.attributes[SemanticAttributes.HTTP_STATUS_CODE],
801-
404
802-
);
799+
assert.strictEqual(span.attributes[SEMATTRS_HTTP_STATUS_CODE], 404);
803800
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
804801
done();
805802
});

0 commit comments

Comments
 (0)