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 4 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.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
### :bug: (Bug Fix)

* fix(sdk-trace-base): do not load OTEL_ env vars on module load, but when needed [#5224](https://github.com/open-telemetry/opentelemetry-js/pull/5224)
* fix(instrumentation-xhr, instrumentation-fetch): content length attributes no longer get removed with `ignoreNetworkEvents: true` being set [#5229](https://github.com/open-telemetry/opentelemetry-js/issues/5229)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for our confusing changelog setup :/
this should go into CHANGELOG_NEXT.md on this branch.

The fix from main will already be included once this version is released (February), so you can just document the breaking change to sdk-trace-web as feat(sdk-trace-web)!: ... 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if it looks good now. I had a bunch of meetings but am free now.


### :books: (Refine Doc)

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