documentation/diagnostics-channel.md
Got integrates with Node.js diagnostic channels for low-overhead observability. Events are only published when subscribers exist.
got:request:createEmitted when a request is created.
{requestId: string, url: string, method: string}
got:request:startEmitted before the native HTTP request is sent.
{requestId: string, url: string, method: string, headers: Record<string, string | string[] | undefined>}
got:response:startEmitted when response headers are received.
{requestId: string, url: string, statusCode: number, headers: Record<string, string | string[] | undefined>, isFromCache: boolean}
got:response:endEmitted when the response completes.
{requestId: string, url: string, statusCode: number, bodySize?: number, timings?: Timings}
got:request:retryEmitted when retrying a request.
{requestId: string, retryCount: number, error: RequestError, delay: number}
got:request:errorEmitted when a request fails.
{requestId: string, url: string, error: RequestError, timings?: Timings}
got:response:redirectEmitted when following a redirect.
{requestId: string, fromUrl: string, toUrl: string, statusCode: number}
import diagnosticsChannel from 'node:diagnostics_channel';
const channel = diagnosticsChannel.channel('got:request:start');
channel.subscribe(message => {
console.log(`${message.method} ${message.url}`);
});
All events for a single request share the same requestId.
All message types are exported from the main package:
import type {
DiagnosticRequestCreate,
DiagnosticRequestStart,
DiagnosticResponseStart,
DiagnosticResponseEnd,
DiagnosticRequestRetry,
DiagnosticRequestError,
DiagnosticResponseRedirect,
} from 'got';