Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(instrumentation-xhr): use exported strings for semantic attr… #4681

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -156,23 +163,20 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
if (typeof spanUrl === 'string') {
const parsedUrl = parseUrl(spanUrl);
if (xhrMem.status !== undefined) {
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, xhrMem.status);
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, xhrMem.status);
}
if (xhrMem.statusText !== undefined) {
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, xhrMem.statusText);
}
span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
span.setAttribute(SEMATTRS_HTTP_HOST, parsedUrl.host);
span.setAttribute(
SemanticAttributes.HTTP_SCHEME,
SEMATTRS_HTTP_SCHEME,
parsedUrl.protocol.replace(':', '')
);

// @TODO do we want to collect this or it will be collected earlier once only or
// maybe when parent span is not available ?
span.setAttribute(
SemanticAttributes.HTTP_USER_AGENT,
navigator.userAgent
);
span.setAttribute(SEMATTRS_HTTP_USER_AGENT, navigator.userAgent);
}
}

Expand Down Expand Up @@ -336,8 +340,8 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
const currentSpan = this.tracer.startSpan(spanName, {
kind: api.SpanKind.CLIENT,
attributes: {
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_URL]: parseUrl(url).toString(),
[SEMATTRS_HTTP_METHOD]: method,
[SEMATTRS_HTTP_URL]: parseUrl(url).toString(),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { Span } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH } from '@opentelemetry/semantic-conventions';
import { ReadableSpan, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import { XMLHttpRequestInstrumentation } from '../src';
Expand Down Expand Up @@ -65,9 +65,7 @@ describe('unmocked xhr', () => {
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
JamieDanielson marked this conversation as resolved.
Show resolved Hide resolved
(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]],
Expand All @@ -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');
Expand Down Expand Up @@ -713,7 +722,7 @@ describe('xhr', () => {
assert.strictEqual(
attributes[keys[1]],
secondUrl,
`attribute ${SemanticAttributes.HTTP_URL} is wrong`
`attribute ${SEMATTRS_HTTP_URL} is wrong`
);
});
});
Expand Down Expand Up @@ -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`
);
});
});
Expand Down Expand Up @@ -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]],
Expand All @@ -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');
Expand Down Expand Up @@ -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]],
Expand All @@ -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');
Expand Down Expand Up @@ -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]],
Expand All @@ -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');
Expand Down Expand Up @@ -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]],
Expand All @@ -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');
Expand Down