Skip to content

Commit

Permalink
feat(opentelemetry-instrumentation-xhr): optionally ignore network ev…
Browse files Browse the repository at this point in the history
…ents (#4571)
  • Loading branch information
MustafaHaddara committed Mar 28, 2024
1 parent f6a075b commit 900b7d8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ req.send();

```

### XHR Instrumentation options

XHR 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-xml-http-request/src/xhr.ts#L76) | `XHRCustomAttributeFunction` | Function for adding custom attributes |
| [`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) |

## Example Screenshots

![Screenshot of the running example](images/main.jpg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export interface XMLHttpRequestInstrumentationConfig
ignoreUrls?: Array<string | RegExp>;
/** Function for adding custom attributes on the span */
applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;
/** Ignore adding network events as span events */
ignoreNetworkEvents?: boolean;
}

/**
Expand Down Expand Up @@ -140,7 +142,9 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
const childSpan = this.tracer.startSpan('CORS Preflight', {
startTime: corsPreFlightRequest[PTN.FETCH_START],
});
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
if (!this._getConfig().ignoreNetworkEvents) {
addSpanNetworkEvents(childSpan, corsPreFlightRequest);
}
childSpan.end(corsPreFlightRequest[PTN.RESPONSE_END]);
});
}
Expand Down Expand Up @@ -292,7 +296,9 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
this._addChildSpan(span, corsPreFlightRequest);
this._markResourceAsUsed(corsPreFlightRequest);
}
addSpanNetworkEvents(span, mainRequest);
if (!this._getConfig().ignoreNetworkEvents) {
addSpanNetworkEvents(span, mainRequest);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,20 @@ describe('xhr', () => {
);
});
});

describe('when network events are ignored', () => {
beforeEach(done => {
clearData();
prepareData(done, url, {
ignoreNetworkEvents: true,
});
});
it('should NOT add network events', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const events = span.events;
assert.strictEqual(events.length, 3, 'number of events is wrong');
});
});
});

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

0 comments on commit 900b7d8

Please sign in to comment.