diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 9c45fb65d0..4aedd33ffe 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -37,6 +37,7 @@ All notable changes to experimental packages in this project will be documented * feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir * feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan * feat(propagator-aws-xray-lambda): add AWS Xray Lambda propagator [4554](https://github.com/open-telemetry/opentelemetry-js/pull/4554) +* refactor(instrumentation-xml-http-request): use exported strings for semantic attributes. [#4681](https://github.com/open-telemetry/opentelemetry-js/pull/4681/files) ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts index 111d68272b..5a8ba3012c 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -22,7 +22,14 @@ import { safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; import { hrTime, isUrlIgnored, otperformance } from '@opentelemetry/core'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_HOST, + SEMATTRS_HTTP_METHOD, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_HTTP_URL, + SEMATTRS_HTTP_USER_AGENT, +} from '@opentelemetry/semantic-conventions'; import { addSpanNetworkEvents, getResource, @@ -156,23 +163,20 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase { // content length comes from the PerformanceTiming resource; this ensures that our // matching logic found the right one assert.ok( - (span.attributes[ - SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH - ] as any) > 0 + (span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH] as any) > 0 ); done(); }, 500); diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index 7a55b5c9d8..f3685e06a1 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -25,7 +25,16 @@ import { } from '@opentelemetry/propagator-b3'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import * as tracing from '@opentelemetry/sdk-trace-base'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_HOST, + SEMATTRS_HTTP_METHOD, + SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, + SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_HTTP_URL, + SEMATTRS_HTTP_USER_AGENT, +} from '@opentelemetry/semantic-conventions'; import { PerformanceTimingNames as PTN, WebTracerProvider, @@ -356,21 +365,21 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[0]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[1]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.ok( (attributes[keys[2]] as number) > 0, - 'attributes ${SemanticAttributess.HTTP_RESPONSE_CONTENT_SIZE} <= 0' + `attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} <= 0` ); assert.strictEqual( attributes[keys[3]], 200, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.strictEqual( attributes[keys[4]], @@ -380,15 +389,15 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[5]], parseUrl(url).host, - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[7]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.strictEqual(keys.length, 8, 'number of attributes is wrong'); @@ -713,7 +722,7 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[1]], secondUrl, - `attribute ${SemanticAttributes.HTTP_URL} is wrong` + `attribute ${SEMATTRS_HTTP_URL} is wrong` ); }); }); @@ -777,9 +786,9 @@ describe('xhr', () => { const attributes = span.attributes; assert.strictEqual( - attributes[SemanticAttributes.HTTP_URL], + attributes[SEMATTRS_HTTP_URL], location.origin + '/get', - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); }); }); @@ -955,22 +964,22 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[0]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[1]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.strictEqual( attributes[keys[2]], 0, - `attributes ${SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH} is wrong` + `attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} is wrong` ); assert.strictEqual( attributes[keys[3]], 400, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.strictEqual( attributes[keys[4]], @@ -980,15 +989,15 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[5]], 'raw.githubusercontent.com', - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[7]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.strictEqual(keys.length, 8, 'number of attributes is wrong'); @@ -1029,17 +1038,17 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[0]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[1]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.strictEqual( attributes[keys[2]], 0, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.strictEqual( attributes[keys[3]], @@ -1049,15 +1058,15 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[4]], 'raw.githubusercontent.com', - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[5]] === 'http' || attributes[keys[5]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[6]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.strictEqual(keys.length, 7, 'number of attributes is wrong'); @@ -1095,17 +1104,17 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[0]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[1]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.strictEqual( attributes[keys[2]], 0, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.strictEqual( attributes[keys[3]], @@ -1115,15 +1124,15 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[4]], 'raw.githubusercontent.com', - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[5]] === 'http' || attributes[keys[5]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[6]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.strictEqual(keys.length, 7, 'number of attributes is wrong'); @@ -1161,17 +1170,17 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[0]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[1]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.strictEqual( attributes[keys[2]], 0, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.strictEqual( attributes[keys[3]], @@ -1181,15 +1190,15 @@ describe('xhr', () => { assert.strictEqual( attributes[keys[4]], 'raw.githubusercontent.com', - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[5]] === 'http' || attributes[keys[5]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[6]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.strictEqual(keys.length, 7, 'number of attributes is wrong');