UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

97 lines (95 loc) 4.3 kB
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs'); const require_consts = require('./consts.cjs'); const require_processor = require('./processor.cjs'); let debug = require("debug"); debug = require_rolldown_runtime.__toESM(debug); let __opentelemetry_api = require("@opentelemetry/api"); let __opentelemetry_sdk_trace_base = require("@opentelemetry/sdk-trace-base"); //#region src/components/execution/otel/util.ts const debug$1 = (0, debug.default)(`${require_consts.debugPrefix}:createProvider`); const createProvider = async (_behaviour, instrumentations = []) => { try { const processor = new require_processor.InngestSpanProcessor(); const p = new __opentelemetry_sdk_trace_base.BasicTracerProvider({ spanProcessors: [processor] }); const { getNodeAutoInstrumentations } = await import("@opentelemetry/auto-instrumentations-node"); const { registerInstrumentations } = await import("@opentelemetry/instrumentation"); const { AnthropicInstrumentation } = await import("@traceloop/instrumentation-anthropic"); const { AsyncHooksContextManager } = await import("@opentelemetry/context-async-hooks"); registerInstrumentations({ instrumentations: [ ...instrumentations, ...getNodeAutoInstrumentations(), new AnthropicInstrumentation() ] }); __opentelemetry_api.trace.setGlobalTracerProvider(p); __opentelemetry_api.context.setGlobalContextManager(new AsyncHooksContextManager().enable()); return { success: true, processor }; } catch (err) { debug$1("failed to create provider:", err); return { success: false, error: err }; } }; /** * Attempts to extend the existing OTel provider with our processor. Returns true * if the provider was extended, false if it was not. */ const extendProvider = (behaviour) => { const globalProvider = __opentelemetry_api.trace.getTracerProvider(); if (!globalProvider) { if (behaviour !== "auto") console.warn("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."); return { success: false }; } const existingProvider = "getDelegate" in globalProvider && typeof globalProvider.getDelegate === "function" ? globalProvider.getDelegate() : globalProvider; if (!existingProvider) { if (behaviour !== "auto") console.warn("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."); return { success: false }; } const processor = new require_processor.InngestSpanProcessor(); if ("addSpanProcessor" in existingProvider && typeof existingProvider.addSpanProcessor === "function") { existingProvider.addSpanProcessor(processor); return { success: true, processor }; } const spanProcessors = getInternalSpanProcessors(existingProvider); if (spanProcessors) { spanProcessors.push(processor); return { success: true, processor }; } if (behaviour !== "auto") console.warn("Unable to add InngestSpanProcessor to existing OTel provider. The provider does not support addSpanProcessor() (OTel SDK v1) or expose _activeSpanProcessor._spanProcessors (OTel SDK v2)."); return { success: false }; }; /** * Extract the internal span processors array from a BasicTracerProvider. * Returns the mutable array if accessible, undefined otherwise. * * BasicTracerProvider._activeSpanProcessor is a MultiSpanProcessor, * which holds a _spanProcessors: SpanProcessor[] array. * Both are TypeScript `private` (not ES #private), so accessible at runtime. * * Wrapped in try/catch because this accesses internal OTel fields that may * change — must never crash the host app. */ function getInternalSpanProcessors(provider) { try { const active = provider?._activeSpanProcessor; if (typeof active !== "object" || active === null) return void 0; const arr = active._spanProcessors; return Array.isArray(arr) ? arr : void 0; } catch { return; } } //#endregion exports.createProvider = createProvider; exports.extendProvider = extendProvider; //# sourceMappingURL=util.cjs.map