Skip to content

Commit 900b7d8

Browse files
feat(opentelemetry-instrumentation-xhr): optionally ignore network events (#4571)
1 parent f6a075b commit 900b7d8

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ All notable changes to experimental packages in this project will be documented
2424

2525
### :rocket: (Enhancement)
2626

27+
* feat(opentelemetry-instrumentation-xhr): optionally ignore network events [#4571](https://github.com/open-telemetry/opentelemetry-js/pull/4571/) @mustafahaddara
2728
* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
2829
* feat(sdk-node): add `HostDetector` as default resource detector
2930

experimental/packages/opentelemetry-instrumentation-xml-http-request/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ req.send();
6464

6565
```
6666

67+
### XHR Instrumentation options
68+
69+
XHR instrumentation plugin has few options available to choose from. You can set the following:
70+
71+
| Options | Type | Description |
72+
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|-----------------------------------------------------------------------------------------|
73+
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L76) | `XHRCustomAttributeFunction` | Function for adding custom attributes |
74+
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L78) | `boolean` | Disable network events being added as span events (network events are added by default) |
75+
6776
## Example Screenshots
6877

6978
![Screenshot of the running example](images/main.jpg)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export interface XMLHttpRequestInstrumentationConfig
7474
ignoreUrls?: Array<string | RegExp>;
7575
/** Function for adding custom attributes on the span */
7676
applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;
77+
/** Ignore adding network events as span events */
78+
ignoreNetworkEvents?: boolean;
7779
}
7880

7981
/**
@@ -140,7 +142,9 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
140142
const childSpan = this.tracer.startSpan('CORS Preflight', {
141143
startTime: corsPreFlightRequest[PTN.FETCH_START],
142144
});
143-
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
145+
if (!this._getConfig().ignoreNetworkEvents) {
146+
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
147+
}
144148
childSpan.end(corsPreFlightRequest[PTN.RESPONSE_END]);
145149
});
146150
}
@@ -292,7 +296,9 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
292296
this._addChildSpan(span, corsPreFlightRequest);
293297
this._markResourceAsUsed(corsPreFlightRequest);
294298
}
295-
addSpanNetworkEvents(span, mainRequest);
299+
if (!this._getConfig().ignoreNetworkEvents) {
300+
addSpanNetworkEvents(span, mainRequest);
301+
}
296302
}
297303
}
298304

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,20 @@ describe('xhr', () => {
783783
);
784784
});
785785
});
786+
787+
describe('when network events are ignored', () => {
788+
beforeEach(done => {
789+
clearData();
790+
prepareData(done, url, {
791+
ignoreNetworkEvents: true,
792+
});
793+
});
794+
it('should NOT add network events', () => {
795+
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
796+
const events = span.events;
797+
assert.strictEqual(events.length, 3, 'number of events is wrong');
798+
});
799+
});
786800
});
787801

788802
describe('when request is NOT successful', () => {

0 commit comments

Comments
 (0)