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.
94 lines (92 loc) • 3.95 kB
JavaScript
import { debugPrefix } from "./consts.js";
import { InngestSpanProcessor } from "./processor.js";
import Debug from "debug";
import { context, trace } from "@opentelemetry/api";
import { BasicTracerProvider } from "@opentelemetry/sdk-trace-base";
//#region src/components/execution/otel/util.ts
const debug = Debug(`${debugPrefix}:createProvider`);
const createProvider = async (_behaviour, instrumentations = []) => {
try {
const processor = new InngestSpanProcessor();
const p = new 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()
] });
trace.setGlobalTracerProvider(p);
context.setGlobalContextManager(new AsyncHooksContextManager().enable());
return {
success: true,
processor
};
} catch (err) {
debug("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 = 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 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
export { createProvider, extendProvider };
//# sourceMappingURL=util.js.map