{"version":3,"file":"operation-context-D6LDf4W_.cjs","names":["nodeAsyncHooks"],"sources":["../src/operation-context.ts"],"sourcesContent":["/**\n * Operation context tracking using AsyncLocalStorage\n *\n * This module provides a way to track operation names across async boundaries\n * so they can be automatically captured in events events.\n *\n * We cannot read span attributes from OpenTelemetry's API (it's write-only),\n * so we maintain our own async context storage.\n */\n\n// namespace import for browser-bundler compat — see node-require.ts\nimport * as nodeAsyncHooks from 'node:async_hooks';\n\n/**\n * Operation context that flows through async operations\n */\nexport interface OperationContext {\n  /**\n   * The name of the current operation\n   * This is set by trace() or span() and can be read by events\n   */\n  name: string;\n}\n\n/**\n * AsyncLocalStorage instance for tracking operation context\n */\nconst operationStorage =\n  new nodeAsyncHooks.AsyncLocalStorage<OperationContext>();\n\n/**\n * Get the current operation context (if any)\n *\n * @returns The current operation context, or undefined if not in an operation\n *\n * @example\n * ```typescript\n * const ctx = getOperationContext();\n * if (ctx) {\n *   console.log('Current operation:', ctx.name);\n * }\n * ```\n */\nexport function getOperationContext(): OperationContext | undefined {\n  return operationStorage.getStore();\n}\n\n/**\n * Run a function within an operation context\n *\n * This sets the operation name for the duration of the function execution,\n * including all async operations spawned from it.\n *\n * @param name - The operation name to set\n * @param fn - The function to execute within the context\n * @returns The result of the function\n *\n * @example\n * ```typescript\n * const result = await runInOperationContext('user.create', async () => {\n *   // Any events.trackEvent() calls here will automatically capture\n *   // 'operation.name': 'user.create'\n *   await createUser();\n *   return 'success';\n * });\n * ```\n */\nexport function runInOperationContext<T>(name: string, fn: () => T): T {\n  return operationStorage.run({ name }, fn);\n}\n\n/**\n * Update the operation name in the current context\n *\n * This is useful when you want to change the operation name within\n * an already-established context (e.g., when entering a nested span).\n *\n * @param name - The new operation name\n *\n * @example\n * ```typescript\n * runInOperationContext('parent.operation', () => {\n *   // operation.name is 'parent.operation'\n *\n *   updateOperationName('nested.operation');\n *   // operation.name is now 'nested.operation'\n * });\n * ```\n */\nexport function updateOperationName(name: string): void {\n  const store = operationStorage.getStore();\n  if (store) {\n    store.name = name;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2BA,MAAM,mBACJ,IAAIA,iBAAe,kBAAoC;;;;;;;;;;;;;;AAezD,SAAgB,sBAAoD;CAClE,OAAO,iBAAiB,SAAS;AACnC;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,sBAAyB,MAAc,IAAgB;CACrE,OAAO,iBAAiB,IAAI,EAAE,KAAK,GAAG,EAAE;AAC1C"}