@embrace-io/web-sdk
Version:
3 lines (2 loc) • 5.19 kB
JavaScript
"use strict";var t=require("react"),e=require("hoist-non-react-statics"),n=require("./EmbraceInstrumentationBase-CS1Yr6S5.js");function r(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}require("uuid"),require("@opentelemetry/core"),require("@opentelemetry/api"),require("@opentelemetry/api-logs");var s=r(t);const a=/\(.*?\)/g;class o extends n.EmbraceInstrumentationBase{constructor({diag:t,shouldCleanupPathOptionsFromRouteName:e=!0}){super({instrumentationName:"NavigationInstrumentation",instrumentationVersion:"1.0.0",diag:t,config:{}}),this._shouldCleanupPathOptionsFromRouteName=!0,this._currentRoute=null,this._currentRouteSpan=null,this._instrumentationType=null,this._removeSessionStartedFn=null,this._removeSessionEndedFn=null,this.setInstrumentationType=t=>{this._instrumentationType=t},this.setCurrentRoute=t=>{var e;this._config.enabled&&t.url!==(null===(e=this._currentRoute)||void 0===e?void 0:e.url)&&(this._endRouteSpan(),this._startRouteSpan(t),this._currentRoute=t)},this._setupSessionListeners=()=>{this._removeSessionStartedFn||(this._removeSessionStartedFn=this.sessionManager.addSessionStartedListener((()=>{this._currentRoute&&!this._currentRouteSpan&&(this._diag.debug("Session started, starting route span."),this._startRouteSpan(this._currentRoute))}))),this._removeSessionEndedFn||(this._removeSessionEndedFn=this.sessionManager.addSessionEndedListener((()=>{this._currentRouteSpan&&(this._diag.debug("Session ended, ending route span."),this._endRouteSpan())})))},this._cleanUpSessionListeners=()=>{this._removeSessionStartedFn&&(this._removeSessionStartedFn(),this._removeSessionStartedFn=null),this._removeSessionEndedFn&&(this._removeSessionEndedFn(),this._removeSessionEndedFn=null)},this._startRouteSpan=t=>{this._diag.debug(`Starting route span for url: ${t.url}`),this._setupSessionListeners();const e=this._shouldCleanupPathOptionsFromRouteName?t.path.replace(a,""):t.path;return this._currentRouteSpan=this.tracer.startSpan(e,{attributes:{[n.KEY_EMB_TYPE]:n.EMB_TYPES.View,[n.KEY_VIEW_NAME]:e}}),this._instrumentationType&&this._currentRouteSpan.setAttribute(n.KEY_EMB_INSTRUMENTATION,this._instrumentationType),this._currentRouteSpan},this._endRouteSpan=()=>{this._currentRouteSpan&&this._currentRoute&&(this._diag.debug(`Ending route span for url: ${this._currentRoute.url}`),this._currentRouteSpan.end(),this._currentRouteSpan=null)},this.enable=()=>{this._diag.debug("NavigationInstrumentation enabled, listening for navigation events.")},this.disable=()=>{this._cleanUpSessionListeners(),this.setConfig({enabled:!1}),this._diag.debug("NavigationInstrumentation disabled, stopped listening for navigation events.")},this._shouldCleanupPathOptionsFromRouteName=e,this._config.enabled&&this.enable()}}let i;const u=(t={})=>(i||(i=new o(t)),i),h=(t,e)=>{var n;if(!t.props.match||!t.props.match.route)return null;const r=e?`${e.path}/${t.props.match.route.path}`:t.props.match.route.path;return(null===(n=t.props.routeContext)||void 0===n?void 0:n.outlet)?h(t.props.routeContext.outlet,{path:r,url:t.props.match.pathname}):{path:r,url:t.props.match.pathname}},p=(t,e)=>t.filter((t=>e.includes(t.pathname))).reduce(((t,e)=>e.route.path?t?{url:e.pathname,path:`${t.path}/${e.route.path}`}:{url:e.pathname,path:e.route.path}:t),null);class c extends s.Component{constructor(t){super(t),this._logManager=n.log.getLogManager(),this.state={hasError:!1}}static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(t,e){var r;this._logManager.logException(t,{handled:!1,attributes:{"react.component_stack":null!==(r=e.componentStack)&&void 0!==r?r:void 0,[n.KEY_EMB_INSTRUMENTATION]:n.EMB_ERROR_INSTRUMENTATIONS.ReactErrorBoundary}})}render(){return this.state.hasError?this.props.fallback():this.props.children}}exports.EmbraceErrorBoundary=c,exports.createReactRouterNavigationInstrumentation=(t={})=>u(t),exports.listenToRouterChanges=({router:t,routesMatcher:e,config:{pathnameDocument:r=window.location}={}})=>{const s=u();s.setInstrumentationType(n.EMB_NAVIGATION_INSTRUMENTATIONS.Data);const a=e(t.routes,{pathname:r.pathname}),o=a?p(a,r.pathname):null;return o&&s.setCurrentRoute(o),t.subscribe((t=>{const e=p(t.matches,t.location.pathname);e&&s.setCurrentRoute(e)}))},exports.withEmbraceRouting=r=>{const s=u();s.setInstrumentationType(n.EMB_NAVIGATION_INSTRUMENTATIONS.Declarative);const a=e=>{const n=r(e);if(n.props.match&&n.props.match.route){const t=h(n,null);t&&s.setCurrentRoute(t)}return t.createElement(r,e)};return a.displayName=`withEmbraceRouting(${r.displayName||r.name||"Component"})`,e(a,r),a},exports.withEmbraceRoutingLegacy=t=>{const r=u();r.setInstrumentationType(n.EMB_NAVIGATION_INSTRUMENTATIONS.DeclarativeLegacy);const a=e=>{if(e.path){const t=e;t.computedMatch&&r.setCurrentRoute({path:t.computedMatch.path,url:t.computedMatch.url})}return s.createElement(t,e)};return a.displayName=`withEmbraceRoutingLegacy(${t.displayName||t.name||"Component"})`,e(a,t),a};
//# sourceMappingURL=react-instrumentation.js.map