@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
104 lines (103 loc) • 4.51 kB
JavaScript
import { getDefaultExport } from "../../utils/get-default-export.js";
import { HTTP_ON_CLIENT_REQUEST } from "./constants.js";
import { getOriginalFunction, wrapMethod } from "../../utils/object.js";
import { getHttpClientSubscriptions } from "./client-subscriptions.js";
// src/integrations/http/client-patch.ts
/**
* Platform-portable HTTP(S) outgoing-request patching integration
*
* Patches the `http` and `https` Node.js built-in module exports to create
* Sentry spans for outgoing requests and optionally inject distributed trace
* propagation headers.
*
* @module
*
* This Sentry integration is a derivative work based on the OpenTelemetry
* HTTP instrumentation.
*
* <https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-http>
*
* Extended under the terms of the Apache 2.0 license linked below:
*
* ----
*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let onHttpClientRequestCreated;
/**
* Patch `ClientRequest.prototype._storeHeader` so that every outgoing request
* is routed through our instrumentation.
*
* We deliberately patch the shared `ClientRequest` prototype rather than the
* module's `request`/`get` exports. Every outgoing request — no matter how the
* module was imported (`require('node:http')`, `import http from 'node:http'`,
* or `import * as http from 'node:http'`) — ultimately constructs a
* `ClientRequest` and serializes its headers through `_storeHeader` on this one
* prototype. ES module namespace bindings (`import * as http` / `import
* { request }`) are immutable snapshots that cannot be monkey-patched at all,
* but the prototype is a shared, mutable object, so patching it reaches those
* consumers too.
*
* `_storeHeader` is the method that renders the outgoing header block into
* `request._header`. We run *before* the original, which is the last moment a
* header can still be added via `setHeader` (afterwards `request._header` is
* set and any `setHeader` call throws `ERR_HTTP_HEADERS_SENT`). It runs
* synchronously in the caller's async context — during `request.end()` /
* `request.write()` — so spans are parented correctly, and is invoked exactly
* once per request whether the headers are being sent immediately or buffered
* while a socket is assigned.
*
* We intentionally do *not* hook `onSocket`: it does not fire until a socket is
* assigned to the request, which for an `Agent` with all sockets busy (e.g.
* `maxSockets: 1`) happens only *after* the request's headers have already been
* serialized — far too late to inject trace-propagation headers.
*
* `https` requests reuse `http`'s `ClientRequest`, so patching `http` covers
* both; the `https` module does not expose its own `ClientRequest` and is a
* no-op here.
*/
function patchClientRequest(httpModule, options) {
const proto = httpModule.ClientRequest?.prototype;
if (typeof proto?._storeHeader !== "function") return;
onHttpClientRequestCreated = getHttpClientSubscriptions({
...options,
http: httpModule
})[HTTP_ON_CLIENT_REQUEST];
if (getOriginalFunction(proto._storeHeader)) return;
const originalStoreHeader = proto._storeHeader;
wrapMethod(proto, "_storeHeader", function patchedStoreHeader(...args) {
try {
onHttpClientRequestCreated({ request: this }, HTTP_ON_CLIENT_REQUEST);
} catch {}
return originalStoreHeader.apply(this, args);
});
}
function patchModule(httpModuleExport, options = {}) {
patchClientRequest(getDefaultExport(httpModuleExport), options);
return httpModuleExport;
}
/**
* Patch `node:http`. This also covers `node:https` as it reuses the same `ClientRequest` class.
*
* @example
* ```javascript
* import http from 'http';
* import { patchHttpModuleClient } from '@sentry/core';
* patchHttpModuleClient(http, { tracePropagation: true });
* ```
*/
const patchHttpModuleClient = (httpModuleExport, options = {}) => patchModule(httpModuleExport, options);
export { patchHttpModuleClient };
//# sourceMappingURL=client-patch.js.map