@embrace-io/web-sdk
Version:
3 lines (2 loc) • 3.12 kB
JavaScript
import{diag as s,trace as e}from"@opentelemetry/api";import{ATTR_SESSION_ID as i}from"@opentelemetry/semantic-conventions/incubating";import{KEY_PREFIX_EMB_PROPERTIES as t,KEY_EMB_SESSION_REASON_ENDED as n,EMB_STATES as o,EMB_TYPES as r,KEY_EMB_STATE as a,KEY_EMB_TYPE as h}from"../../constants/attributes.js";import{generateUUID as d}from"../../utils/generateUUID.js";import{OTelPerformanceManager as S}from"../../utils/PerformanceManager/OTelPerformanceManager.js";class c{constructor({diag:e,perf:i,visibilityDoc:t=window.document}={}){this._activeSessionId=null,this._activeSessionStartTime=null,this._sessionSpan=null,this._activeSessionCounts=null,this._sessionStartedListeners=[],this._sessionEndedListeners=[],this._diag=null!=e?e:s.createComponentLogger({namespace:"EmbraceSpanSessionManager"}),this._perf=null!=i?i:new S,this._visibilityDoc=t}addBreadcrumb(s){this._sessionSpan?this._sessionSpan.addEvent("emb-breadcrumb",{message:s},this._perf.getNowMillis()):this._diag.debug("trying to add breadcrumb to a session, but there is no session in progress. This is a no-op.")}addProperty(s,e){this._sessionSpan?this._sessionSpan.setAttribute(t+s,e):this._diag.debug("trying to add properties to a session, but there is no session in progress. This is a no-op.")}endSessionSpan(){this.endSessionSpanInternal("manual")}endSessionSpanInternal(s){if(this._sessionSpan){this._sessionSpan.setAttributes(Object.assign({[n]:s},this._activeSessionCounts)),this._sessionSpan.end(),this._sessionSpan=null,this._activeSessionStartTime=null,this._activeSessionId=null,this._activeSessionCounts=null;for(const s of this._sessionEndedListeners)try{s()}catch(s){this._diag.warn("Error while executing session ended listener",s)}}else this._diag.debug("trying to end a session, but there is no session in progress. This is a no-op.")}getSessionId(){return this._activeSessionId}getSessionSpan(){return this._sessionSpan}getSessionStartTime(){return this._activeSessionStartTime}startSessionSpan(){this._sessionSpan&&this.endSessionSpanInternal("manual");const s=e.getTracer("embrace-web-sdk-sessions");this._activeSessionId=d(),this._activeSessionStartTime=this._perf.getNowHRTime(),this._activeSessionCounts={},this._sessionSpan=s.startSpan("emb-session",{attributes:{[h]:r.Session,[a]:"hidden"===this._visibilityDoc.visibilityState?o.Background:o.Foreground,[i]:this._activeSessionId}});for(const s of this._sessionStartedListeners)try{s()}catch(s){this._diag.warn("Error while executing session started listener",s)}}incrSessionCountForKey(s){this._sessionSpan&&this._activeSessionCounts?this._activeSessionCounts[s]=(this._activeSessionCounts[s]||0)+1:this._diag.debug("trying to increment a count for the active session, but there is no session in progress. This is a no-op.")}addSessionStartedListener(s){const e=this._sessionStartedListeners.push(s);return()=>{this._sessionStartedListeners.splice(e-1,1)}}addSessionEndedListener(s){const e=this._sessionEndedListeners.push(s);return()=>{this._sessionEndedListeners.splice(e-1,1)}}}export{c as EmbraceSpanSessionManager};
//# sourceMappingURL=EmbraceSpanSessionManager.js.map