@grafana/faro-web-tracing 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,7 +86,7 @@ const qi=ci("zoneTask");function Wi(t,e,n,r){let o=null,i=null;n+=r;const a={};f
86
86
  * Use of this source code is governed by an MIT-style license that can be
87
87
  * found in the LICENSE file at https://angular.io/license
88
88
  */
89
- function Ki(t,e){if(Zone[e.symbol("patchEventTarget")])return;const{eventNames:n,zoneSymbolEventNames:r,TRUE_STR:o,FALSE_STR:i,ZONE_SYMBOL_PREFIX:a}=e.getGlobalObjects();for(let t=0;t<n.length;t++){const e=n[t],s=a+(e+i),u=a+(e+o);r[e]={},r[e][i]=s,r[e][o]=u}const s=t.EventTarget;return s&&s.prototype?(e.patchEventTarget(t,e,[s&&s.prototype]),!0):void 0}function Yi(t,e,n){const r=n instanceof Error?0:n.status;if(null==r)return;(0===r||r>=400&&r<600)&&t.setStatus({code:wt.ERROR})}function $i(t){return(e,n,r)=>{Yi(e,0,r),null==t||t(e,n,r)}}function Qi(t={}){var e;return"true"===(null===(e=t.attributes)||void 0===e?void 0:e.isSampled)?Vr.RECORD_AND_SAMPLED:Vr.NOT_RECORD}
89
+ function Ki(t,e){if(Zone[e.symbol("patchEventTarget")])return;const{eventNames:n,zoneSymbolEventNames:r,TRUE_STR:o,FALSE_STR:i,ZONE_SYMBOL_PREFIX:a}=e.getGlobalObjects();for(let t=0;t<n.length;t++){const e=n[t],s=a+(e+i),u=a+(e+o);r[e]={},r[e][i]=s,r[e][o]=u}const s=t.EventTarget;return s&&s.prototype?(e.patchEventTarget(t,e,[s&&s.prototype]),!0):void 0}function Yi(t,e,n){$i(t,n instanceof Error?0:n.status)}function $i(t,e){if(null==e)return;(0===e||e>=400&&e<600)&&t.setStatus({code:wt.ERROR})}function Qi(t){return(e,n,r)=>{Yi(e,0,r),null==t||t(e,n,r)}}function Ji(t){return(e,n)=>{!function(t,e){$i(t,e.status)}(e,n),null==t||t(e,n)}}function ta(t={}){var e;return"true"===(null===(e=t.attributes)||void 0===e?void 0:e.isSampled)?Vr.RECORD_AND_SAMPLED:Vr.NOT_RECORD}
90
90
  /**
91
91
  * @license
92
92
  * Copyright Google LLC All Rights Reserved.
@@ -94,4 +94,4 @@ function Ki(t,e){if(Zone[e.symbol("patchEventTarget")])return;const{eventNames:n
94
94
  * Use of this source code is governed by an MIT-style license that can be
95
95
  * found in the LICENSE file at https://angular.io/license
96
96
  */
97
- Zone.__load_patch("legacy",(t=>{const e=t[Zone.__symbol__("legacyPatch")];e&&e()})),Zone.__load_patch("queueMicrotask",((t,e,n)=>{n.patchMethod(t,"queueMicrotask",(t=>function(t,n){e.current.scheduleMicroTask("queueMicrotask",n[0])}))})),Zone.__load_patch("timers",(t=>{const e="set",n="clear";Wi(t,e,n,"Timeout"),Wi(t,e,n,"Interval"),Wi(t,e,n,"Immediate")})),Zone.__load_patch("requestAnimationFrame",(t=>{Wi(t,"request","cancel","AnimationFrame"),Wi(t,"mozRequest","mozCancel","AnimationFrame"),Wi(t,"webkitRequest","webkitCancel","AnimationFrame")})),Zone.__load_patch("blocking",((t,e)=>{const n=["alert","prompt","confirm"];for(let r=0;r<n.length;r++){Ri(t,n[r],((n,r,o)=>function(r,i){return e.current.run(n,t,i,o)}))}})),Zone.__load_patch("EventTarget",((t,e,n)=>{!function(t,e){e.patchEventPrototype(t,e)}(t,n),Ki(t,n);const r=t.XMLHttpRequestEventTarget;r&&r.prototype&&n.patchEventTarget(t,n,[r.prototype])})),Zone.__load_patch("MutationObserver",((t,e,n)=>{Oi("MutationObserver"),Oi("WebKitMutationObserver")})),Zone.__load_patch("IntersectionObserver",((t,e,n)=>{Oi("IntersectionObserver")})),Zone.__load_patch("FileReader",((t,e,n)=>{Oi("FileReader")})),Zone.__load_patch("on_property",((t,e,n)=>{Vi(n,t)})),Zone.__load_patch("customElements",((t,e,n)=>{!function(t,e){const{isBrowser:n,isMix:r}=e.getGlobalObjects();if(!n&&!r||!t.customElements||!("customElements"in t))return;e.patchCallbacks(e,t.customElements,"customElements","define",["connectedCallback","disconnectedCallback","adoptedCallback","attributeChangedCallback"])}(t,n)})),Zone.__load_patch("XHR",((t,e)=>{!function(t){const u=t.XMLHttpRequest;if(!u)return;const c=u.prototype;let l=c[ni],p=c[ri];if(!l){const e=t.XMLHttpRequestEventTarget;if(e){const t=e.prototype;l=t[ni],p=t[ri]}}const f="readystatechange",h="scheduled";function d(t){const r=t.data,a=r.target;a[i]=!1,a[s]=!1;const u=a[o];l||(l=a[ni],p=a[ri]),u&&p.call(a,f,u);const c=a[o]=()=>{if(a.readyState===a.DONE)if(!r.aborted&&a[i]&&t.state===h){const n=a[e.__symbol__("loadfalse")];if(0!==a.status&&n&&n.length>0){const o=t.invoke;t.invoke=function(){const n=a[e.__symbol__("loadfalse")];for(let e=0;e<n.length;e++)n[e]===t&&n.splice(e,1);r.aborted||t.state!==h||o.call(t)},n.push(t)}else t.invoke()}else r.aborted||!1!==a[i]||(a[s]=!0)};l.call(a,f,c);return a[n]||(a[n]=t),T.apply(a,r.args),a[i]=!0,t}function _(){}function v(t){const e=t.data;return e.aborted=!0,m.apply(e.target,e.args)}const g=Ri(c,"open",(()=>function(t,e){return t[r]=0==e[2],t[a]=e[1],g.apply(t,e)})),y=ci("fetchTaskAborting"),E=ci("fetchTaskScheduling"),T=Ri(c,"send",(()=>function(t,n){if(!0===e.current[E])return T.apply(t,n);if(t[r])return T.apply(t,n);{const e={target:t,url:t[a],isPeriodic:!1,args:n,aborted:!1},r=ui("XMLHttpRequest.send",_,e,d,v);t&&!0===t[s]&&!e.aborted&&r.state===h&&r.invoke()}})),m=Ri(c,"abort",(()=>function(t,r){const o=t[n];if(o&&"string"==typeof o.type){if(null==o.cancelFn||o.data&&o.data.aborted)return;o.zone.cancelTask(o)}else if(!0===e.current[y])return m.apply(t,r)}))}(t);const n=ci("xhrTask"),r=ci("xhrSync"),o=ci("xhrListener"),i=ci("xhrScheduled"),a=ci("xhrURL"),s=ci("xhrErrorBeforeScheduled")})),Zone.__load_patch("geolocation",(t=>{t.navigator&&t.navigator.geolocation&&function(t,e){const n=t.constructor.name;for(let r=0;r<e.length;r++){const o=e[r],i=t[o];if(i){if(!di(Ko(t,o)))continue;t[o]=(t=>{const e=function(){return t.apply(this,hi(arguments,n+"."+o))};return Pi(e,t),e})(i)}}}(t.navigator.geolocation,["getCurrentPosition","watchPosition"])})),Zone.__load_patch("PromiseRejectionEvent",((t,e)=>{function n(e){return function(n){Bi(t,e).forEach((r=>{const o=t.PromiseRejectionEvent;if(o){const t=new o(e,{promise:n.promise,reason:n.rejection});r.invoke(t)}}))}}t.PromiseRejectionEvent&&(e[ci("unhandledPromiseRejectionHandler")]=n("unhandledrejection"),e[ci("rejectionHandledHandler")]=n("rejectionhandled"))}));class Ji extends e.BaseInstrumentation{constructor(t={}){super(),this.options=t,this.name="@grafana/faro-web-tracing",this.version=e.VERSION}initialize(){var t,e,n,r,o,i,a,s;const u=this.options,c={};this.config.app.name&&(c[tn]=this.config.app.name),this.config.app.namespace&&(c["service.namespace"]=this.config.app.namespace),this.config.app.version&&(c["service.version"]=this.config.app.version),this.config.app.environment&&(c["deployment.environment"]=this.config.app.environment),Object.assign(c,u.resourceAttributes);const l=mr.default().merge(new mr(c)),p=new Po({resource:l,sampler:{shouldSample:()=>({decision:Qi(this.api.getSession())})}});p.addSpanProcessor(null!==(t=u.spanProcessor)&&void 0!==t?t:new Rr(new co(new Or({api:this.api}),{scheduledDelayMillis:Ji.SCHEDULED_BATCH_DELAY_MS,maxExportBatchSize:30}),this.metas)),p.register({propagator:null!==(e=u.propagator)&&void 0!==e?e:new An,contextManager:null!==(n=u.contextManager)&&void 0!==n?n:new Wo}),function(t){var e,n,r=t.tracerProvider||qt.getTracerProvider(),o=t.meterProvider||Dt.getMeterProvider(),i=t.loggerProvider||Ir.getLoggerProvider(),a=null!==(n=null===(e=t.instrumentations)||void 0===e?void 0:e.flat())&&void 0!==n?n:[];(function(t,e,n,r){for(var o=0,i=t.length;o<i;o++){var a=t[o];e&&a.setTracerProvider(e),n&&a.setMeterProvider(n),r&&a.setLoggerProvider&&a.setLoggerProvider(r),a.getConfig().enabled||a.enable()}})(a,r,o,i)}({instrumentations:null!==(r=u.instrumentations)&&void 0!==r?r:Vo({ignoreUrls:this.getIgnoreUrls(),propagateTraceHeaderCorsUrls:null===(o=this.options.instrumentationOptions)||void 0===o?void 0:o.propagateTraceHeaderCorsUrls,fetchInstrumentationOptions:{applyCustomAttributesOnSpan:$i(null===(a=null===(i=this.options.instrumentationOptions)||void 0===i?void 0:i.fetchInstrumentationOptions)||void 0===a?void 0:a.applyCustomAttributesOnSpan)},xhrInstrumentationOptions:Object.assign({},null===(s=this.options.instrumentationOptions)||void 0===s?void 0:s.xhrInstrumentationOptions)})}),this.api.initOTEL(qt,kt)}getIgnoreUrls(){return this.transports.transports.flatMap((t=>t.getIgnoreUrls()))}}return Ji.SCHEDULED_BATCH_DELAY_MS=1e3,t.FaroSessionSpanProcessor=Rr,t.FaroTraceExporter=Or,t.TracingInstrumentation=Ji,t.fetchCustomAttributeFunctionWithDefaults=$i,t.getDefaultOTELInstrumentations=Vo,t.getSamplingDecision=Qi,t.setSpanStatusOnFetchError=Yi,t}({},GrafanaFaroWebSdk);
97
+ Zone.__load_patch("legacy",(t=>{const e=t[Zone.__symbol__("legacyPatch")];e&&e()})),Zone.__load_patch("queueMicrotask",((t,e,n)=>{n.patchMethod(t,"queueMicrotask",(t=>function(t,n){e.current.scheduleMicroTask("queueMicrotask",n[0])}))})),Zone.__load_patch("timers",(t=>{const e="set",n="clear";Wi(t,e,n,"Timeout"),Wi(t,e,n,"Interval"),Wi(t,e,n,"Immediate")})),Zone.__load_patch("requestAnimationFrame",(t=>{Wi(t,"request","cancel","AnimationFrame"),Wi(t,"mozRequest","mozCancel","AnimationFrame"),Wi(t,"webkitRequest","webkitCancel","AnimationFrame")})),Zone.__load_patch("blocking",((t,e)=>{const n=["alert","prompt","confirm"];for(let r=0;r<n.length;r++){Ri(t,n[r],((n,r,o)=>function(r,i){return e.current.run(n,t,i,o)}))}})),Zone.__load_patch("EventTarget",((t,e,n)=>{!function(t,e){e.patchEventPrototype(t,e)}(t,n),Ki(t,n);const r=t.XMLHttpRequestEventTarget;r&&r.prototype&&n.patchEventTarget(t,n,[r.prototype])})),Zone.__load_patch("MutationObserver",((t,e,n)=>{Oi("MutationObserver"),Oi("WebKitMutationObserver")})),Zone.__load_patch("IntersectionObserver",((t,e,n)=>{Oi("IntersectionObserver")})),Zone.__load_patch("FileReader",((t,e,n)=>{Oi("FileReader")})),Zone.__load_patch("on_property",((t,e,n)=>{Vi(n,t)})),Zone.__load_patch("customElements",((t,e,n)=>{!function(t,e){const{isBrowser:n,isMix:r}=e.getGlobalObjects();if(!n&&!r||!t.customElements||!("customElements"in t))return;e.patchCallbacks(e,t.customElements,"customElements","define",["connectedCallback","disconnectedCallback","adoptedCallback","attributeChangedCallback"])}(t,n)})),Zone.__load_patch("XHR",((t,e)=>{!function(t){const u=t.XMLHttpRequest;if(!u)return;const c=u.prototype;let l=c[ni],p=c[ri];if(!l){const e=t.XMLHttpRequestEventTarget;if(e){const t=e.prototype;l=t[ni],p=t[ri]}}const f="readystatechange",h="scheduled";function d(t){const r=t.data,a=r.target;a[i]=!1,a[s]=!1;const u=a[o];l||(l=a[ni],p=a[ri]),u&&p.call(a,f,u);const c=a[o]=()=>{if(a.readyState===a.DONE)if(!r.aborted&&a[i]&&t.state===h){const n=a[e.__symbol__("loadfalse")];if(0!==a.status&&n&&n.length>0){const o=t.invoke;t.invoke=function(){const n=a[e.__symbol__("loadfalse")];for(let e=0;e<n.length;e++)n[e]===t&&n.splice(e,1);r.aborted||t.state!==h||o.call(t)},n.push(t)}else t.invoke()}else r.aborted||!1!==a[i]||(a[s]=!0)};l.call(a,f,c);return a[n]||(a[n]=t),T.apply(a,r.args),a[i]=!0,t}function _(){}function v(t){const e=t.data;return e.aborted=!0,m.apply(e.target,e.args)}const g=Ri(c,"open",(()=>function(t,e){return t[r]=0==e[2],t[a]=e[1],g.apply(t,e)})),y=ci("fetchTaskAborting"),E=ci("fetchTaskScheduling"),T=Ri(c,"send",(()=>function(t,n){if(!0===e.current[E])return T.apply(t,n);if(t[r])return T.apply(t,n);{const e={target:t,url:t[a],isPeriodic:!1,args:n,aborted:!1},r=ui("XMLHttpRequest.send",_,e,d,v);t&&!0===t[s]&&!e.aborted&&r.state===h&&r.invoke()}})),m=Ri(c,"abort",(()=>function(t,r){const o=t[n];if(o&&"string"==typeof o.type){if(null==o.cancelFn||o.data&&o.data.aborted)return;o.zone.cancelTask(o)}else if(!0===e.current[y])return m.apply(t,r)}))}(t);const n=ci("xhrTask"),r=ci("xhrSync"),o=ci("xhrListener"),i=ci("xhrScheduled"),a=ci("xhrURL"),s=ci("xhrErrorBeforeScheduled")})),Zone.__load_patch("geolocation",(t=>{t.navigator&&t.navigator.geolocation&&function(t,e){const n=t.constructor.name;for(let r=0;r<e.length;r++){const o=e[r],i=t[o];if(i){if(!di(Ko(t,o)))continue;t[o]=(t=>{const e=function(){return t.apply(this,hi(arguments,n+"."+o))};return Pi(e,t),e})(i)}}}(t.navigator.geolocation,["getCurrentPosition","watchPosition"])})),Zone.__load_patch("PromiseRejectionEvent",((t,e)=>{function n(e){return function(n){Bi(t,e).forEach((r=>{const o=t.PromiseRejectionEvent;if(o){const t=new o(e,{promise:n.promise,reason:n.rejection});r.invoke(t)}}))}}t.PromiseRejectionEvent&&(e[ci("unhandledPromiseRejectionHandler")]=n("unhandledrejection"),e[ci("rejectionHandledHandler")]=n("rejectionhandled"))}));class ea extends e.BaseInstrumentation{constructor(t={}){super(),this.options=t,this.name="@grafana/faro-web-tracing",this.version=e.VERSION}initialize(){var t,e,n,r,o,i,a,s,u;const c=this.options,l={};this.config.app.name&&(l[tn]=this.config.app.name),this.config.app.namespace&&(l["service.namespace"]=this.config.app.namespace),this.config.app.version&&(l["service.version"]=this.config.app.version),this.config.app.environment&&(l["deployment.environment"]=this.config.app.environment),Object.assign(l,c.resourceAttributes);const p=mr.default().merge(new mr(l)),f=new Po({resource:p,sampler:{shouldSample:()=>({decision:ta(this.api.getSession())})}});f.addSpanProcessor(null!==(t=c.spanProcessor)&&void 0!==t?t:new Rr(new co(new Or({api:this.api}),{scheduledDelayMillis:ea.SCHEDULED_BATCH_DELAY_MS,maxExportBatchSize:30}),this.metas)),f.register({propagator:null!==(e=c.propagator)&&void 0!==e?e:new An,contextManager:null!==(n=c.contextManager)&&void 0!==n?n:new Wo}),function(t){var e,n,r=t.tracerProvider||qt.getTracerProvider(),o=t.meterProvider||Dt.getMeterProvider(),i=t.loggerProvider||Ir.getLoggerProvider(),a=null!==(n=null===(e=t.instrumentations)||void 0===e?void 0:e.flat())&&void 0!==n?n:[];(function(t,e,n,r){for(var o=0,i=t.length;o<i;o++){var a=t[o];e&&a.setTracerProvider(e),n&&a.setMeterProvider(n),r&&a.setLoggerProvider&&a.setLoggerProvider(r),a.getConfig().enabled||a.enable()}})(a,r,o,i)}({instrumentations:null!==(r=c.instrumentations)&&void 0!==r?r:Vo({ignoreUrls:this.getIgnoreUrls(),propagateTraceHeaderCorsUrls:null===(o=this.options.instrumentationOptions)||void 0===o?void 0:o.propagateTraceHeaderCorsUrls,fetchInstrumentationOptions:{applyCustomAttributesOnSpan:Qi(null===(a=null===(i=this.options.instrumentationOptions)||void 0===i?void 0:i.fetchInstrumentationOptions)||void 0===a?void 0:a.applyCustomAttributesOnSpan)},xhrInstrumentationOptions:{applyCustomAttributesOnSpan:Ji(null===(u=null===(s=this.options.instrumentationOptions)||void 0===s?void 0:s.xhrInstrumentationOptions)||void 0===u?void 0:u.applyCustomAttributesOnSpan)}})}),this.api.initOTEL(qt,kt)}getIgnoreUrls(){return this.transports.transports.flatMap((t=>t.getIgnoreUrls()))}}return ea.SCHEDULED_BATCH_DELAY_MS=1e3,t.FaroSessionSpanProcessor=Rr,t.FaroTraceExporter=Or,t.TracingInstrumentation=ea,t.fetchCustomAttributeFunctionWithDefaults=Qi,t.getDefaultOTELInstrumentations=Vo,t.getSamplingDecision=ta,t.setSpanStatusOnFetchError=Yi,t}({},GrafanaFaroWebSdk);
@@ -1,5 +1,6 @@
1
1
  import { Span } from '@opentelemetry/api';
