From 5f9ef511dd3d588ead86d354ecddcf3131b7508c Mon Sep 17 00:00:00 2001 From: Inokentii Mykhailov Date: Wed, 15 Jun 2022 12:51:24 +0100 Subject: [PATCH] feat(@opentelemetry-instrumentation-fetch): optionally ignore network events (#3028) Co-authored-by: Valentin Marchaud --- experimental/CHANGELOG.md | 1 + .../README.md | 7 ++++--- .../src/fetch.ts | 10 ++++++++-- .../test/fetch.test.ts | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 696643c81f..205dd3123c 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(opentelemetry-instrumentation-fetch): optionally ignore network events #3028 @gregolsen * feat(http-instrumentation): record exceptions in http instrumentation #3008 @luismiramirez ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/README.md b/experimental/packages/opentelemetry-instrumentation-fetch/README.md index 17e6c2efa3..bf6719731c 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/README.md +++ b/experimental/packages/opentelemetry-instrumentation-fetch/README.md @@ -62,9 +62,10 @@ See [examples/tracer-web/fetch](https://github.com/open-telemetry/opentelemetry- Fetch instrumentation plugin has few options available to choose from. You can set the following: -| Options | Type | Description | -| ------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ------------------------------------- | -| [`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 | +| Options | Type | Description | +|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|-----------------------------------------------------------------------------------------| +| [`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 | +| [`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) | ## Useful links diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index ba4a2f422b..37ff62e234 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -63,6 +63,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig { ignoreUrls?: Array; /** Function for adding custom attributes on the span */ applyCustomAttributesOnSpan?: FetchCustomAttributeFunction; + // Ignore adding network events as span events + ignoreNetworkEvents?: boolean; } /** @@ -105,7 +107,9 @@ export class FetchInstrumentation extends InstrumentationBase> }, api.trace.setSpan(api.context.active(), span) ); - web.addSpanNetworkEvents(childSpan, corsPreFlightRequest); + if (!this._getConfig().ignoreNetworkEvents) { + web.addSpanNetworkEvents(childSpan, corsPreFlightRequest); + } childSpan.end( corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END] ); @@ -247,7 +251,9 @@ export class FetchInstrumentation extends InstrumentationBase> this._addChildSpan(span, corsPreFlightRequest); this._markResourceAsUsed(corsPreFlightRequest); } - web.addSpanNetworkEvents(span, mainRequest); + if (!this._getConfig().ignoreNetworkEvents) { + web.addSpanNetworkEvents(span, mainRequest); + } } } diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index c7a55acb7d..e4ce4f9f5a 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -900,4 +900,20 @@ describe('fetch', () => { ); }); }); + + describe('when network events are ignored', () => { + beforeEach(async () => { + await prepareData(url, { + ignoreNetworkEvents: true, + }); + }); + afterEach(() => { + clearData(); + }); + it('should NOT add network events', () => { + const span: tracing.ReadableSpan = exportSpy.args[1][0][0]; + const events = span.events; + assert.strictEqual(events.length, 0, 'number of events is wrong'); + }); + }); });