[]previous and next page

Instrumentation

Instrumentation in the Configuration with dusk.config.ts guide.

For agentsUpdated

src/instrumentation.ts is read by the server. Its exports:

  • register() runs once when the handler is created (the server starts). A failure is reported and the server keeps going.
  • onRequest(request, context) runs before each request is routed. context.routeKind is "unresolved" at that point.
  • onRequestError(error, request, context) sees every error the framework reports, with context.phase (request, api, rpc, loader, render, cache, after, instrumentation and others) and the route kind and id.
  • tracer is an object with OpenTelemetry's startActiveSpan shape. Each request runs inside one span named dusk.request, with the attributes http.request.method and url.path, and the loaders, the render, the endpoint that answers (API route, server function, page) and the end of a streamed page get child spans (see Observability). Pass the tracer of @opentelemetry/api or any object with the same methods; Dusk does not depend on OpenTelemetry.

None of these hooks can fail a request: a hook that throws is logged and the request is answered.

src/instrumentation.tsts
export function register(): void {
  console.info("dusk server started");
}

export function onRequestError(error: unknown, request: Request | undefined, context: { readonly phase: string; readonly routeKind: string | undefined }): void {
  console.error("request failed", context.phase, context.routeKind, request?.url, error);
}