Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opentelemetry-instrumentation-xhr): optionally ignore network events #4571

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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