Skip to content

Commit e4d9c21

Browse files
authored
fix(instrumentation-fetch, instrumentation-xml-http-request) content length attributes now prese (#5230)
1 parent bdee949 commit e4d9c21

File tree

7 files changed

+96
-27
lines changed

7 files changed

+96
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
1919

2020
* 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)
2121

22+
* 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)
23+
2224
### :books: (Refine Doc)
2325

2426
### :house: (Internal)

experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
112112
},
113113
api.trace.setSpan(api.context.active(), span)
114114
);
115-
if (!this.getConfig().ignoreNetworkEvents) {
116-
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
117-
}
115+
web.addSpanNetworkEvents(
116+
childSpan,
117+
corsPreFlightRequest,
118+
this.getConfig().ignoreNetworkEvents
119+
);
118120
childSpan.end(
119121
corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]
120122
);
@@ -262,9 +264,11 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
262264
this._addChildSpan(span, corsPreFlightRequest);
263265
this._markResourceAsUsed(corsPreFlightRequest);
264266
}
265-
if (!this.getConfig().ignoreNetworkEvents) {
266-
web.addSpanNetworkEvents(span, mainRequest);
267-
}
267+
web.addSpanNetworkEvents(
268+
span,
269+
mainRequest,
270+
this.getConfig().ignoreNetworkEvents
271+
);
268272
}
269273
}
270274

experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,5 +1212,18 @@ describe('fetch', () => {
12121212
const events = span.events;
12131213
assert.strictEqual(events.length, 0, 'number of events is wrong');
12141214
});
1215+
1216+
it('should still add the CONTENT_LENGTH attribute', () => {
1217+
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
1218+
const attributes = span.attributes;
1219+
const responseContentLength = attributes[
1220+
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
1221+
] as number;
1222+
assert.strictEqual(
1223+
responseContentLength,
1224+
30,
1225+
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
1226+
);
1227+
});
12151228
});
12161229
});

experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
149149
const childSpan = this.tracer.startSpan('CORS Preflight', {
150150
startTime: corsPreFlightRequest[PTN.FETCH_START],
151151
});
152-
if (!this.getConfig().ignoreNetworkEvents) {
153-
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
154-
}
152+
addSpanNetworkEvents(
153+
childSpan,
154+
corsPreFlightRequest,
155+
this.getConfig().ignoreNetworkEvents
156+
);
155157
childSpan.end(corsPreFlightRequest[PTN.RESPONSE_END]);
156158
});
157159
}
@@ -300,9 +302,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
300302
this._addChildSpan(span, corsPreFlightRequest);
301303
this._markResourceAsUsed(corsPreFlightRequest);
302304
}
303-
if (!this.getConfig().ignoreNetworkEvents) {
304-
addSpanNetworkEvents(span, mainRequest);
305-
}
305+
addSpanNetworkEvents(
306+
span,
307+
mainRequest,
308+
this.getConfig().ignoreNetworkEvents
309+
);
306310
}
307311
}
308312

experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,19 @@ describe('xhr', () => {
857857
const events = span.events;
858858
assert.strictEqual(events.length, 3, 'number of events is wrong');
859859
});
860+
861+
it('should still add the CONTENT_LENGTH attribute', () => {
862+
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
863+
const attributes = span.attributes;
864+
const responseContentLength = attributes[
865+
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
866+
] as number;
867+
assert.strictEqual(
868+
responseContentLength,
869+
30,
870+
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
871+
);
872+
});
860873
});
861874
});
862875

packages/opentelemetry-sdk-trace-web/src/utils.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,32 @@ export function addSpanNetworkEvent(
8989
}
9090

9191
/**
92-
* Helper function for adding network events
92+
* Helper function for adding network events and content length attributes
9393
* @param span
9494
* @param resource
95+
* @param ignoreNetworkEvents
9596
*/
9697
export function addSpanNetworkEvents(
9798
span: api.Span,
98-
resource: PerformanceEntries
99+
resource: PerformanceEntries,
100+
ignoreNetworkEvents = false
99101
): void {
100-
addSpanNetworkEvent(span, PTN.FETCH_START, resource);
101-
addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_START, resource);
102-
addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_END, resource);
103-
addSpanNetworkEvent(span, PTN.CONNECT_START, resource);
104-
if (
105-
hasKey(resource as PerformanceResourceTiming, 'name') &&
106-
(resource as PerformanceResourceTiming)['name'].startsWith('https:')
107-
) {
108-
addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource);
102+
if (!ignoreNetworkEvents) {
103+
addSpanNetworkEvent(span, PTN.FETCH_START, resource);
104+
addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_START, resource);
105+
addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_END, resource);
106+
addSpanNetworkEvent(span, PTN.CONNECT_START, resource);
107+
if (
108+
hasKey(resource as PerformanceResourceTiming, 'name') &&
109+
(resource as PerformanceResourceTiming)['name'].startsWith('https:')
110+
) {
111+
addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource);
112+
}
113+
addSpanNetworkEvent(span, PTN.CONNECT_END, resource);
114+
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
115+
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
116+
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
109117
}
110-
addSpanNetworkEvent(span, PTN.CONNECT_END, resource);
111-
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
112-
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
113-
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
114118
const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
115119
if (encodedLength !== undefined) {
116120
span.setAttribute(SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH, encodedLength);

packages/opentelemetry-sdk-trace-web/test/utils.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,35 @@ describe('utils', () => {
127127
} as PerformanceResourceTiming);
128128
assert.strictEqual(addEventSpy.callCount, 9);
129129
});
130+
131+
it('should ignore network events when ignoreNetworkEvents is true', () => {
132+
const addEventSpy = sinon.spy();
133+
const setAttributeSpy = sinon.spy();
134+
const span = {
135+
addEvent: addEventSpy,
136+
setAttribute: setAttributeSpy,
137+
} as unknown as tracing.Span;
138+
const entries = {
139+
[PTN.FETCH_START]: 123,
140+
[PTN.DOMAIN_LOOKUP_START]: 123,
141+
[PTN.DOMAIN_LOOKUP_END]: 123,
142+
[PTN.CONNECT_START]: 123,
143+
[PTN.SECURE_CONNECTION_START]: 123,
144+
[PTN.CONNECT_END]: 123,
145+
[PTN.REQUEST_START]: 123,
146+
[PTN.RESPONSE_START]: 123,
147+
[PTN.RESPONSE_END]: 123,
148+
[PTN.DECODED_BODY_SIZE]: 123,
149+
[PTN.ENCODED_BODY_SIZE]: 61,
150+
} as PerformanceEntries;
151+
152+
assert.strictEqual(addEventSpy.callCount, 0);
153+
154+
addSpanNetworkEvents(span, entries, true);
155+
assert.strictEqual(setAttributeSpy.callCount, 2);
156+
//secure connect start should not be added to non-https resource
157+
assert.strictEqual(addEventSpy.callCount, 0);
158+
});
130159
it('should only include encoded size when content encoding is being used', () => {
131160
const addEventSpy = sinon.spy();
132161
const setAttributeSpy = sinon.spy();

0 commit comments

Comments
 (0)