2
2
  import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
3
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
3
4
  export interface FetchError {
4
5
  status?: number;
5
6
  message: string;
@@ -14,4 +15,6 @@ export interface FetchError {
14
15
  * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
15
16
  */
16
17
  export declare function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError): void;
18
+ export declare function setSpanStatusOnXMLHttpRequestError(span: Span, xhr: XMLHttpRequest): void;
17
19
  export declare function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction): (span: Span, request: Request | RequestInit, result: Response | FetchError) => void;
20
+ export declare function xhrCustomAttributeFunctionWithDefaults(callback?: XHRCustomAttributeFunction): (span: Span, xhr: XMLHttpRequest) => void;
@@ -14,17 +14,6 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
17
  Object.defineProperty(exports, "__esModule", { value: true });
29
18
  exports.TracingInstrumentation = void 0;
30
19
  var api_1 = require("@opentelemetry/api");
@@ -55,7 +44,7 @@ var TracingInstrumentation = /** @class */ (function (_super) {
55
44
  }
56
45
  TracingInstrumentation.prototype.initialize = function () {
57
46
  var _this = this;
58
- var _a, _b, _c, _d, _e, _f, _g, _h;
47
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
59
48
  var options = this.options;
60
49
  var attributes = {};
61
50
  if (this.config.app.name) {
@@ -97,7 +86,9 @@ var TracingInstrumentation = /** @class */ (function (_super) {
97
86
  fetchInstrumentationOptions: {
98
87
  applyCustomAttributesOnSpan: (0, instrumentationUtils_1.fetchCustomAttributeFunctionWithDefaults)((_g = (_f = this.options.instrumentationOptions) === null || _f === void 0 ? void 0 : _f.fetchInstrumentationOptions) === null || _g === void 0 ? void 0 : _g.applyCustomAttributesOnSpan),
99
88
  },
100
- xhrInstrumentationOptions: __assign({}, (_h = this.options.instrumentationOptions) === null || _h === void 0 ? void 0 : _h.xhrInstrumentationOptions),
89
+ xhrInstrumentationOptions: {
90
+ applyCustomAttributesOnSpan: (0, instrumentationUtils_1.xhrCustomAttributeFunctionWithDefaults)((_j = (_h = this.options.instrumentationOptions) === null || _h === void 0 ? void 0 : _h.xhrInstrumentationOptions) === null || _j === void 0 ? void 0 : _j.applyCustomAttributesOnSpan),
91
+ },
101
92
  }),
102
93
  });
103
94
  this.api.initOTEL(api_1.trace, api_1.context);
@@ -1 +1 @@
1
- {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAoD;AACpD,4DAAiE;AACjE,4CAAgE;AAChE,kEAA0E;AAC1E,sDAAwE;AACxE,8DAAqF;AACrF,4EAK6C;AAE7C,sDAAgF;AAEhF,yDAAwD;AACxD,mFAAkF;AAClF,+DAAkF;AAClF,qCAAgD;AAChD,+DAAkE;AAGlE,8CAA8C;AAC9C,gEAAgE;AAChE,0BAA0B;AAE1B;IAA4C,0CAAmB;IAM7D,gCAAoB,OAA2C;QAA3C,wBAAA,EAAA,YAA2C;QAA/D,YACE,iBAAO,SACR;QAFmB,aAAO,GAAP,OAAO,CAAoC;QAL/D,UAAI,G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A/EM,+CAAwB,GAAG,IAAI,CAAC;IAgFzC,6BAAC;CAAA,AApFD,CAA4C,kCAAmB,GAoF9D;AApFY,wDAAsB","sourcesContent":["import { context, trace } from '@opentelemetry/api';\nimport { ZoneContextManager } from '@opentelemetry/context-zone';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { registerInstrumentations } from '@opentelemetry/instrumentation';\nimport { Resource, ResourceAttributes } from '@opentelemetry/resources';\nimport { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web';\nimport {\n SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nimport { BaseInstrumentation, Transport, VERSION } from '@grafana/faro-web-sdk';\n\nimport { FaroTraceExporter } from './faroTraceExporter';\nimport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\nimport { fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';\nimport { getSamplingDecision } from './sampler';\nimport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\nimport type { TracingInstrumentationOptions } from './types';\n\n// the providing of app name here is not great\n// should delay initialization and provide the full Faro config,\n// taking app name from it\n\nexport class TracingInstrumentation extends BaseInstrumentation {\n name = '@grafana/faro-web-tracing';\n version = VERSION;\n\n static SCHEDULED_BATCH_DELAY_MS = 1000;\n\n constructor(private options: TracingInstrumentationOptions = {}) {\n super();\n }\n\n initialize(): void {\n const options = this.options;\n const attributes: ResourceAttributes = {};\n\n if (this.config.app.name) {\n attributes[SEMRESATTRS_SERVICE_NAME] = this.config.app.name;\n }\n\n if (this.config.app.namespace) {\n attributes[SEMRESATTRS_SERVICE_NAMESPACE] = this.config.app.namespace;\n }\n\n if (this.config.app.version) {\n attributes[SEMRESATTRS_SERVICE_VERSION] = this.config.app.version;\n }\n\n if (this.config.app.environment) {\n attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT] = this.config.app.environment;\n }\n\n Object.assign(attributes, options.resourceAttributes);\n\n const resource = Resource.default().merge(new Resource(attributes));\n\n const provider = new WebTracerProvider({\n resource,\n sampler: {\n shouldSample: () => {\n return {\n decision: getSamplingDecision(this.api.getSession()),\n };\n },\n },\n });\n\n provider.addSpanProcessor(\n options.spanProcessor ??\n new FaroSessionSpanProcessor(\n new BatchSpanProcessor(new FaroTraceExporter({ api: this.api }), {\n scheduledDelayMillis: TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS,\n maxExportBatchSize: 30,\n }),\n this.metas\n )\n );\n\n provider.register({\n propagator: options.propagator ?? new W3CTraceContextPropagator(),\n contextManager: options.contextManager ?? new ZoneContextManager(),\n });\n\n registerInstrumentations({\n instrumentations:\n options.instrumentations ??\n getDefaultOTELInstrumentations({\n ignoreUrls: this.getIgnoreUrls(),\n propagateTraceHeaderCorsUrls: this.options.instrumentationOptions?.propagateTraceHeaderCorsUrls,\n fetchInstrumentationOptions: {\n applyCustomAttributesOnSpan: fetchCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.fetchInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n xhrInstrumentationOptions: {\n ...this.options.instrumentationOptions?.xhrInstrumentationOptions,\n },\n }),\n });\n\n this.api.initOTEL(trace, context);\n }\n\n private getIgnoreUrls(): Array<string | RegExp> {\n return this.transports.transports.flatMap((transport: Transport) => transport.getIgnoreUrls());\n }\n}\n"]}
1
+ {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,0CAAoD;AACpD,4DAAiE;AACjE,4CAAgE;AAChE,kEAA0E;AAC1E,sDAAwE;AACxE,8DAAqF;AACrF,4EAK6C;AAE7C,sDAAgF;AAEhF,yDAAwD;AACxD,mFAAkF;AAClF,+DAGgC;AAChC,qCAAgD;AAChD,+DAAkE;AAGlE,8CAA8C;AAC9C,gEA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,GAAG,CAAC,QAAQ,CAAC,WAAK,EAAE,aAAO,CAAC,CAAC;IACpC,CAAC;IAEO,8CAAa,GAArB;QACE,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,UAAC,SAAoB,IAAK,OAAA,SAAS,CAAC,aAAa,EAAE,EAAzB,CAAyB,CAAC,CAAC;IACjG,CAAC;IAjFM,+CAAwB,GAAG,IAAI,CAAC;IAkFzC,6BAAC;CAAA,AAtFD,CAA4C,kCAAmB,GAsF9D;AAtFY,wDAAsB","sourcesContent":["import { context, trace } from '@opentelemetry/api';\nimport { ZoneContextManager } from '@opentelemetry/context-zone';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { registerInstrumentations } from '@opentelemetry/instrumentation';\nimport { Resource, ResourceAttributes } from '@opentelemetry/resources';\nimport { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web';\nimport {\n SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nimport { BaseInstrumentation, Transport, VERSION } from '@grafana/faro-web-sdk';\n\nimport { FaroTraceExporter } from './faroTraceExporter';\nimport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\nimport {\n fetchCustomAttributeFunctionWithDefaults,\n xhrCustomAttributeFunctionWithDefaults,\n} from './instrumentationUtils';\nimport { getSamplingDecision } from './sampler';\nimport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\nimport type { TracingInstrumentationOptions } from './types';\n\n// the providing of app name here is not great\n// should delay initialization and provide the full Faro config,\n// taking app name from it\n\nexport class TracingInstrumentation extends BaseInstrumentation {\n name = '@grafana/faro-web-tracing';\n version = VERSION;\n\n static SCHEDULED_BATCH_DELAY_MS = 1000;\n\n constructor(private options: TracingInstrumentationOptions = {}) {\n super();\n }\n\n initialize(): void {\n const options = this.options;\n const attributes: ResourceAttributes = {};\n\n if (this.config.app.name) {\n attributes[SEMRESATTRS_SERVICE_NAME] = this.config.app.name;\n }\n\n if (this.config.app.namespace) {\n attributes[SEMRESATTRS_SERVICE_NAMESPACE] = this.config.app.namespace;\n }\n\n if (this.config.app.version) {\n attributes[SEMRESATTRS_SERVICE_VERSION] = this.config.app.version;\n }\n\n if (this.config.app.environment) {\n attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT] = this.config.app.environment;\n }\n\n Object.assign(attributes, options.resourceAttributes);\n\n const resource = Resource.default().merge(new Resource(attributes));\n\n const provider = new WebTracerProvider({\n resource,\n sampler: {\n shouldSample: () => {\n return {\n decision: getSamplingDecision(this.api.getSession()),\n };\n },\n },\n });\n\n provider.addSpanProcessor(\n options.spanProcessor ??\n new FaroSessionSpanProcessor(\n new BatchSpanProcessor(new FaroTraceExporter({ api: this.api }), {\n scheduledDelayMillis: TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS,\n maxExportBatchSize: 30,\n }),\n this.metas\n )\n );\n\n provider.register({\n propagator: options.propagator ?? new W3CTraceContextPropagator(),\n contextManager: options.contextManager ?? new ZoneContextManager(),\n });\n\n registerInstrumentations({\n instrumentations:\n options.instrumentations ??\n getDefaultOTELInstrumentations({\n ignoreUrls: this.getIgnoreUrls(),\n propagateTraceHeaderCorsUrls: this.options.instrumentationOptions?.propagateTraceHeaderCorsUrls,\n fetchInstrumentationOptions: {\n applyCustomAttributesOnSpan: fetchCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.fetchInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n xhrInstrumentationOptions: {\n applyCustomAttributesOnSpan: xhrCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.xhrInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n }),\n });\n\n this.api.initOTEL(trace, context);\n }\n\n private getIgnoreUrls(): Array<string | RegExp> {\n return this.transports.transports.flatMap((transport: Transport) => transport.getIgnoreUrls());\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchCustomAttributeFunctionWithDefaults = exports.setSpanStatusOnFetchError = void 0;
3
+ exports.xhrCustomAttributeFunctionWithDefaults = exports.fetchCustomAttributeFunctionWithDefaults = exports.setSpanStatusOnXMLHttpRequestError = exports.setSpanStatusOnFetchError = void 0;
4
4
  var api_1 = require("@opentelemetry/api");
5
5
  /**
6
6
  * Adds HTTP status code to every span.
@@ -13,6 +13,14 @@ var api_1 = require("@opentelemetry/api");
13
13
  */
14
14
  function setSpanStatusOnFetchError(span, _request, result) {
15
15
  var httpStatusCode = result instanceof Error ? 0 : result.status;
16
+ setSpanStatus(span, httpStatusCode);
17
+ }
18
+ exports.setSpanStatusOnFetchError = setSpanStatusOnFetchError;
19
+ function setSpanStatusOnXMLHttpRequestError(span, xhr) {
20
+ setSpanStatus(span, xhr.status);
21
+ }
22
+ exports.setSpanStatusOnXMLHttpRequestError = setSpanStatusOnXMLHttpRequestError;
23
+ function setSpanStatus(span, httpStatusCode) {
16
24
  if (httpStatusCode == null) {
17
25
  return;
18
26
  }
@@ -22,7 +30,6 @@ function setSpanStatusOnFetchError(span, _request, result) {
22
30
  span.setStatus({ code: api_1.SpanStatusCode.ERROR });
23
31
  }
24
32
  }
25
- exports.setSpanStatusOnFetchError = setSpanStatusOnFetchError;
26
33
  function fetchCustomAttributeFunctionWithDefaults(callback) {
27
34
  return function (span, request, result) {
28
35
  setSpanStatusOnFetchError(span, request, result);
@@ -30,4 +37,11 @@ function fetchCustomAttributeFunctionWithDefaults(callback) {
30
37
  };
31
38
  }
32
39
  exports.fetchCustomAttributeFunctionWithDefaults = fetchCustomAttributeFunctionWithDefaults;
40
+ function xhrCustomAttributeFunctionWithDefaults(callback) {
41
+ return function (span, xhr) {
42
+ setSpanStatusOnXMLHttpRequestError(span, xhr);
43
+ callback === null || callback === void 0 ? void 0 : callback(span, xhr);
44
+ };
45
+ }
46
+ exports.xhrCustomAttributeFunctionWithDefaults = xhrCustomAttributeFunctionWithDefaults;
33
47
  //# sourceMappingURL=instrumentationUtils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"instrumentationUtils.js","sourceRoot":"","sources":["../../src/instrumentationUtils.ts"],"names":[],"mappings":";;;AAAA,0CAA0D;AAQ1D;;;;;;;;GAQG;AACH,SAAgB,yBAAyB,CAAC,IAAU,EAAE,QAA+B,EAAE,MAA6B;IAClH,IAAM,cAAc,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IAEnE,IAAI,cAAc,IAAI,IAAI,EAAE;QAC1B,OAAO;KACR;IAED,IAAM,OAAO,GAAG,cAAc,KAAK,CAAC,CAAC;IACrC,IAAM,qBAAqB,GAAG,cAAc,IAAI,GAAG,IAAI,cAAc,GAAG,GAAG,CAAC;IAE5E,IAAI,OAAO,IAAI,qBAAqB,EAAE;QACpC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,oBAAc,CAAC,KAAK,EAAE,CAAC,CAAC;KAChD;AACH,CAAC;AAbD,8DAaC;AAED,SAAgB,wCAAwC,CAAC,QAAuC;IAC9F,OAAO,UAAC,IAAU,EAAE,OAA8B,EAAE,MAA6B;QAC/E,yBAAyB,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC;AALD,4FAKC","sourcesContent":["import { Span, SpanStatusCode } from '@opentelemetry/api';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\n\nexport interface FetchError {\n status?: number;\n message: string;\n}\n\n/**\n * Adds HTTP status code to every span.\n *\n * The fetch instrumentation does not always set the span status to error as defined by the spec.\n * To work around that issue we manually set the span status.\n *\n * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564\n * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status\n */\nexport function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError) {\n const httpStatusCode = result instanceof Error ? 0 : result.status;\n\n if (httpStatusCode == null) {\n return;\n }\n\n const isError = httpStatusCode === 0;\n const isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;\n\n if (isError || isClientOrServerError) {\n span.setStatus({ code: SpanStatusCode.ERROR });\n }\n}\n\nexport function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction) {\n return (span: Span, request: Request | RequestInit, result: Response | FetchError) => {\n setSpanStatusOnFetchError(span, request, result);\n callback?.(span, request, result);\n };\n}\n"]}
1
+ {"version":3,"file":"instrumentationUtils.js","sourceRoot":"","sources":["../../src/instrumentationUtils.ts"],"names":[],"mappings":";;;AAAA,0CAA0D;AAS1D;;;;;;;;GAQG;AACH,SAAgB,yBAAyB,CAAC,IAAU,EAAE,QAA+B,EAAE,MAA6B;IAClH,IAAM,cAAc,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IACnE,aAAa,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;AACtC,CAAC;AAHD,8DAGC;AAED,SAAgB,kCAAkC,CAAC,IAAU,EAAE,GAAmB;IAChF,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC;AAFD,gFAEC;AAED,SAAS,aAAa,CAAC,IAAU,EAAE,cAAuB;IACxD,IAAI,cAAc,IAAI,IAAI,EAAE;QAC1B,OAAO;KACR;IAED,IAAM,OAAO,GAAG,cAAc,KAAK,CAAC,CAAC;IACrC,IAAM,qBAAqB,GAAG,cAAc,IAAI,GAAG,IAAI,cAAc,GAAG,GAAG,CAAC;IAE5E,IAAI,OAAO,IAAI,qBAAqB,EAAE;QACpC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,oBAAc,CAAC,KAAK,EAAE,CAAC,CAAC;KAChD;AACH,CAAC;AAED,SAAgB,wCAAwC,CAAC,QAAuC;IAC9F,OAAO,UAAC,IAAU,EAAE,OAA8B,EAAE,MAA6B;QAC/E,yBAAyB,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC;AALD,4FAKC;AAED,SAAgB,sCAAsC,CAAC,QAAqC;IAC1F,OAAO,UAAC,IAAU,EAAE,GAAmB;QACrC,kCAAkC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC9C,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,GAAG,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC;AALD,wFAKC","sourcesContent":["import { Span, SpanStatusCode } from '@opentelemetry/api';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\nimport type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';\n\nexport interface FetchError {\n status?: number;\n message: string;\n}\n\n/**\n * Adds HTTP status code to every span.\n *\n * The fetch instrumentation does not always set the span status to error as defined by the spec.\n * To work around that issue we manually set the span status.\n *\n * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564\n * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status\n */\nexport function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError) {\n const httpStatusCode = result instanceof Error ? 0 : result.status;\n setSpanStatus(span, httpStatusCode);\n}\n\nexport function setSpanStatusOnXMLHttpRequestError(span: Span, xhr: XMLHttpRequest) {\n setSpanStatus(span, xhr.status);\n}\n\nfunction setSpanStatus(span: Span, httpStatusCode?: number) {\n if (httpStatusCode == null) {\n return;\n }\n\n const isError = httpStatusCode === 0;\n const isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;\n\n if (isError || isClientOrServerError) {\n span.setStatus({ code: SpanStatusCode.ERROR });\n }\n}\n\nexport function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction) {\n return (span: Span, request: Request | RequestInit, result: Response | FetchError) => {\n setSpanStatusOnFetchError(span, request, result);\n callback?.(span, request, result);\n };\n}\n\nexport function xhrCustomAttributeFunctionWithDefaults(callback?: XHRCustomAttributeFunction) {\n return (span: Span, xhr: XMLHttpRequest) => {\n setSpanStatusOnXMLHttpRequestError(span, xhr);\n callback?.(span, xhr);\n };\n}\n"]}
@@ -8,7 +8,7 @@ import { SEMRESATTRS_DEPLOYMENT_ENVIRONMENT, SEMRESATTRS_SERVICE_NAME, SEMRESATT
8
8
  import { BaseInstrumentation, VERSION } from '@grafana/faro-web-sdk';
9
9
  import { FaroTraceExporter } from './faroTraceExporter';
10
10
  import { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';
11
- import { fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';
11
+ import { fetchCustomAttributeFunctionWithDefaults, xhrCustomAttributeFunctionWithDefaults, } from './instrumentationUtils';
12
12
  import { getSamplingDecision } from './sampler';
13
13
  import { FaroSessionSpanProcessor } from './sessionSpanProcessor';
14
14
  // the providing of app name here is not great
@@ -22,7 +22,7 @@ export class TracingInstrumentation extends BaseInstrumentation {
22
22
  this.version = VERSION;
23
23
  }
24
24
  initialize() {
25
- var _a, _b, _c, _d, _e, _f, _g, _h;
25
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
26
26
  const options = this.options;
27
27
  const attributes = {};
28
28
  if (this.config.app.name) {
@@ -64,7 +64,9 @@ export class TracingInstrumentation extends BaseInstrumentation {
64
64
  fetchInstrumentationOptions: {
65
65
  applyCustomAttributesOnSpan: fetchCustomAttributeFunctionWithDefaults((_g = (_f = this.options.instrumentationOptions) === null || _f === void 0 ? void 0 : _f.fetchInstrumentationOptions) === null || _g === void 0 ? void 0 : _g.applyCustomAttributesOnSpan),
66
66
  },
67
- xhrInstrumentationOptions: Object.assign({}, (_h = this.options.instrumentationOptions) === null || _h === void 0 ? void 0 : _h.xhrInstrumentationOptions),
67
+ xhrInstrumentationOptions: {
68
+ applyCustomAttributesOnSpan: xhrCustomAttributeFunctionWithDefaults((_j = (_h = this.options.instrumentationOptions) === null || _h === void 0 ? void 0 : _h.xhrInstrumentationOptions) === null || _j === void 0 ? void 0 : _j.applyCustomAttributesOnSpan),
69
+ },
68
70
  }),
69
71
  });
70
72
  this.api.initOTEL(trace, context);
@@ -1 +1 @@
1
- {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,QAAQ,EAAsB,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EACL,kCAAkC,EAClC,wBAAwB,EACxB,6BAA6B,EAC7B,2BAA2B,GAC5B,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EAAE,mBAAmB,EAAa,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,wCAAwC,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,EAA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,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACpC,CAAC;IAEO,aAAa;QACnB,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,SAAoB,EAAE,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,CAAC,CAAC;IACjG,CAAC;;AA/EM,+CAAwB,GAAG,IAAI,CAAC","sourcesContent":["import { context, trace } from '@opentelemetry/api';\nimport { ZoneContextManager } from '@opentelemetry/context-zone';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { registerInstrumentations } from '@opentelemetry/instrumentation';\nimport { Resource, ResourceAttributes } from '@opentelemetry/resources';\nimport { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web';\nimport {\n SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nimport { BaseInstrumentation, Transport, VERSION } from '@grafana/faro-web-sdk';\n\nimport { FaroTraceExporter } from './faroTraceExporter';\nimport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\nimport { fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';\nimport { getSamplingDecision } from './sampler';\nimport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\nimport type { TracingInstrumentationOptions } from './types';\n\n// the providing of app name here is not great\n// should delay initialization and provide the full Faro config,\n// taking app name from it\n\nexport class TracingInstrumentation extends BaseInstrumentation {\n name = '@grafana/faro-web-tracing';\n version = VERSION;\n\n static SCHEDULED_BATCH_DELAY_MS = 1000;\n\n constructor(private options: TracingInstrumentationOptions = {}) {\n super();\n }\n\n initialize(): void {\n const options = this.options;\n const attributes: ResourceAttributes = {};\n\n if (this.config.app.name) {\n attributes[SEMRESATTRS_SERVICE_NAME] = this.config.app.name;\n }\n\n if (this.config.app.namespace) {\n attributes[SEMRESATTRS_SERVICE_NAMESPACE] = this.config.app.namespace;\n }\n\n if (this.config.app.version) {\n attributes[SEMRESATTRS_SERVICE_VERSION] = this.config.app.version;\n }\n\n if (this.config.app.environment) {\n attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT] = this.config.app.environment;\n }\n\n Object.assign(attributes, options.resourceAttributes);\n\n const resource = Resource.default().merge(new Resource(attributes));\n\n const provider = new WebTracerProvider({\n resource,\n sampler: {\n shouldSample: () => {\n return {\n decision: getSamplingDecision(this.api.getSession()),\n };\n },\n },\n });\n\n provider.addSpanProcessor(\n options.spanProcessor ??\n new FaroSessionSpanProcessor(\n new BatchSpanProcessor(new FaroTraceExporter({ api: this.api }), {\n scheduledDelayMillis: TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS,\n maxExportBatchSize: 30,\n }),\n this.metas\n )\n );\n\n provider.register({\n propagator: options.propagator ?? new W3CTraceContextPropagator(),\n contextManager: options.contextManager ?? new ZoneContextManager(),\n });\n\n registerInstrumentations({\n instrumentations:\n options.instrumentations ??\n getDefaultOTELInstrumentations({\n ignoreUrls: this.getIgnoreUrls(),\n propagateTraceHeaderCorsUrls: this.options.instrumentationOptions?.propagateTraceHeaderCorsUrls,\n fetchInstrumentationOptions: {\n applyCustomAttributesOnSpan: fetchCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.fetchInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n xhrInstrumentationOptions: {\n ...this.options.instrumentationOptions?.xhrInstrumentationOptions,\n },\n }),\n });\n\n this.api.initOTEL(trace, context);\n }\n\n private getIgnoreUrls(): Array<string | RegExp> {\n return this.transports.transports.flatMap((transport: Transport) => transport.getIgnoreUrls());\n }\n}\n"]}
1
+ {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,kBAAkB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U,mCAAI,IAAI,yBAAyB,EAAE;YACjE,cAAc,EAAE,MAAA,OAAO,CAAC,cAAc,mCAAI,IAAI,kBAAkB,EAAE;SACnE,CAAC,CAAC;QAEH,wBAAwB,CAAC;YACvB,gBAAgB,EACd,MAAA,OAAO,CAAC,gBAAgB,mCACxB,8BAA8B,CAAC;gBAC7B,UAAU,EAAE,IAAI,CAAC,aAAa,EAAE;gBAChC,4BAA4B,EAAE,MAAA,IAAI,CAAC,OAAO,CAAC,sBAAsB,0CAAE,4BAA4B;gBAC/F,2BAA2B,EAAE;oBAC3B,2BAA2B,EAAE,wCAAwC,CACnE,MAAA,MAAA,IAAI,CAAC,OAAO,CAAC,sBAAsB,0CAAE,2BAA2B,0CAAE,2BAA2B,CAC9F;iBACF;gBACD,yBAAyB,EAAE;oBACzB,2BAA2B,EAAE,sCAAsC,CACjE,MAAA,MAAA,IAAI,CAAC,OAAO,CAAC,sBAAsB,0CAAE,yBAAyB,0CAAE,2BAA2B,CAC5F;iBACF;aACF,CAAC;SACL,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACpC,CAAC;IAEO,aAAa;QACnB,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,SAAoB,EAAE,EAAE,CAAC,SAAS,CAAC,aAAa,EAAE,CAAC,CAAC;IACjG,CAAC;;AAjFM,+CAAwB,GAAG,IAAI,CAAC","sourcesContent":["import { context, trace } from '@opentelemetry/api';\nimport { ZoneContextManager } from '@opentelemetry/context-zone';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { registerInstrumentations } from '@opentelemetry/instrumentation';\nimport { Resource, ResourceAttributes } from '@opentelemetry/resources';\nimport { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web';\nimport {\n SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nimport { BaseInstrumentation, Transport, VERSION } from '@grafana/faro-web-sdk';\n\nimport { FaroTraceExporter } from './faroTraceExporter';\nimport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\nimport {\n fetchCustomAttributeFunctionWithDefaults,\n xhrCustomAttributeFunctionWithDefaults,\n} from './instrumentationUtils';\nimport { getSamplingDecision } from './sampler';\nimport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\nimport type { TracingInstrumentationOptions } from './types';\n\n// the providing of app name here is not great\n// should delay initialization and provide the full Faro config,\n// taking app name from it\n\nexport class TracingInstrumentation extends BaseInstrumentation {\n name = '@grafana/faro-web-tracing';\n version = VERSION;\n\n static SCHEDULED_BATCH_DELAY_MS = 1000;\n\n constructor(private options: TracingInstrumentationOptions = {}) {\n super();\n }\n\n initialize(): void {\n const options = this.options;\n const attributes: ResourceAttributes = {};\n\n if (this.config.app.name) {\n attributes[SEMRESATTRS_SERVICE_NAME] = this.config.app.name;\n }\n\n if (this.config.app.namespace) {\n attributes[SEMRESATTRS_SERVICE_NAMESPACE] = this.config.app.namespace;\n }\n\n if (this.config.app.version) {\n attributes[SEMRESATTRS_SERVICE_VERSION] = this.config.app.version;\n }\n\n if (this.config.app.environment) {\n attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT] = this.config.app.environment;\n }\n\n Object.assign(attributes, options.resourceAttributes);\n\n const resource = Resource.default().merge(new Resource(attributes));\n\n const provider = new WebTracerProvider({\n resource,\n sampler: {\n shouldSample: () => {\n return {\n decision: getSamplingDecision(this.api.getSession()),\n };\n },\n },\n });\n\n provider.addSpanProcessor(\n options.spanProcessor ??\n new FaroSessionSpanProcessor(\n new BatchSpanProcessor(new FaroTraceExporter({ api: this.api }), {\n scheduledDelayMillis: TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS,\n maxExportBatchSize: 30,\n }),\n this.metas\n )\n );\n\n provider.register({\n propagator: options.propagator ?? new W3CTraceContextPropagator(),\n contextManager: options.contextManager ?? new ZoneContextManager(),\n });\n\n registerInstrumentations({\n instrumentations:\n options.instrumentations ??\n getDefaultOTELInstrumentations({\n ignoreUrls: this.getIgnoreUrls(),\n propagateTraceHeaderCorsUrls: this.options.instrumentationOptions?.propagateTraceHeaderCorsUrls,\n fetchInstrumentationOptions: {\n applyCustomAttributesOnSpan: fetchCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.fetchInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n xhrInstrumentationOptions: {\n applyCustomAttributesOnSpan: xhrCustomAttributeFunctionWithDefaults(\n this.options.instrumentationOptions?.xhrInstrumentationOptions?.applyCustomAttributesOnSpan\n ),\n },\n }),\n });\n\n this.api.initOTEL(trace, context);\n }\n\n private getIgnoreUrls(): Array<string | RegExp> {\n return this.transports.transports.flatMap((transport: Transport) => transport.getIgnoreUrls());\n }\n}\n"]}
@@ -10,6 +10,12 @@ import { SpanStatusCode } from '@opentelemetry/api';
10
10
  */
11
11
  export function setSpanStatusOnFetchError(span, _request, result) {
12
12
  const httpStatusCode = result instanceof Error ? 0 : result.status;
13
+ setSpanStatus(span, httpStatusCode);
14
+ }
15
+ export function setSpanStatusOnXMLHttpRequestError(span, xhr) {
16
+ setSpanStatus(span, xhr.status);
17
+ }
18
+ function setSpanStatus(span, httpStatusCode) {
13
19
  if (httpStatusCode == null) {
14
20
  return;
15
21
  }
@@ -25,4 +31,10 @@ export function fetchCustomAttributeFunctionWithDefaults(callback) {
25
31
  callback === null || callback === void 0 ? void 0 : callback(span, request, result);
26
32
  };
27
33
  }
34
+ export function xhrCustomAttributeFunctionWithDefaults(callback) {
35
+ return (span, xhr) => {
36
+ setSpanStatusOnXMLHttpRequestError(span, xhr);
37
+ callback === null || callback === void 0 ? void 0 : callback(span, xhr);
38
+ };
39
+ }
28
40
  //# sourceMappingURL=instrumentationUtils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"instrumentationUtils.js","sourceRoot":"","sources":["../../src/instrumentationUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAQ1D;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CAAC,IAAU,EAAE,QAA+B,EAAE,MAA6B;IAClH,MAAM,cAAc,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IAEnE,IAAI,cAAc,IAAI,IAAI,EAAE;QAC1B,OAAO;KACR;IAED,MAAM,OAAO,GAAG,cAAc,KAAK,CAAC,CAAC;IACrC,MAAM,qBAAqB,GAAG,cAAc,IAAI,GAAG,IAAI,cAAc,GAAG,GAAG,CAAC;IAE5E,IAAI,OAAO,IAAI,qBAAqB,EAAE;QACpC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC;KAChD;AACH,CAAC;AAED,MAAM,UAAU,wCAAwC,CAAC,QAAuC;IAC9F,OAAO,CAAC,IAAU,EAAE,OAA8B,EAAE,MAA6B,EAAE,EAAE;QACnF,yBAAyB,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { Span, SpanStatusCode } from '@opentelemetry/api';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\n\nexport interface FetchError {\n status?: number;\n message: string;\n}\n\n/**\n * Adds HTTP status code to every span.\n *\n * The fetch instrumentation does not always set the span status to error as defined by the spec.\n * To work around that issue we manually set the span status.\n *\n * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564\n * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status\n */\nexport function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError) {\n const httpStatusCode = result instanceof Error ? 0 : result.status;\n\n if (httpStatusCode == null) {\n return;\n }\n\n const isError = httpStatusCode === 0;\n const isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;\n\n if (isError || isClientOrServerError) {\n span.setStatus({ code: SpanStatusCode.ERROR });\n }\n}\n\nexport function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction) {\n return (span: Span, request: Request | RequestInit, result: Response | FetchError) => {\n setSpanStatusOnFetchError(span, request, result);\n callback?.(span, request, result);\n };\n}\n"]}
1
+ {"version":3,"file":"instrumentationUtils.js","sourceRoot":"","sources":["../../src/instrumentationUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAS1D;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CAAC,IAAU,EAAE,QAA+B,EAAE,MAA6B;IAClH,MAAM,cAAc,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IACnE,aAAa,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,kCAAkC,CAAC,IAAU,EAAE,GAAmB;IAChF,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,aAAa,CAAC,IAAU,EAAE,cAAuB;IACxD,IAAI,cAAc,IAAI,IAAI,EAAE;QAC1B,OAAO;KACR;IAED,MAAM,OAAO,GAAG,cAAc,KAAK,CAAC,CAAC;IACrC,MAAM,qBAAqB,GAAG,cAAc,IAAI,GAAG,IAAI,cAAc,GAAG,GAAG,CAAC;IAE5E,IAAI,OAAO,IAAI,qBAAqB,EAAE;QACpC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC;KAChD;AACH,CAAC;AAED,MAAM,UAAU,wCAAwC,CAAC,QAAuC;IAC9F,OAAO,CAAC,IAAU,EAAE,OAA8B,EAAE,MAA6B,EAAE,EAAE;QACnF,yBAAyB,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,sCAAsC,CAAC,QAAqC;IAC1F,OAAO,CAAC,IAAU,EAAE,GAAmB,EAAE,EAAE;QACzC,kCAAkC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC9C,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,IAAI,EAAE,GAAG,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { Span, SpanStatusCode } from '@opentelemetry/api';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\nimport type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';\n\nexport interface FetchError {\n status?: number;\n message: string;\n}\n\n/**\n * Adds HTTP status code to every span.\n *\n * The fetch instrumentation does not always set the span status to error as defined by the spec.\n * To work around that issue we manually set the span status.\n *\n * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564\n * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status\n */\nexport function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError) {\n const httpStatusCode = result instanceof Error ? 0 : result.status;\n setSpanStatus(span, httpStatusCode);\n}\n\nexport function setSpanStatusOnXMLHttpRequestError(span: Span, xhr: XMLHttpRequest) {\n setSpanStatus(span, xhr.status);\n}\n\nfunction setSpanStatus(span: Span, httpStatusCode?: number) {\n if (httpStatusCode == null) {\n return;\n }\n\n const isError = httpStatusCode === 0;\n const isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;\n\n if (isError || isClientOrServerError) {\n span.setStatus({ code: SpanStatusCode.ERROR });\n }\n}\n\nexport function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction) {\n return (span: Span, request: Request | RequestInit, result: Response | FetchError) => {\n setSpanStatusOnFetchError(span, request, result);\n callback?.(span, request, result);\n };\n}\n\nexport function xhrCustomAttributeFunctionWithDefaults(callback?: XHRCustomAttributeFunction) {\n return (span: Span, xhr: XMLHttpRequest) => {\n setSpanStatusOnXMLHttpRequestError(span, xhr);\n callback?.(span, xhr);\n };\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import { Span } from '@opentelemetry/api';
2
2
  import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
3
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
3
4
  export interface FetchError {
4
5
  status?: number;
5
6
  message: string;
@@ -14,4 +15,6 @@ export interface FetchError {
14
15
  * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
15
16
  */
16
17
  export declare function setSpanStatusOnFetchError(span: Span, _request: Request | RequestInit, result: Response | FetchError): void;
18
+ export declare function setSpanStatusOnXMLHttpRequestError(span: Span, xhr: XMLHttpRequest): void;
17
19
  export declare function fetchCustomAttributeFunctionWithDefaults(callback?: FetchCustomAttributeFunction): (span: Span, request: Request | RequestInit, result: Response | FetchError) => void;
20
+ export declare function xhrCustomAttributeFunctionWithDefaults(callback?: XHRCustomAttributeFunction): (span: Span, xhr: XMLHttpRequest) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/faro-web-tracing",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "Faro web tracing implementation.",
5
5
  "keywords": [
6
6
  "observability",
@@ -52,7 +52,7 @@
52
52
  "quality:circular-deps": "madge --circular ."
53
53
  },
54
54
  "dependencies": {
55
- "@grafana/faro-web-sdk": "^1.8.1",
55
+ "@grafana/faro-web-sdk": "^1.8.2",
56
56
  "@opentelemetry/api": "^1.9.0",
57
57
  "@opentelemetry/context-zone": "1.21.0",
58
58
  "@opentelemetry/core": "^1.25.0",
@@ -68,5 +68,5 @@
68
68
  "publishConfig": {
69
69
  "access": "public"
70
70
  },
71
- "gitHead": "08d0f63dba5ff82c9b29b2f91df9d790514ea8d5"
71
+ "gitHead": "ce860054636cc98bc3ad86420c3da19cccfc971a"
72
72
  }