{"version":3,"file":"util.cjs","names":["debug","debugPrefix","InngestSpanProcessor","BasicTracerProvider","trace"],"sources":["../../../../src/components/execution/otel/util.ts"],"sourcesContent":["import { context, trace } from \"@opentelemetry/api\";\nimport type { Instrumentation } from \"@opentelemetry/instrumentation\";\nimport { BasicTracerProvider } from \"@opentelemetry/sdk-trace-base\";\nimport Debug from \"debug\";\nimport { debugPrefix } from \"./consts.ts\";\nimport { InngestSpanProcessor } from \"./processor.ts\";\n\nconst debug = Debug(`${debugPrefix}:createProvider`);\n\nexport type Behaviour = \"createProvider\" | \"extendProvider\" | \"off\" | \"auto\";\nexport type Instrumentations = (Instrumentation | Instrumentation[])[];\n\nexport const createProvider = async (\n  _behaviour: Behaviour,\n  instrumentations: Instrumentations | undefined = [],\n): Promise<\n  | { success: true; processor: InngestSpanProcessor }\n  | { success: false; error?: unknown }\n> => {\n  try {\n    // TODO Check if there's an existing provider\n    const processor = new InngestSpanProcessor();\n\n    const p = new BasicTracerProvider({\n      spanProcessors: [processor],\n    });\n\n    // Dynamic imports to avoid loading the full auto-instrumentation suite at\n    // module evaluation time. These are only needed when creating a new provider,\n    // not when extending an existing one. Static imports here caused version\n    // conflicts with host app OTel setups (e.g. Sentry) and silently broke\n    // inngest.send(). See #1324.\n    const { getNodeAutoInstrumentations } = await import(\n      \"@opentelemetry/auto-instrumentations-node\"\n    );\n    const { registerInstrumentations } = await import(\n      \"@opentelemetry/instrumentation\"\n    );\n    const { AnthropicInstrumentation } = await import(\n      \"@traceloop/instrumentation-anthropic\"\n    );\n    const { AsyncHooksContextManager } = await import(\n      \"@opentelemetry/context-async-hooks\"\n    );\n\n    const instrList: Instrumentations = [\n      ...instrumentations,\n      ...getNodeAutoInstrumentations(),\n      new AnthropicInstrumentation(),\n    ];\n\n    registerInstrumentations({\n      instrumentations: instrList,\n    });\n\n    trace.setGlobalTracerProvider(p);\n    context.setGlobalContextManager(new AsyncHooksContextManager().enable());\n\n    return { success: true, processor };\n  } catch (err) {\n    debug(\"failed to create provider:\", err);\n    return { success: false, error: err };\n  }\n};\n\n/**\n * Attempts to extend the existing OTel provider with our processor. Returns true\n * if the provider was extended, false if it was not.\n */\nexport const extendProvider = (\n  behaviour: Behaviour,\n): { success: true; processor: InngestSpanProcessor } | { success: false } => {\n  // Attempt to add our processor and export to the existing provider\n  const globalProvider = trace.getTracerProvider();\n  if (!globalProvider) {\n    if (behaviour !== \"auto\") {\n      console.warn(\n        'No existing OTel provider found and behaviour is \"extendProvider\". Inngest\\'s OTel middleware will not work. Either allow the middleware to create a provider by setting `behaviour: \"createProvider\"` or `behaviour: \"auto\"`, or make sure that the provider is created and imported before the middleware is used.',\n      );\n    }\n\n    return { success: false };\n  }\n\n  // trace.getTracerProvider() returns a ProxyTracerProvider wrapper\n  // Unwrap it to get the actual provider.\n  const existingProvider =\n    \"getDelegate\" in globalProvider &&\n    typeof globalProvider.getDelegate === \"function\"\n      ? globalProvider.getDelegate()\n      : globalProvider;\n\n  if (!existingProvider) {\n    if (behaviour !== \"auto\") {\n      console.warn(\n        \"Existing OTel provider is not a BasicTracerProvider. Inngest's OTel middleware will not work, as it can only extend an existing processor if it's a BasicTracerProvider.\",\n      );\n    }\n\n    return { success: false };\n  }\n\n  const processor = new InngestSpanProcessor();\n\n  // OTel SDK v1 exposes addSpanProcessor() on BasicTracerProvider.\n  if (\n    \"addSpanProcessor\" in existingProvider &&\n    typeof existingProvider.addSpanProcessor === \"function\"\n  ) {\n    existingProvider.addSpanProcessor(processor);\n    return { success: true, processor };\n  }\n\n  // OTel SDK v2 removed addSpanProcessor() — span processors are constructor-only.\n  // No public API exists to add processors post-construction (OTel issue #5299),\n  // so push into the internal _spanProcessors array.\n  // These fields are TypeScript `private` (not #private), so accessible at runtime.\n  const spanProcessors = getInternalSpanProcessors(existingProvider);\n  if (spanProcessors) {\n    spanProcessors.push(processor);\n    return { success: true, processor };\n  }\n\n  if (behaviour !== \"auto\") {\n    console.warn(\n      \"Unable to add InngestSpanProcessor to existing OTel provider. \" +\n        \"The provider does not support addSpanProcessor() (OTel SDK v1) \" +\n        \"or expose _activeSpanProcessor._spanProcessors (OTel SDK v2).\",\n    );\n  }\n\n  return { success: false };\n};\n\n/**\n * Extract the internal span processors array from a BasicTracerProvider.\n * Returns the mutable array if accessible, undefined otherwise.\n *\n * BasicTracerProvider._activeSpanProcessor is a MultiSpanProcessor,\n * which holds a _spanProcessors: SpanProcessor[] array.\n * Both are TypeScript `private` (not ES #private), so accessible at runtime.\n *\n * Wrapped in try/catch because this accesses internal OTel fields that may\n * change — must never crash the host app.\n */\nfunction getInternalSpanProcessors(provider: unknown): unknown[] | undefined {\n  try {\n    const active = (provider as Record<string, unknown>)?._activeSpanProcessor;\n    if (typeof active !== \"object\" || active === null) return undefined;\n\n    const arr = (active as Record<string, unknown>)._spanProcessors;\n    return Array.isArray(arr) ? arr : undefined;\n  } catch {\n    return undefined;\n  }\n}\n"],"mappings":";;;;;;;;;AAOA,MAAMA,6BAAc,GAAGC,2BAAY,iBAAiB;AAKpD,MAAa,iBAAiB,OAC5B,YACA,mBAAiD,EAAE,KAIhD;AACH,KAAI;EAEF,MAAM,YAAY,IAAIC,wCAAsB;EAE5C,MAAM,IAAI,IAAIC,mDAAoB,EAChC,gBAAgB,CAAC,UAAU,EAC5B,CAAC;EAOF,MAAM,EAAE,gCAAgC,MAAM,OAC5C;EAEF,MAAM,EAAE,6BAA6B,MAAM,OACzC;EAEF,MAAM,EAAE,6BAA6B,MAAM,OACzC;EAEF,MAAM,EAAE,6BAA6B,MAAM,OACzC;AASF,2BAAyB,EACvB,kBAPkC;GAClC,GAAG;GACH,GAAG,6BAA6B;GAChC,IAAI,0BAA0B;GAC/B,EAIA,CAAC;AAEF,4BAAM,wBAAwB,EAAE;AAChC,8BAAQ,wBAAwB,IAAI,0BAA0B,CAAC,QAAQ,CAAC;AAExE,SAAO;GAAE,SAAS;GAAM;GAAW;UAC5B,KAAK;AACZ,UAAM,8BAA8B,IAAI;AACxC,SAAO;GAAE,SAAS;GAA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