From a5f4d0e7200a861c93c91992c6e9c3f8375ee25b Mon Sep 17 00:00:00 2001 From: RulaKhaled Date: Thu, 5 Feb 2026 15:51:03 +0100 Subject: [PATCH] fix(node): Actually check for ai require resolve to enforce the integraton --- .../integrations/tracing/vercelai/index.ts | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/node/src/integrations/tracing/vercelai/index.ts b/packages/node/src/integrations/tracing/vercelai/index.ts index a0b3f3126d01..7d5433b185c1 100644 --- a/packages/node/src/integrations/tracing/vercelai/index.ts +++ b/packages/node/src/integrations/tracing/vercelai/index.ts @@ -1,6 +1,6 @@ -import type { Client, IntegrationFn } from '@sentry/core'; +import type { IntegrationFn } from '@sentry/core'; import { addVercelAiProcessors, defineIntegration } from '@sentry/core'; -import { generateInstrumentOnce, type modulesIntegration } from '@sentry/node-core'; +import { generateInstrumentOnce } from '@sentry/node-core'; import { INTEGRATION_NAME } from './constants'; import { SentryVercelAiInstrumentation } from './instrumentation'; import type { VercelAiOptions } from './types'; @@ -8,12 +8,21 @@ import type { VercelAiOptions } from './types'; export const instrumentVercelAi = generateInstrumentOnce(INTEGRATION_NAME, () => new SentryVercelAiInstrumentation({})); /** - * Determines if the integration should be forced based on environment and package availability. - * Returns true if the 'ai' package is available. + * Determines if the 'ai' package is installed and available. + * + * Uses require.resolve() to check for package availability without loading it. + * This approach avoids race conditions that can occur with filesystem-based + * detection during initialization in serverless environments (Lambda/Vercel). + * + * @returns true if the 'ai' package can be resolved, false otherwise */ -function shouldForceIntegration(client: Client): boolean { - const modules = client.getIntegrationByName>('Modules'); - return !!modules?.getModules?.()?.ai; +function shouldForceIntegration(): boolean { + try { + require.resolve('ai'); + return true; + } catch { + return false; + } } const _vercelAIIntegration = ((options: VercelAiOptions = {}) => { @@ -26,13 +35,14 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => { instrumentation = instrumentVercelAi(); }, afterAllSetup(client) { - // Auto-detect if we should force the integration when running with 'ai' package available - // Note that this can only be detected if the 'Modules' integration is available, and running in CJS mode - const shouldForce = options.force ?? shouldForceIntegration(client); + // Auto-detect if we should force the integration when the 'ai' package is available + // Uses require.resolve() for reliable detection in all environments + const shouldForce = options.force ?? shouldForceIntegration(); if (shouldForce) { addVercelAiProcessors(client); } else { + // Lazy registration - only registers when 'ai' package is actually imported instrumentation?.callWhenPatched(() => addVercelAiProcessors(client)); } },