@sentry/nextjs
Version:
Official Sentry SDK for Next.js
88 lines (83 loc) • 4.43 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const api = require('@opentelemetry/api');
const cloudflare = require('@sentry/cloudflare');
const core = require('@sentry/core');
const opentelemetry = require('@sentry/opentelemetry');
const nextSpanAttributes = require('../common/nextSpanAttributes.js');
const dropMiddlewareTunnelRequests = require('../common/utils/dropMiddlewareTunnelRequests.js');
const dropReactControlFlowErrors = require('../common/utils/dropReactControlFlowErrors.js');
const responseEnd = require('../common/utils/responseEnd.js');
const nextSentryPropagator = require('../server/nextSentryPropagator.js');
const prepareSafeIdGeneratorContext = require('../server/prepareSafeIdGeneratorContext.js');
const prerenderContext = require('../server/prerenderContext.js');
const serverSpanHooks = require('../server/serverSpanHooks.js');
const useCacheInstrumentation = require('../server/useCacheInstrumentation.js');
prepareSafeIdGeneratorContext.prepareSafeIdGeneratorContext();
class NextjsCloudflarePropagator extends nextSentryPropagator.NextSentryPropagator {
/** @inheritDoc */
extract(ctx, carrier, getter) {
const activeSpan = api.trace.getSpan(api.context.active());
const sentryTrace = getter.get(carrier, "sentry-trace");
const incoming = core.extractTraceparentData(Array.isArray(sentryTrace) ? sentryTrace[0] : sentryTrace);
if (activeSpan && incoming?.parentSpanId && incoming.parentSpanId === core.spanToJSON(core.getRootSpan(activeSpan)).parent_span_id) {
return ctx;
}
return super.extract(ctx, carrier, getter);
}
}
const nextjsIntegration = () => ({
name: "Nextjs",
setup: (client) => {
var _a;
if (client.getOptions().enableOpenTelemetrySetup) {
prerenderContext.markPrerendersOnTracerProvider(prerenderContext.getGlobalTracerProvider());
api.propagation.setGlobalPropagator(new NextjsCloudflarePropagator());
}
(_a = client.getOptions()).release ?? (_a.release = core.GLOBAL_OBJ._sentryRelease);
serverSpanHooks.addNextjsServerSpanHooks(client);
client.addEventProcessor(dropReactControlFlowErrors.dropReactControlFlowErrorsEventProcessor);
opentelemetry.registerPrepareSpanScope(client);
client.on("beforeSampling", (samplingData) => {
if (dropMiddlewareTunnelRequests.isTunnelRouteSpan(samplingData.spanAttributes)) {
samplingData.parentSampled = false;
}
});
}
});
const withSentry = (optionsCallback, handler) => {
opentelemetry.setOpenTelemetryContextAsyncContextStrategy();
responseEnd.markAsyncContextOwnedByNextjsCloudflare();
return cloudflare.withSentry((env) => {
const options = { enableOpenTelemetrySetup: true, ...optionsCallback(env) };
options.ignoreSpans = [
...options.ignoreSpans || [],
...serverSpanHooks.NEXTJS_SERVER_IGNORE_SPANS,
// The request spans of Next.js repeat the request span of `withSentry`. An ignored span does not become active,
// so the other Next.js spans become children of the request span of `withSentry`, which then gets the route
// from their `next.route` and its status from the response.
{ attributes: { [nextSpanAttributes.ATTR_NEXT_SPAN_TYPE]: "BaseServer.handleRequest" } }
];
const { tracesSampler } = options;
if (tracesSampler) {
options.tracesSampler = (samplingContext) => !dropMiddlewareTunnelRequests.isTunnelRouteSpan(samplingContext.attributes || {}) && tracesSampler(samplingContext);
}
const userIntegrations = options.integrations;
options.integrations = (defaults) => {
const integrations = [...defaults, nextjsIntegration(), useCacheInstrumentation.nextjsUseCacheIntegration()];
return typeof userIntegrations === "function" ? userIntegrations(integrations) : [...integrations, ...userIntegrations || []];
};
core.applySdkMetadata(options, "nextjs", ["nextjs", "cloudflare"]);
return options;
}, handler);
};
exports.withSentry = withSentry;
Object.prototype.hasOwnProperty.call(cloudflare, '__proto__') &&
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
Object.defineProperty(exports, '__proto__', {
enumerable: true,
value: cloudflare['__proto__']
});
Object.keys(cloudflare).forEach(function (k) {
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = cloudflare[k];
});
//# sourceMappingURL=index.js.map