@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
70 lines (69 loc) • 3.39 kB
JavaScript
import { DEBUG_BUILD } from "../../debug-build.js";
import { addNonEnumerableProperty, debug, defineIntegration, getClient } from "@sentry/core";
import { HTTP2_ON_SERVER_STREAM, HTTP_ON_SERVER_REQUEST, getHttpServerSubscriptions } from "@sentry/core/server";
import { context, createContextKey, propagation } from "@opentelemetry/api";
import { subscribeDiagnosticsChannel } from "@sentry/server-utils";
// src/integrations/http/httpServerIntegration.ts
const HTTP_SERVER_INSTRUMENTED_KEY = createContextKey("sentry_http_server_instrumented");
const INTEGRATION_NAME = "Http.Server";
/**
* Add a callback to the request object that will be called when the request is started.
* The callback will receive the next function to continue processing the request.
*/
function addStartSpanCallback(request, callback) {
addNonEnumerableProperty(request, "_startSpanCallback", new WeakRef(callback));
}
const _httpServerIntegration = ((options = {}) => {
const _options = {
sessions: options.sessions ?? true,
sessionFlushingDelayMS: options.sessionFlushingDelayMS ?? 6e4,
maxRequestBodySize: options.maxRequestBodySize,
spans: false,
ignoreRequestBody: options.ignoreRequestBody,
/**
* Hook called by core's `instrumentServer` to wrap the upstream
* `emit('request')` call.
*
* We use it to extract OTel context from request headers and re-enter
* the OTel context before the framework sees the request, so subsequent
* spans (eg from `httpServerSpansIntegration`) attach to the right trace.
*/
wrapServerEmitRequest(request, response, normalizedRequest, next) {
const client = getClient();
if (!client) return next();
if (context.active().getValue(HTTP_SERVER_INSTRUMENTED_KEY)) return next();
const ctx = propagation.extract(context.active(), normalizedRequest.headers).setValue(HTTP_SERVER_INSTRUMENTED_KEY, true);
context.with(ctx, () => {
client.emit("httpServerRequest", request, response, normalizedRequest);
const callback = request._startSpanCallback?.deref();
if (callback) callback(() => {
next();
return true;
});
else next();
});
}
};
return {
name: INTEGRATION_NAME,
setupOnce() {
const { [HTTP_ON_SERVER_REQUEST]: onHttpServerRequestStart, [HTTP2_ON_SERVER_STREAM]: onHttp2ServerStreamStart } = getHttpServerSubscriptions(_options);
subscribeDiagnosticsChannel(HTTP_ON_SERVER_REQUEST, onHttpServerRequestStart);
subscribeDiagnosticsChannel(HTTP2_ON_SERVER_STREAM, onHttp2ServerStreamStart);
},
afterAllSetup(client) {
if (DEBUG_BUILD && client.getIntegrationByName("Http")) debug.warn("It seems that you have manually added `httpServerIntegration` while `httpIntegration` is also present. Make sure to remove `httpServerIntegration` when adding `httpIntegration`.");
}
};
});
/**
* This integration handles request isolation, trace continuation and other core Sentry functionality around incoming http requests
* handled via the node `http` module, or via the compat API of the `http2` module.
*
* This version uses OpenTelemetry for context propagation and span management.
*
* @see {@link ../../light/integrations/httpServerIntegration.ts} for the lightweight version without OpenTelemetry
*/
const httpServerIntegration = defineIntegration(_httpServerIntegration);
export { addStartSpanCallback, httpServerIntegration };
//# sourceMappingURL=httpServerIntegration.js.map