UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

198 lines (195 loc) • 8.12 kB
import { hasSpansEnabled, eventFiltersIntegration, functionToStringIntegration, linkedErrorsIntegration, dedupeIntegration, requestDataIntegration, conversationIdIntegration, consoleSandbox, applySdkMetadata, envToBool, debug, _INTERNAL_warnIfClientIsActive, getCurrentScope, stackParserFromStackParserOptions, getIntegrationsToSetup, propagationContextFromHeaders } from '@sentry/core'; import { isMainThread, parentPort } from 'node:worker_threads'; import { getTracingIntegrations, getErrorIntegrations, detectOrchestrionSetup } from '@sentry/server-utils'; import { DEBUG_BUILD } from '../debug-build.js'; import { childProcessIntegration } from '../integrations/childProcess.js'; import { consoleIntegration } from '../integrations/console.js'; import { nodeContextIntegration } from '../integrations/context.js'; import { contextLinesIntegration } from '../integrations/contextlines.js'; import { httpIntegration } from '../integrations/http/index.js'; import { localVariablesIntegration } from '../integrations/local-variables/index.js'; import { modulesIntegration } from '../integrations/modules.js'; import { nativeNodeFetchIntegration } from '../integrations/node-fetch/index.js'; import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception.js'; import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection.js'; import { processSessionIntegration } from '../integrations/processSession.js'; import { INTEGRATION_NAME, spotlightIntegration } from '../integrations/spotlight.js'; import { systemErrorIntegration } from '../integrations/systemError.js'; import { workerThreadsIntegration } from '../integrations/workerThreads.js'; import { makeNodeTransport } from '../transports/http.js'; import { getEntryPointType } from '../utils/entry-point.js'; import { getSpotlightConfig } from '../utils/spotlight.js'; import { defaultStackParser, getSentryRelease } from './api.js'; import { NodeClient } from './client.js'; import { initOpenTelemetry } from './initOtel.js'; import { setupVercelKeepAlive } from './vercel.js'; function getBaseDefaultIntegrations() { return [ // Common eventFiltersIntegration(), functionToStringIntegration(), linkedErrorsIntegration(), dedupeIntegration(), requestDataIntegration(), systemErrorIntegration(), conversationIdIntegration(), // Native Wrappers consoleIntegration(), httpIntegration(), nativeNodeFetchIntegration(), // Global Handlers onUncaughtExceptionIntegration(), onUnhandledRejectionIntegration(), // Event Info contextLinesIntegration(), localVariablesIntegration(), nodeContextIntegration(), childProcessIntegration(), workerThreadsIntegration(), processSessionIntegration(), modulesIntegration(), // Framework-level integrations ...getErrorIntegrations() ]; } function getDefaultIntegrationsWithoutPerformance() { return getBaseDefaultIntegrations(); } function getDefaultIntegrations(options) { return [ ...getDefaultIntegrationsWithoutPerformance(), // We only add tracing-only integrations if tracing is enabled ...hasSpansEnabled(options) ? getTracingIntegrations() : [] ]; } function init(options = {}) { return _init(options, getDefaultIntegrations); } function initWithoutDefaultIntegrations(options = {}) { return _init(options, () => []); } function _init(options = {}, getDefaultIntegrationsImpl) { if (!isMainThread && !parentPort) { return void 0; } if (getEntryPointType() === "require") { consoleSandbox(() => { console.warn( "[Sentry] Initializing the SDK via the Node `--require` flag is no longer supported, because Node re-runs `--require` preloads on its module loader thread. Use `--import` instead: `node --import ./instrument.js app.js`" ); }); } applySdkMetadata(options, "node"); if (envToBool(options.debug ?? process.env.SENTRY_DEBUG)) { if (DEBUG_BUILD) { debug.enable(); } else { consoleSandbox(() => { console.warn("[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle."); }); } } const optionsWithResolvedTracing = { ...options, tracesSampleRate: getTracesSampleRate(options.tracesSampleRate) }; const defaultIntegrations = options.defaultIntegrations ?? getDefaultIntegrationsImpl(optionsWithResolvedTracing); const clientOptions = getClientOptions({ ...options, defaultIntegrations }, getDefaultIntegrationsImpl); _INTERNAL_warnIfClientIsActive(); const scope = getCurrentScope(); scope.update(clientOptions.initialScope); if (clientOptions.spotlight && !clientOptions.integrations.some(({ name }) => name === INTEGRATION_NAME)) { clientOptions.integrations.push( spotlightIntegration({ sidecarUrl: typeof clientOptions.spotlight === "string" ? clientOptions.spotlight : void 0 }) ); } const client = new NodeClient(clientOptions); getCurrentScope().setClient(client); client.init(); debug.log(`SDK initialized from ESM`); client.startClientReportTracking(); updateScopeFromEnvVariables(); if (process.env.VERCEL) { setupVercelKeepAlive(client); } if (clientOptions.enableOpenTelemetrySetup) { initOpenTelemetry(client); } detectOrchestrionSetup(); return client; } function getClientOptions(options, getDefaultIntegrationsImpl) { const release = getRelease(options.release); const spotlight = getSpotlightConfig(options.spotlight); const tracesSampleRate = getTracesSampleRate(options.tracesSampleRate); const traceLifecycle = getTraceLifecycle(options.traceLifecycle); const mergedOptions = { ...options, dsn: options.dsn ?? process.env.SENTRY_DSN, environment: options.environment ?? process.env.SENTRY_ENVIRONMENT, sendClientReports: options.sendClientReports ?? true, transport: options.transport ?? makeNodeTransport, stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser), release, tracesSampleRate, spotlight, traceLifecycle, // Most Node-based SDKs default to running without a Sentry OpenTelemetry tracer provider. SDKs // that need OTel spans surfaced in Sentry (nextjs, sveltekit) opt back in by passing `true`. enableOpenTelemetrySetup: options.enableOpenTelemetrySetup ?? false, debug: envToBool(options.debug ?? process.env.SENTRY_DEBUG) }; const integrations = options.integrations; const defaultIntegrations = options.defaultIntegrations ?? getDefaultIntegrationsImpl(mergedOptions); const resolvedIntegrations = getIntegrationsToSetup({ defaultIntegrations, integrations }); return { ...mergedOptions, integrations: resolvedIntegrations }; } function getRelease(release) { if (release !== void 0) { return release; } const detectedRelease = getSentryRelease(); if (detectedRelease !== void 0) { return detectedRelease; } return void 0; } function getTracesSampleRate(tracesSampleRate) { if (tracesSampleRate !== void 0) { return tracesSampleRate; } const sampleRateFromEnv = process.env.SENTRY_TRACES_SAMPLE_RATE; if (!sampleRateFromEnv) { return void 0; } const parsed = parseFloat(sampleRateFromEnv); return isFinite(parsed) ? parsed : void 0; } function getTraceLifecycle(traceLifecycle) { if (traceLifecycle !== void 0) { return traceLifecycle; } const lifecycleFromEnv = process.env.SENTRY_TRACE_LIFECYCLE; if (lifecycleFromEnv === "stream" || lifecycleFromEnv === "static") { return lifecycleFromEnv; } return "stream"; } function updateScopeFromEnvVariables() { if (envToBool(process.env.SENTRY_USE_ENVIRONMENT) !== false) { const sentryTraceEnv = process.env.SENTRY_TRACE; const baggageEnv = process.env.SENTRY_BAGGAGE; const propagationContext = propagationContextFromHeaders(sentryTraceEnv, baggageEnv); getCurrentScope().setPropagationContext(propagationContext); } } export { getDefaultIntegrations, getDefaultIntegrationsWithoutPerformance, init, initWithoutDefaultIntegrations }; //# sourceMappingURL=index.js.map