Skip to content

Network

The network Instrument automatically tracks outgoing HTTP(S) requests, providing valuable insights into network traffic and how modules and different parts of your application utilize the network.

It works by intercepting the fetch function and Node’s built-in http(s) module. This approach ensures that all outgoing requests, regardless of their origin, will be captured automatically across the entire application, including external modules. This allows you to track requests made by third-party modules and SDKs such as Stripe and AWS.

Instrument

Access the network instrument via the network namespace:

inspector.instruments.network

Configuration

Configure the network instrument using the main Inspector configuration options:

const inspector = new Inspector({
instruments: {
network: {
disabled: false,
interceptors: ['fetch', 'http'],
maxBodySize: 32000,
readBody: true,
},
},
});

Available configuration options:

  • disabled: boolean: Disables the instrument. When disabled, the instrument cannot be activated, and no data will be recorded (default false).
  • interceptors: [fetch | http]: An array of enabled interceptors. Possible values: fetch, http.
  • maxBodySize: number: The maximum size of the captured request body (in bytes). The body will be trimmed to this size.
  • readBody: boolean: Whether to capture the request body.

Capturing Requests

When the instrument is activated, request capture works automatically. Simply calling fetch (or making a request from the http(s) modules) will capture the request.

const resp = await fetch('https://example.com/');

If you wish to track custom requests, for example, if you use unsupported APIs, utilize the push function:

inspector.instruments.network.push(requets: NetworkRequest);

Querying Network

While you can easily view tracked requests in the Application, sometimes you may need to perform more specific queries. In such cases, utilize the query() function, which is consistent across all instruments.

inspector.instruments.network.query({
endTime: Date.now(),
startTime: Date.now() - 60000,
});

Request Schema

interface NetworkRequest {
duration: number;
error?: string;
initiator: string;
modules: string[];
request: {
body?: string | Buffer;
bodySize: number;
headers: Record<string, string | string[]>;
method: string;
url: string;
};
response: {
body?: string | Buffer;
bodySize: number;
headers: Record<string, string | string[]>;
status: number;
};
stack: string[];
start: number;
}