Skip to content

Commit

Permalink
feat(@opentelemetry-instrumentation-fetch): optionally ignore network…
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
gregolsen committed Jun 12, 2022
1 parent 922e963 commit 2a604f8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
ignoreUrls?: Array<string | RegExp>;
/** Function for adding custom attributes on the span */
applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
// Ignore adding network events as span events
ignoreNetworkEvents?: boolean;
}

/**
Expand Down Expand Up @@ -105,7 +107,9 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
},
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]
);
Expand Down Expand Up @@ -247,7 +251,9 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
this._addChildSpan(span, corsPreFlightRequest);
this._markResourceAsUsed(corsPreFlightRequest);
}
web.addSpanNetworkEvents(span, mainRequest);
if (!this._getConfig().ignoreNetworkEvents) {
web.addSpanNetworkEvents(span, mainRequest);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit 2a604f8

Please sign in to comment.