Skip to content

Commit 5f9ef51

Browse files
gregolsenvmarchaud
andauthored
feat(@opentelemetry-instrumentation-fetch): optionally ignore network events (#3028)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent c8c4ec6 commit 5f9ef51

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

experimental/CHANGELOG.md

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

1111
### :rocket: (Enhancement)
1212

13+
* feat(opentelemetry-instrumentation-fetch): optionally ignore network events #3028 @gregolsen
1314
* feat(http-instrumentation): record exceptions in http instrumentation #3008 @luismiramirez
1415

1516
### :bug: (Bug Fix)

experimental/packages/opentelemetry-instrumentation-fetch/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ See [examples/tracer-web/fetch](https://github.com/open-telemetry/opentelemetry-
6262

6363
Fetch instrumentation plugin has few options available to choose from. You can set the following:
6464

65-
| Options | Type | Description |
66-
| ------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ------------------------------------- |
67-
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L64) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
65+
| Options | Type | Description |
66+
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|-----------------------------------------------------------------------------------------|
67+
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L64) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
68+
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L67) | `boolean` | Disable network events being added as span events (network events are added by default) |
6869

6970
## Useful links
7071

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
6363
ignoreUrls?: Array<string | RegExp>;
6464
/** Function for adding custom attributes on the span */
6565
applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
66+
// Ignore adding network events as span events
67+
ignoreNetworkEvents?: boolean;
6668
}
6769

6870
/**
@@ -105,7 +107,9 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
105107
},
106108
api.trace.setSpan(api.context.active(), span)
107109
);
108-
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
110+
if (!this._getConfig().ignoreNetworkEvents) {
111+
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
112+
}
109113
childSpan.end(
110114
corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]
111115
);
@@ -247,7 +251,9 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
247251
this._addChildSpan(span, corsPreFlightRequest);
248252
this._markResourceAsUsed(corsPreFlightRequest);
249253
}
250-
web.addSpanNetworkEvents(span, mainRequest);
254+
if (!this._getConfig().ignoreNetworkEvents) {
255+
web.addSpanNetworkEvents(span, mainRequest);
256+
}
251257
}
252258
}
253259

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,4 +900,20 @@ describe('fetch', () => {
900900
);
901901
});
902902
});
903+
904+
describe('when network events are ignored', () => {
905+
beforeEach(async () => {
906+
await prepareData(url, {
907+
ignoreNetworkEvents: true,
908+
});
909+
});
910+
afterEach(() => {
911+
clearData();
912+
});
913+
it('should NOT add network events', () => {
914+
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
915+
const events = span.events;
916+
assert.strictEqual(events.length, 0, 'number of events is wrong');
917+
});
918+
});
903919
});

0 commit comments

Comments
 (0)