{"version":3,"file":"UserSpanProcessor.cjs","names":["ATTR_USER_ID"],"sources":["../../../src/processors/UserSpanProcessor/UserSpanProcessor.ts"],"sourcesContent":["import type { ReadableSpan, SpanProcessor } from '@opentelemetry/sdk-trace-web';\nimport { ATTR_USER_ID } from '@opentelemetry/semantic-conventions/incubating';\nimport type { UserManager } from '../../api-users/index.ts';\nimport type { UserSpanProcessorArgs } from './types.ts';\n\n/**\n * UserSpanProcessor sets the userId attribute on all spans if the userId is set in the UserManager.\n */\nexport class UserSpanProcessor implements SpanProcessor {\n  private readonly _userManager: UserManager;\n\n  public constructor({ userManager }: UserSpanProcessorArgs) {\n    this._userManager = userManager;\n  }\n\n  public forceFlush(): Promise<void> {\n    return Promise.resolve(undefined);\n  }\n\n  // TODO `onEnd` is not supposed to modify the span. There is a new experimental onEnding api that allows modifying\n  // using onEnd to make sure we get the userId at the last possible moment in case it was set up after the span was started.\n  public onEnd(span: ReadableSpan): void {\n    const userId = this._userManager.getUserId();\n\n    if (userId) {\n      span.attributes[ATTR_USER_ID] = userId;\n    }\n  }\n\n  public onStart(this: void): void {\n    // do nothing.\n  }\n\n  public shutdown(): Promise<void> {\n    return Promise.resolve(undefined);\n  }\n}\n"],"mappings":";;;;;;;AAQA,IAAa,oBAAb,MAAwD;CACtD;CAEA,YAAmB,EAAE,eAAsC;AACzD,OAAK,eAAe;;CAGtB,aAAmC;AACjC,SAAO,QAAQ,QAAQ,KAAA,EAAU;;CAKnC,MAAa,MAA0B;EACrC,MAAM,SAAS,KAAK,aAAa,WAAW;AAE5C,MAAI,OACF,MAAK,WAAWA,+CAAAA,gBAAgB;;CAIpC,UAAiC;CAIjC,WAAiC;AAC/B,SAAO,QAAQ,QAAQ,KAAA,EAAU"}