Note: This is an experimental package. New releases may include breaking changes.
This module provides auto instrumentation for web using XMLHttpRequest.
npm install --save @opentelemetry/instrumentation-xml-http-request
import {
ConsoleSpanExporter,
SimpleSpanProcessor,
WebTracerProvider,
} from '@opentelemetry/sdk-trace-web';
import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
const providerWithZone = new WebTracerProvider({
spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())]
});
providerWithZone.register({
contextManager: new ZoneContextManager(),
});
registerInstrumentations({
instrumentations: [
new XMLHttpRequestInstrumentation({
propagateTraceHeaderCorsUrls: ['http://localhost:8090']
}),
],
});
const webTracerWithZone = providerWithZone.getTracer('default');
/////////////////////////////////////////
// or plugin can be also initialised separately and then set the tracer provider or meter provider
const xmlHttpRequestInstrumentation = new XMLHttpRequestInstrumentation({
propagateTraceHeaderCorsUrls: ['http://localhost:8090']
});
const providerWithZone = new WebTracerProvider();
providerWithZone.register({
contextManager: new ZoneContextManager(),
});
xmlHttpRequestInstrumentation.setTracerProvider(providerWithZone);
/////////////////////////////////////////
// and some test
const req = new XMLHttpRequest();
req.open('GET', 'http://localhost:8090/xml-http-request.js', true);
req.send();
XHR instrumentation plugin has few options available to choose from. You can set the following:
| Options | Type | Description |
|---|---|---|
applyCustomAttributesOnSpan |
XHRCustomAttributeFunction |
Function for adding custom attributes |
ignoreNetworkEvents |
boolean | Disable network events being added as span events (network events are added by default) |
measureRequestSize |
boolean | Measure outgoing request length (outgoing request length is not measured by default) |
The instrumentation-xml-http-request versions v0.220.0 and later emit the stable v1.23.0+ semantic conventions.
Span attributes:
| v1.23.0 semconv | Notes |
|---|---|
http.request.method |
HTTP request method. With v1.23.0 semconv http.request.method_original may also be included. |
url.full |
Full HTTP request URL |
server.address and server.port |
The hostname and port of the request URL |
http.response.status_code |
HTTP response status code |
http.request.body.size |
This is only added if measureRequestSize is true. |
error.type |
The response status (as a string), if the response status was >=400, or one of these possible request errors: 'timeout' and 'error'. |

See examples/tracer-web for a short example.
Apache 2.0 - See LICENSE for more information.