diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 60a15df6c2..a4806e6546 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -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) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index e5d9a84bde..6f847e78f5 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -115,6 +115,7 @@ export class FetchInstrumentation extends InstrumentationBase { 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` + ); + }); }); }); 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 6d2eafa054..ce51f6b401 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -37,6 +37,7 @@ import { PerformanceTimingNames as PTN, shouldPropagateTraceHeaders, parseUrl, + addSpanContentLengthAttributes, } from '@opentelemetry/sdk-trace-web'; import { EventNames } from './enums/EventNames'; import { @@ -152,6 +153,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase { 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` + ); + }); }); }); diff --git a/packages/opentelemetry-sdk-trace-web/src/index.ts b/packages/opentelemetry-sdk-trace-web/src/index.ts index 017b934a8f..25101e1f8c 100644 --- a/packages/opentelemetry-sdk-trace-web/src/index.ts +++ b/packages/opentelemetry-sdk-trace-web/src/index.ts @@ -27,6 +27,7 @@ export { URLLike, addSpanNetworkEvent, addSpanNetworkEvents, + addSpanContentLengthAttributes, getElementXPath, getResource, hasKey, diff --git a/packages/opentelemetry-sdk-trace-web/src/utils.ts b/packages/opentelemetry-sdk-trace-web/src/utils.ts index 5fcd9abf4e..e8d7bdb513 100644 --- a/packages/opentelemetry-sdk-trace-web/src/utils.ts +++ b/packages/opentelemetry-sdk-trace-web/src/utils.ts @@ -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);