axios
Record every axios request with instrument_axios. It wraps the axios adapter, so the real headers and body that go on the wire are captured, including interceptor changes.
import axios from 'axios';
import instrument_axios from '@inspector-rest/sdk/axios';
const eject = instrument_axios(axios, inspector, {
labels: { client: 'billing-api' }, // added to every request of this instance
status_as_error: true, // default: 4xx/5xx responses are reported as errors
});
axios.get(url, { inspector_labels: { op: 'list' } }); // labels of a single request
eject(); // restores the original adapterHow it works
The adapter wraps axios’ adapter. The request headers and body that are recorded are the ones that go on the wire, including headers added by interceptors registered before or after instrument_axios (for example an authorization header). The body is never serialized a second time, and getUri is not called.
Good to know
- It works with any axios adapter (
http,fetch,xhr) and in every runtime. - Pass the default
axiosexport or an instance fromaxios.create(). For created instances the SDK resolves the adapter through the installedaxiospackage, so keep a single copy of axios. - A request that sets its own
adapteroption is not recorded. - 4xx and 5xx responses that your
validateStatusaccepts are still reported with an error namedHttpError(the class is exported from the package). Withstatus_as_error: falsethey settle asok. - Streaming responses (
responseType: 'stream') are recorded without a body. - axios has no retry of its own, so every call is one attempt. Retries done by a library that calls axios again are separate records.
- Labels: the instance
labels, the context set withwith_labelsandinspector_labelsof a request are merged. See Labels.