{"version":3,"file":"tracing.mjs","names":["TRACING_TAG_EXPERIMENT_PREFIX","KEY_VALUE_SEPARATOR","tagsByInstance","WeakMap","tracingPlugin","options","gb","tags","Map","set","unsubscribe","_subscribeExperimentViewed","experiment","result","user","experimentKey","key","variationKey","includes","tag","onAssignment","hashAttribute","hashValue","e","console","error","onDestroy","delete","getTracingTags","get","Array","from","values","sort"],"sources":["../../../src/plugins/tracing.ts"],"sourcesContent":["import type { TrackingUserContext } from \"../types/growthbook\";\nimport type { GrowthBook } from \"../GrowthBook\";\nimport type {\n  GrowthBookClient,\n  UserScopedGrowthBook,\n} from \"../GrowthBookClient\";\n\n// Tags are `gb.<kind>:<key>=<value>`; `gb.feature:` is reserved for flag evaluations.\nexport const TRACING_TAG_EXPERIMENT_PREFIX = \"gb.exp\";\n\nexport type TracingAssignment = {\n  experimentKey: string;\n  variationKey: string;\n  hashAttribute: string;\n  hashValue: string;\n  // `gb.exp:${experimentKey}=${variationKey}`\n  tag: string;\n};\n\nexport type TracingPluginOptions = {\n  // Called once per unique experiment assignment. Attach `assignment.tag` to\n  // the current trace/span in your LLM tracing tool (Langfuse, Phoenix, OTel).\n  onAssignment?: (\n    assignment: TracingAssignment,\n    user: TrackingUserContext,\n  ) => void;\n};\n\nconst KEY_VALUE_SEPARATOR = \"=\";\n\n// Latest tag per experiment key, per instance. WeakMap so a destroyed or\n// garbage-collected instance does not pin its tags in memory.\nconst tagsByInstance = new WeakMap<object, Map<string, string>>();\n\nexport function tracingPlugin(options: TracingPluginOptions = {}) {\n  return (gb: GrowthBook | UserScopedGrowthBook | GrowthBookClient) => {\n    // A bare multi-user client has no assignments of its own. The same plugin\n    // is re-run by each UserScopedGrowthBook it creates, which is what we want.\n    if (\"createScopedInstance\" in gb) {\n      return;\n    }\n\n    const tags = new Map<string, string>();\n    tagsByInstance.set(gb, tags);\n\n    const unsubscribe = gb._subscribeExperimentViewed(\n      (experiment, result, user) => {\n        const experimentKey = experiment.key;\n        const variationKey = result.key;\n\n        // The warehouse SQL splits on the first \"=\", so only the experiment key must avoid it.\n        if (experimentKey.includes(KEY_VALUE_SEPARATOR)) {\n          return;\n        }\n\n        const tag = `${TRACING_TAG_EXPERIMENT_PREFIX}:${experimentKey}${KEY_VALUE_SEPARATOR}${variationKey}`;\n        // A reassignment replaces the old variation; keeping both would read as a multiple exposure.\n        tags.set(experimentKey, tag);\n\n        if (options.onAssignment) {\n          try {\n            options.onAssignment(\n              {\n                experimentKey,\n                variationKey,\n                hashAttribute: result.hashAttribute,\n                hashValue: result.hashValue,\n                tag,\n              },\n              user,\n            );\n          } catch (e) {\n            console.error(e);\n          }\n        }\n      },\n    );\n\n    if (\"onDestroy\" in gb) {\n      gb.onDestroy(() => {\n        unsubscribe();\n        tagsByInstance.delete(gb);\n      });\n    }\n  };\n}\n\n// Sorted current tag for each experiment this instance has assigned.\nexport function getTracingTags(\n  gb: GrowthBook | UserScopedGrowthBook,\n): string[] {\n  const tags = tagsByInstance.get(gb);\n  return tags ? Array.from(tags.values()).sort() : [];\n}\n"],"mappings":"AAOA;AACA,OAAO,MAAMA,6BAA6B,GAAG,QAAQ;AAoBrD,MAAMC,mBAAmB,GAAG,GAAG;;AAE/B;AACA;AACA,MAAMC,cAAc,GAAG,IAAIC,OAAO,CAA8B,CAAC;AAEjE,OAAO,SAASC,aAAaA,CAACC,OAA6B,GAAG,CAAC,CAAC,EAAE;EAChE,OAAQC,EAAwD,IAAK;IACnE;IACA;IACA,IAAI,sBAAsB,IAAIA,EAAE,EAAE;MAChC;IACF;IAEA,MAAMC,IAAI,GAAG,IAAIC,GAAG,CAAiB,CAAC;IACtCN,cAAc,CAACO,GAAG,CAACH,EAAE,EAAEC,IAAI,CAAC;IAE5B,MAAMG,WAAW,GAAGJ,EAAE,CAACK,0BAA0B,CAC/C,CAACC,UAAU,EAAEC,MAAM,EAAEC,IAAI,KAAK;MAC5B,MAAMC,aAAa,GAAGH,UAAU,CAACI,GAAG;MACpC,MAAMC,YAAY,GAAGJ,MAAM,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