@embrace-io/web-sdk
Version:
3 lines (2 loc) • 3.81 kB
JavaScript
import{__assign as s,__values as e}from"tslib";import{diag as t,trace as i}from"@opentelemetry/api";import{ATTR_SESSION_ID as n}from"@opentelemetry/semantic-conventions/incubating";import{KEY_PREFIX_EMB_PROPERTIES as o,KEY_EMB_SESSION_REASON_ENDED as r,EMB_TYPES as a,KEY_EMB_TYPE as p,EMB_STATES as d,KEY_EMB_STATE as h}from"../../constants/attributes.js";import{generateUUID as u}from"../../utils/generateUUID.js";import{OTelPerformanceManager as c}from"../../utils/PerformanceManager/OTelPerformanceManager.js";var S=function(){function S(s){var e=void 0===s?{}:s,i=e.diag,n=e.perf,o=e.visibilityDoc,r=void 0===o?window.document:o;this._activeSessionId=null,this._activeSessionStartTime=null,this._sessionSpan=null,this._activeSessionCounts=null,this._sessionStartedListeners=[],this._sessionEndedListeners=[],this._diag=null!=i?i:t.createComponentLogger({namespace:"EmbraceSpanSessionManager"}),this._perf=null!=n?n:new c,this._visibilityDoc=r}return S.prototype.addBreadcrumb=function(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.")},S.prototype.addProperty=function(s,e){this._sessionSpan?this._sessionSpan.setAttribute(o+s,e):this._diag.debug("trying to add properties to a session, but there is no session in progress. This is a no-op.")},S.prototype.endSessionSpan=function(){this.endSessionSpanInternal("manual")},S.prototype.endSessionSpanInternal=function(t){var i,n,o;if(this._sessionSpan){this._sessionSpan.setAttributes(s(((i={})[r]=t,i),this._activeSessionCounts)),this._sessionSpan.end(),this._sessionSpan=null,this._activeSessionStartTime=null,this._activeSessionId=null,this._activeSessionCounts=null;try{for(var a=e(this._sessionEndedListeners),p=a.next();!p.done;p=a.next()){var d=p.value;try{d()}catch(s){this._diag.warn("Error while executing session ended listener",s)}}}catch(s){n={error:s}}finally{try{p&&!p.done&&(o=a.return)&&o.call(a)}finally{if(n)throw n.error}}}else this._diag.debug("trying to end a session, but there is no session in progress. This is a no-op.")},S.prototype.getSessionId=function(){return this._activeSessionId},S.prototype.getSessionSpan=function(){return this._sessionSpan},S.prototype.getSessionStartTime=function(){return this._activeSessionStartTime},S.prototype.startSessionSpan=function(){var s,t,o;this._sessionSpan&&this.endSessionSpanInternal("manual");var r=i.getTracer("embrace-web-sdk-sessions");this._activeSessionId=u(),this._activeSessionStartTime=this._perf.getNowHRTime(),this._activeSessionCounts={},this._sessionSpan=r.startSpan("emb-session",{attributes:(s={},s[p]=a.Session,s[h]="hidden"===this._visibilityDoc.visibilityState?d.Background:d.Foreground,s[n]=this._activeSessionId,s)});try{for(var c=e(this._sessionStartedListeners),S=c.next();!S.done;S=c.next()){var l=S.value;try{l()}catch(s){this._diag.warn("Error while executing session started listener",s)}}}catch(s){t={error:s}}finally{try{S&&!S.done&&(o=c.return)&&o.call(c)}finally{if(t)throw t.error}}},S.prototype.incrSessionCountForKey=function(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.")},S.prototype.addSessionStartedListener=function(s){var e=this,t=this._sessionStartedListeners.push(s);return function(){e._sessionStartedListeners.splice(t-1,1)}},S.prototype.addSessionEndedListener=function(s){var e=this,t=this._sessionEndedListeners.push(s);return function(){e._sessionEndedListeners.splice(t-1,1)}},S}();export{S as EmbraceSpanSessionManager};
//# sourceMappingURL=EmbraceSpanSessionManager.js.map