From 2a604f8197de345533cbd224c0a5c6a89509449c Mon Sep 17 00:00:00 2001 From: Inokentii Mykhailov Date: Thu, 9 Jun 2022 09:08:44 +0100 Subject: [PATCH] feat(@opentelemetry-instrumentation-fetch): optionally ignore network events --- experimental/CHANGELOG.md | 2 ++ .../README.md | 7 ++++--- .../src/fetch.ts | 10 ++++++++-- .../test/fetch.test.ts | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index c6b64299f0..ee8653d16c 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(opentelemetry-instrumentation-fetch): optionally ignore network events #3028 @gregolsen + ### :bug: (Bug Fix) * fix(otlp-transformer): remove type dependency on Long #3022 @legendecas 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'); + }); + }); });