UNPKG

@mastra/core

Version:
92 lines (91 loc) • 3.62 kB
import { r as setSpanErrorAttemptResolver } from "../error-count-BH-rJW7q.js"; import { g as setExecuteWithContextSync, h as setExecuteWithContext, m as setCurrentSpanResolver } from "../utils-DU_pU5SK.js"; import { AsyncLocalStorage } from "async_hooks"; // src/observability/context-storage.ts /** * Ambient storage for the current span. Populated by executeWithContext/executeWithContextSync * so that infrastructure code (e.g. DualLogger) can resolve the active span without * being passed it explicitly. */ const spanContextStorage = new AsyncLocalStorage(); const errorAttemptStorage = new AsyncLocalStorage(); /** @internal An actual retry gets a new scope; durable replay uses the same scope. */ function withSpanErrorAttempt(attemptId, fn) { setSpanErrorAttemptResolver(() => errorAttemptStorage.getStore()); return errorAttemptStorage.run(attemptId, fn); } /** * Returns the current span from AsyncLocalStorage, if one is active. * Used by DualLogger to forward logs to a span-correlated loggerVNext. */ function getCurrentSpan() { return spanContextStorage.getStore(); } let initialized = false; /** * Registers the AsyncLocalStorage-backed context resolvers with the browser-safe * utils module. Must be called once at startup (e.g. from the Mastra constructor). * * This is an explicit initialization function rather than a top-level side-effect * because the package declares `"sideEffects": false` and bundler tree-shaking * strips bare side-effect imports. */ function initContextStorage() { if (initialized) return; initialized = true; setCurrentSpanResolver(getCurrentSpan); setExecuteWithContext(executeWithContext); setExecuteWithContextSync(executeWithContextSync); } /** * Execute an async function within the span's tracing context if available. * Falls back to direct execution if no span exists. * * When a bridge is configured, this enables auto-instrumented operations * (HTTP requests, database queries, etc.) to be properly nested under the * current span in the external tracing system. * * @param span - The span to use as context (or undefined to execute without context) * @param fn - The async function to execute * @returns The result of the function execution * * @example * ```typescript * const result = await executeWithContext(llmSpan, async () => * model.generateText(args) * ); * ``` */ async function executeWithContext(params) { const { span, fn } = params; const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn; if (span?.executeInContext) return span.executeInContext(wrappedFn); return wrappedFn(); } /** * Execute a synchronous function within the span's tracing context if available. * Falls back to direct execution if no span exists. * * When a bridge is configured, this enables auto-instrumented operations * (HTTP requests, database queries, etc.) to be properly nested under the * current span in the external tracing system. * * @param span - The span to use as context (or undefined to execute without context) * @param fn - The synchronous function to execute * @returns The result of the function execution * * @example * ```typescript * const result = executeWithContextSync(llmSpan, () => * model.streamText(args) * ); * ``` */ function executeWithContextSync(params) { const { span, fn } = params; const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn; if (span?.executeInContextSync) return span.executeInContextSync(wrappedFn); return wrappedFn(); } export { executeWithContext, executeWithContextSync, getCurrentSpan, initContextStorage, withSpanErrorAttempt }; //# sourceMappingURL=context-storage.js.map