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

fix(instrumentation-fetch, instrumentation-xml-http-request) content length attributes missing #5257

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna
* refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna
* refactor(sdk-trace-base)!: make `resource` property private in `BasicTracerProvider` and remove `getActiveSpanProcessor` API. [#5192](https://github.com/open-telemetry/opentelemetry-js/pull/5192) @david-luna
* feat(sdk-trace-web)!: content length attributes are no longer added in `addSpanNetworkEvents`. Logic has been moved to new method `addSpanContentLengthAttributes` [#5257](https://github.com/open-telemetry/opentelemetry-js/pull/5257) @arriIsHere

### :rocket: (Enhancement)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
if (!this.getConfig().ignoreNetworkEvents) {
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
}
web.addSpanContentLengthAttributes(childSpan, corsPreFlightRequest);
childSpan.end(
corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]
);
Expand Down Expand Up @@ -265,6 +266,7 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
if (!this.getConfig().ignoreNetworkEvents) {
web.addSpanNetworkEvents(span, mainRequest);
}
web.addSpanContentLengthAttributes(span, mainRequest);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,5 +1211,18 @@ describe('fetch', () => {
const events = span.events;
assert.strictEqual(events.length, 0, 'number of events is wrong');
});

it('should still add the CONTENT_LENGTH attribute', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
] as number;
assert.strictEqual(
responseContentLength,
30,
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
PerformanceTimingNames as PTN,
shouldPropagateTraceHeaders,
parseUrl,
addSpanContentLengthAttributes,
} from '@opentelemetry/sdk-trace-web';
import { EventNames } from './enums/EventNames';
import {
Expand Down Expand Up @@ -152,6 +153,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
if (!this.getConfig().ignoreNetworkEvents) {
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
}
addSpanContentLengthAttributes(childSpan, corsPreFlightRequest);
childSpan.end(corsPreFlightRequest[PTN.RESPONSE_END]);
});
}
Expand Down Expand Up @@ -303,6 +305,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
if (!this.getConfig().ignoreNetworkEvents) {
addSpanNetworkEvents(span, mainRequest);
}
addSpanContentLengthAttributes(span, mainRequest);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,19 @@ describe('xhr', () => {
const events = span.events;
assert.strictEqual(events.length, 3, 'number of events is wrong');
});

it('should still add the CONTENT_LENGTH attribute', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
] as number;
assert.strictEqual(
responseContentLength,
30,
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);
});
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-sdk-trace-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
URLLike,
addSpanNetworkEvent,
addSpanNetworkEvents,
addSpanContentLengthAttributes,
getElementXPath,
getResource,
hasKey,
Expand Down
11 changes: 11 additions & 0 deletions packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ export function addSpanNetworkEvents(
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
}

/**
* Helper function for adding content length attributes to a network span
* @param span
* @param resource
*/
export function addSpanContentLengthAttributes(
span: api.Span,
resource: PerformanceEntries
): void {
const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
if (encodedLength !== undefined) {
span.setAttribute(SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, encodedLength);
Expand Down
Loading