Skip to content

Feature Request: http.client.request.created Event in diagnostics_channel #55352

Closed
@antoniomuso

Description

@antoniomuso

What is the problem this feature will solve?

I am developing a library to trace HTTP server requests. The goal is to track requests from initiation to completion, including server-to-server (outgoing) requests.

Using diagnostics_channel and async_hooks, I have successfully implemented most of the tracking, except for extracting the body of outgoing server-to-server requests. The challenge arises because the only channel available to trace these requests, http.client.request.start, is triggered after the request body has been written.

Example

Here’s a typical server-to-server request scenario:

import http from 'http';

const req = http.request(options, (res) => {
    // Handle the response...
});

req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
});

req.write(postData);
req.end();
// <-- The `diagnostics_channel` callback fires after end(), once the body has already been written

I need a way to trace outgoing requests before the body is written. Currently, the callback for http.client.request.start fires only after the body has been sent, which makes it impossible to capture the body at the right time.

What is the feature you are proposing to solve the problem?

The feature I would like to suggest is to add a new diagnostics_channel event that is triggered when the request object is created, before the body is written, such as:

  • New event: http.client.request.created

This would allow developers to hook into the HTTP client request lifecycle earlier, making it possible to trace the request body and other parameters at the time of request creation.

What alternatives have you considered?

I tried different approaches to take the request body, but without success. The library should work without inspector and without adding command line options to the nodejs runtime, for that reason I discarded monkey patching using import-in-the-middle and require-in-the-middle.

Rejected Approaches

  • Monkey Patching: Using libraries like import-in-the-middle or require-in-the-middle was considered, but I want the library to work without modifying the Node.js runtime or requiring command-line options.
  • Node Inspector: Similarly, using the inspector is not an option, as the library needs to work seamlessly in production environments.

If there are other approaches or best practices that would achieve this goal without relying on runtime modifications, I am open to exploring them.

Thank you for taking the time to read and consider this request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnostics_channelIssues and PRs related to diagnostics channelfeature requestIssues that request new features to be added to Node.js.httpIssues or PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions