@grafana/faro-web-tracing 1.8.0 → 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.
@@ -1,7 +1,2 @@
1
- import type { InstrumentationOption, MatchUrlDefinitions } from './types';
2
- type DefaultInstrumentationsOptions = {
3
- ignoreUrls?: MatchUrlDefinitions;
4
- propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;
5
- };
1
+ import type { DefaultInstrumentationsOptions, InstrumentationOption } from './types';
6
2
  export declare function getDefaultOTELInstrumentations(options?: DefaultInstrumentationsOptions): InstrumentationOption[];
7
- export {};
@@ -4,3 +4,4 @@ export { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations
4
4
  export { TracingInstrumentation } from './instrumentation';
5
5
  export { getSamplingDecision } from './sampler';
6
6
  export type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';
7
+ export { setSpanStatusOnFetchError, fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';
@@ -0,0 +1,20 @@
1
+ import { Span } from '@opentelemetry/api';
2
+ import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
3
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
4
+ export interface FetchError {
5
+ status?: number;
6
+ message: string;
7
+ }
8
+ /**
9
+ * Adds HTTP status code to every span.
10
+ *
11
+ * The fetch instrumentation does not always set the span status to error as defined by the spec.
12
+ * To work around that issue we manually set the span status.
13
+ *
14
+ * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564
15
+ * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
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;
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;
@@ -1,5 +1,7 @@
1
1
  import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';
2
2
  import type { Instrumentation } from '@opentelemetry/instrumentation';
3
+ import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
4
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
3
5
  import type { ResourceAttributes } from '@opentelemetry/resources';
4
6
  import type { SpanProcessor } from '@opentelemetry/sdk-trace-web';
5
7
  import type { Patterns } from '@grafana/faro-core';
@@ -14,8 +16,16 @@ export interface TracingInstrumentationOptions {
14
16
  contextManager?: ContextManager;
15
17
  instrumentations?: InstrumentationOption[];
16
18
  spanProcessor?: SpanProcessor;
17
- instrumentationOptions?: {
18
- propagateTraceHeaderCorsUrls: MatchUrlDefinitions;
19
- };
19
+ instrumentationOptions?: Omit<DefaultInstrumentationsOptions, 'ignoreUrls'>;
20
20
  }
21
21
  export type MatchUrlDefinitions = Patterns;
22
+ export type DefaultInstrumentationsOptions = {
23
+ ignoreUrls?: MatchUrlDefinitions;
24
+ propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;
25
+ fetchInstrumentationOptions?: {
26
+ applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
27
+ };
28
+ xhrInstrumentationOptions?: {
29
+ applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;
30
+ };
31
+ };
@@ -1,15 +1,41 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
2
24
  Object.defineProperty(exports, "__esModule", { value: true });
3
25
  exports.getDefaultOTELInstrumentations = void 0;
4
26
  var instrumentation_fetch_1 = require("@opentelemetry/instrumentation-fetch");
5
27
  var instrumentation_xml_http_request_1 = require("@opentelemetry/instrumentation-xml-http-request");
6
- var initialIntrumentationsOptions = {
28
+ var initialInstrumentationsOptions = {
7
29
  ignoreUrls: [],
8
30
  propagateTraceHeaderCorsUrls: [],
9
31
  };
10
32
  function getDefaultOTELInstrumentations(options) {
11
- if (options === void 0) { options = initialIntrumentationsOptions; }
12
- return [new instrumentation_fetch_1.FetchInstrumentation(options), new instrumentation_xml_http_request_1.XMLHttpRequestInstrumentation(options)];
33
+ if (options === void 0) { options = initialInstrumentationsOptions; }
34
+ var fetchInstrumentationOptions = options.fetchInstrumentationOptions, xhrInstrumentationOptions = options.xhrInstrumentationOptions, sharedOptions = __rest(options, ["fetchInstrumentationOptions", "xhrInstrumentationOptions"]);
35
+ return [
36
+ new instrumentation_fetch_1.FetchInstrumentation(__assign(__assign({}, sharedOptions), fetchInstrumentationOptions)),
37
+ new instrumentation_xml_http_request_1.XMLHttpRequestInstrumentation(__assign(__assign({}, sharedOptions), xhrInstrumentationOptions)),
38
+ ];
13
39
  }
14
40
  exports.getDefaultOTELInstrumentations = getDefaultOTELInstrumentations;
15
41
  //# sourceMappingURL=getDefaultOTELInstrumentations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getDefaultOTELInstrumentations.js","sourceRoot":"","sources":["../../src/getDefaultOTELInstrumentations.ts"],"names":[],"mappings":";;;AAAA,8EAA4E;AAC5E,oGAAgG;AAShG,IAAM,6BAA6B,GAAG;IACpC,UAAU,EAAE,EAAE;IACd,4BAA4B,EAAE,EAAE;CACjC,CAAC;AAEF,SAAgB,8BAA8B,CAC5C,OAAuE;IAAvE,wBAAA,EAAA,uCAAuE;IAEvE,OAAO,CAAC,IAAI,4CAAoB,CAAC,OAAO,CAAC,EAAE,IAAI,gEAA6B,CAAC,OAAO,CAAC,CAAC,CAAC;AACzF,CAAC;AAJD,wEAIC","sourcesContent":["import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';\nimport { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';\n\nimport type { InstrumentationOption, MatchUrlDefinitions } from './types';\n\ntype DefaultInstrumentationsOptions = {\n ignoreUrls?: MatchUrlDefinitions;\n propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;\n};\n\nconst initialIntrumentationsOptions = {\n ignoreUrls: [],\n propagateTraceHeaderCorsUrls: [],\n};\n\nexport function getDefaultOTELInstrumentations(\n options: DefaultInstrumentationsOptions = initialIntrumentationsOptions\n): InstrumentationOption[] {\n return [new FetchInstrumentation(options), new XMLHttpRequestInstrumentation(options)];\n}\n"]}
1
+ {"version":3,"file":"getDefaultOTELInstrumentations.js","sourceRoot":"","sources":["../../src/getDefaultOTELInstrumentations.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8EAA4E;AAC5E,oGAAgG;AAIhG,IAAM,8BAA8B,GAAG;IACrC,UAAU,EAAE,EAAE;IACd,4BAA4B,EAAE,EAAE;CACjC,CAAC;AAEF,SAAgB,8BAA8B,CAC5C,OAAwE;IAAxE,wBAAA,EAAA,wCAAwE;IAEhE,IAAA,2BAA2B,GAAkD,OAAO,4BAAzD,EAAE,yBAAyB,GAAuB,OAAO,0BAA9B,EAAK,aAAa,UAAK,OAAO,EAAtF,4DAA4E,CAAF,CAAa;IAE7F,OAAO;QACL,IAAI,4CAAoB,uBAAM,aAAa,GAAK,2BAA2B,EAAG;QAC9E,IAAI,gEAA6B,uBAAM,aAAa,GAAK,yBAAyB,EAAG;KACtF,CAAC;AACJ,CAAC;AATD,wEASC","sourcesContent":["import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';\nimport { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';\n\nimport type { DefaultInstrumentationsOptions, InstrumentationOption } from './types';\n\nconst initialInstrumentationsOptions = {\n ignoreUrls: [],\n propagateTraceHeaderCorsUrls: [],\n};\n\nexport function getDefaultOTELInstrumentations(\n options: DefaultInstrumentationsOptions = initialInstrumentationsOptions\n): InstrumentationOption[] {\n const { fetchInstrumentationOptions, xhrInstrumentationOptions, ...sharedOptions } = options;\n\n return [\n new FetchInstrumentation({ ...sharedOptions, ...fetchInstrumentationOptions }),\n new XMLHttpRequestInstrumentation({ ...sharedOptions, ...xhrInstrumentationOptions }),\n ];\n}\n"]}
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSamplingDecision = exports.TracingInstrumentation = exports.getDefaultOTELInstrumentations = exports.FaroSessionSpanProcessor = exports.FaroTraceExporter = void 0;
3
+ exports.fetchCustomAttributeFunctionWithDefaults = exports.setSpanStatusOnFetchError = exports.getSamplingDecision = exports.TracingInstrumentation = exports.getDefaultOTELInstrumentations = exports.FaroSessionSpanProcessor = exports.FaroTraceExporter = void 0;
4
4
  var faroTraceExporter_1 = require("./faroTraceExporter");
5
5
  Object.defineProperty(exports, "FaroTraceExporter", { enumerable: true, get: function () { return faroTraceExporter_1.FaroTraceExporter; } });
6
6
  var sessionSpanProcessor_1 = require("./sessionSpanProcessor");
@@ -11,4 +11,7 @@ var instrumentation_1 = require("./instrumentation");
11
11
  Object.defineProperty(exports, "TracingInstrumentation", { enumerable: true, get: function () { return instrumentation_1.TracingInstrumentation; } });
12
12
  var sampler_1 = require("./sampler");
13
13
  Object.defineProperty(exports, "getSamplingDecision", { enumerable: true, get: function () { return sampler_1.getSamplingDecision; } });
14
+ var instrumentationUtils_1 = require("./instrumentationUtils");
15
+ Object.defineProperty(exports, "setSpanStatusOnFetchError", { enumerable: true, get: function () { return instrumentationUtils_1.setSpanStatusOnFetchError; } });
16
+ Object.defineProperty(exports, "fetchCustomAttributeFunctionWithDefaults", { enumerable: true, get: function () { return instrumentationUtils_1.fetchCustomAttributeFunctionWithDefaults; } });
14
17
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA;AAE1B,+DAAkE;AAAzD,gIAAA,wBAAwB,OAAA;AAEjC,mFAAkF;AAAzE,gJAAA,8BAA8B,OAAA;AAEvC,qDAA2D;AAAlD,yHAAA,sBAAsB,OAAA;AAE/B,qCAAgD;AAAvC,8GAAA,mBAAmB,OAAA","sourcesContent":["export { FaroTraceExporter } from './faroTraceExporter';\n\nexport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\n\nexport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\n\nexport { TracingInstrumentation } from './instrumentation';\n\nexport { getSamplingDecision } from './sampler';\n\nexport type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA;AAE1B,+DAAkE;AAAzD,gIAAA,wBAAwB,OAAA;AAEjC,mFAAkF;AAAzE,gJAAA,8BAA8B,OAAA;AAEvC,qDAA2D;AAAlD,yHAAA,sBAAsB,OAAA;AAE/B,qCAAgD;AAAvC,8GAAA,mBAAmB,OAAA;AAI5B,+DAA6G;AAApG,iIAAA,yBAAyB,OAAA;AAAE,gJAAA,wCAAwC,OAAA","sourcesContent":["export { FaroTraceExporter } from './faroTraceExporter';\n\nexport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\n\nexport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\n\nexport { TracingInstrumentation } from './instrumentation';\n\nexport { getSamplingDecision } from './sampler';\n\nexport type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';\n\nexport { setSpanStatusOnFetchError, fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';\n"]}
@@ -26,6 +26,7 @@ var semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
26
26
  var faro_web_sdk_1 = require("@grafana/faro-web-sdk");
27
27
  var faroTraceExporter_1 = require("./faroTraceExporter");
28
28
  var getDefaultOTELInstrumentations_1 = require("./getDefaultOTELInstrumentations");
29
+ var instrumentationUtils_1 = require("./instrumentationUtils");
29
30
  var sampler_1 = require("./sampler");
30
31
  var sessionSpanProcessor_1 = require("./sessionSpanProcessor");
31
32
  // the providing of app name here is not great
@@ -43,7 +44,7 @@ var TracingInstrumentation = /** @class */ (function (_super) {
43
44
  }
44
45
  TracingInstrumentation.prototype.initialize = function () {
45
46
  var _this = this;
46
- var _a, _b, _c, _d, _e;
47
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
47
48
  var options = this.options;
48
49
  var attributes = {};
49
50
  if (this.config.app.name) {
@@ -82,6 +83,12 @@ var TracingInstrumentation = /** @class */ (function (_super) {
82
83
  instrumentations: (_d = options.instrumentations) !== null && _d !== void 0 ? _d : (0, getDefaultOTELInstrumentations_1.getDefaultOTELInstrumentations)({
83
84
  ignoreUrls: this.getIgnoreUrls(),
84
85
  propagateTraceHeaderCorsUrls: (_e = this.options.instrumentationOptions) === null || _e === void 0 ? void 0 : _e.propagateTraceHeaderCorsUrls,
86
+ fetchInstrumentationOptions: {
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),
88
+ },
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
+ },
85
92
  }),
86
93
  });
87
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,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,GAAG,2BAA2B,CAAC;QACnC,aAAO,GAAG,sBAAO,CAAC;;IAMlB,CAAC;IAED,2CAAU,GAAV;QAAA,iBA6DC;;QA5DC,IAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAC7B,IAAM,UAAU,GAAuB,EAAE,CAAC;QAE1C,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE;YACxB,UAAU,CAAC,+CAAwB,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;SAC7D;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE;YAC7B,UAAU,CAAC,oDAA6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,MAAA,OAAO,CAAC,cAAc,mCAAI,IAAI,iCAAkB,EAAE;SACnE,CAAC,CAAC;QAEH,IAAA,0CAAwB,EAAC;YACvB,gBAAgB,EACd,MAAA,OAAO,CAAC,gBAAgB,mCACxB,IAAA,+DAA8B,EAAC;gBAC7B,UAAU,EAAE,IAAI,CAAC,aAAa,EAAE;gBAChC,4BAA4B,EAAE,MAAA,IAAI,CAAC,OAAO,CAAC,sBAAsB,0CAAE,4BAA4B;aAChG,CAAC;SACL,CAAC,CAAC;QAEH,IAAI,CAAC,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;IAvEM,+CAAwB,GAAG,IAAI,CAAC;IAwEzC,6BAAC;CAAA,AA5ED,CAA4C,kCAAmB,GA4E9D;AA5EY,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 { 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 }),\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,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,GAAG,2BAA2B,CAAC;QACnC,aAAO,GAAG,sBAAO,CAAC;;IAMlB,CAAC;IAED,2CAAU,GAAV;QAAA,iBAuEC;;QAtEC,IAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAC7B,IAAM,UAAU,GAAuB,EAAE,CAAC;QAE1C,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE;YACxB,UAAU,CAAC,+CAAwB,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;SAC7D;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE;YAC7B,UAAU,CAAC,oDAA6B,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC;SACvE;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE;YAC3B,UAAU,CAAC,kDAA2B,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC;SACnE;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE;YAC/B,UAAU,CAAC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,2BAA2B,EAAE;oBAC3B,2BAA2B,EAAE,IAAA,+DAAwC,EACnE,MAAA,MAAA,IAAI,CAAC,OAAO,CAAC,sBAAsB,0CAAE,2BAA2B,0CAAE,2BAA2B,CAC9F;iBACF;gBACD,yBAAyB,EAAE;oBACzB,2BAA2B,EAAE,IAAA,6DAAsC,EACjE,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,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"]}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xhrCustomAttributeFunctionWithDefaults = exports.fetchCustomAttributeFunctionWithDefaults = exports.setSpanStatusOnXMLHttpRequestError = exports.setSpanStatusOnFetchError = void 0;
4
+ var api_1 = require("@opentelemetry/api");
5
+ /**
6
+ * Adds HTTP status code to every span.
7
+ *
8
+ * The fetch instrumentation does not always set the span status to error as defined by the spec.
9
+ * To work around that issue we manually set the span status.
10
+ *
11
+ * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564
12
+ * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
13
+ */
14
+ function setSpanStatusOnFetchError(span, _request, result) {
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) {
24
+ if (httpStatusCode == null) {
25
+ return;
26
+ }
27
+ var isError = httpStatusCode === 0;
28
+ var isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;
29
+ if (isError || isClientOrServerError) {
30
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR });
31
+ }
32
+ }
33
+ function fetchCustomAttributeFunctionWithDefaults(callback) {
34
+ return function (span, request, result) {
35
+ setSpanStatusOnFetchError(span, request, result);
36
+ callback === null || callback === void 0 ? void 0 : callback(span, request, result);
37
+ };
38
+ }
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;
47
+ //# sourceMappingURL=instrumentationUtils.js.map
@@ -0,0 +1 @@
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"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';\nimport type { Instrumentation } from '@opentelemetry/instrumentation';\nimport type { ResourceAttributes } from '@opentelemetry/resources';\nimport type { SpanProcessor } from '@opentelemetry/sdk-trace-web';\n\nimport type { Patterns } from '@grafana/faro-core';\nimport type { API } from '@grafana/faro-web-sdk';\n\n// type got remove by with experimental/v0.52.0 and is replaced by the following type:\n// See: https://github.com/open-telemetry/opentelemetry-js/releases/tag/experimental%2Fv0.52.0\nexport type InstrumentationOption = Instrumentation | Instrumentation[];\n\nexport interface FaroTraceExporterConfig {\n api: API;\n}\n\nexport interface TracingInstrumentationOptions {\n resourceAttributes?: ResourceAttributes;\n propagator?: TextMapPropagator;\n contextManager?: ContextManager;\n instrumentations?: InstrumentationOption[];\n spanProcessor?: SpanProcessor;\n instrumentationOptions?: {\n propagateTraceHeaderCorsUrls: MatchUrlDefinitions;\n };\n}\n\nexport type MatchUrlDefinitions = Patterns;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';\nimport type { Instrumentation } from '@opentelemetry/instrumentation';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\nimport type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';\nimport type { ResourceAttributes } from '@opentelemetry/resources';\nimport type { SpanProcessor } from '@opentelemetry/sdk-trace-web';\n\nimport type { Patterns } from '@grafana/faro-core';\nimport type { API } from '@grafana/faro-web-sdk';\n\n// type got remove by with experimental/v0.52.0 and is replaced by the following type:\n// See: https://github.com/open-telemetry/opentelemetry-js/releases/tag/experimental%2Fv0.52.0\nexport type InstrumentationOption = Instrumentation | Instrumentation[];\n\nexport interface FaroTraceExporterConfig {\n api: API;\n}\n\nexport interface TracingInstrumentationOptions {\n resourceAttributes?: ResourceAttributes;\n propagator?: TextMapPropagator;\n contextManager?: ContextManager;\n instrumentations?: InstrumentationOption[];\n spanProcessor?: SpanProcessor;\n instrumentationOptions?: Omit<DefaultInstrumentationsOptions, 'ignoreUrls'>;\n}\n\nexport type MatchUrlDefinitions = Patterns;\n\nexport type DefaultInstrumentationsOptions = {\n ignoreUrls?: MatchUrlDefinitions;\n propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;\n\n fetchInstrumentationOptions?: {\n applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;\n };\n\n xhrInstrumentationOptions?: {\n applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;\n };\n};\n"]}
@@ -1,10 +1,25 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
2
13
  import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';
3
- const initialIntrumentationsOptions = {
14
+ const initialInstrumentationsOptions = {
4
15
  ignoreUrls: [],
5
16
  propagateTraceHeaderCorsUrls: [],
6
17
  };
7
- export function getDefaultOTELInstrumentations(options = initialIntrumentationsOptions) {
8
- return [new FetchInstrumentation(options), new XMLHttpRequestInstrumentation(options)];
18
+ export function getDefaultOTELInstrumentations(options = initialInstrumentationsOptions) {
19
+ const { fetchInstrumentationOptions, xhrInstrumentationOptions } = options, sharedOptions = __rest(options, ["fetchInstrumentationOptions", "xhrInstrumentationOptions"]);
20
+ return [
21
+ new FetchInstrumentation(Object.assign(Object.assign({}, sharedOptions), fetchInstrumentationOptions)),
22
+ new XMLHttpRequestInstrumentation(Object.assign(Object.assign({}, sharedOptions), xhrInstrumentationOptions)),
23
+ ];
9
24
  }
10
25
  //# sourceMappingURL=getDefaultOTELInstrumentations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getDefaultOTELInstrumentations.js","sourceRoot":"","sources":["../../src/getDefaultOTELInstrumentations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAC5E,OAAO,EAAE,6BAA6B,EAAE,MAAM,iDAAiD,CAAC;AAShG,MAAM,6BAA6B,GAAG;IACpC,UAAU,EAAE,EAAE;IACd,4BAA4B,EAAE,EAAE;CACjC,CAAC;AAEF,MAAM,UAAU,8BAA8B,CAC5C,UAA0C,6BAA6B;IAEvE,OAAO,CAAC,IAAI,oBAAoB,CAAC,OAAO,CAAC,EAAE,IAAI,6BAA6B,CAAC,OAAO,CAAC,CAAC,CAAC;AACzF,CAAC","sourcesContent":["import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';\nimport { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';\n\nimport type { InstrumentationOption, MatchUrlDefinitions } from './types';\n\ntype DefaultInstrumentationsOptions = {\n ignoreUrls?: MatchUrlDefinitions;\n propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;\n};\n\nconst initialIntrumentationsOptions = {\n ignoreUrls: [],\n propagateTraceHeaderCorsUrls: [],\n};\n\nexport function getDefaultOTELInstrumentations(\n options: DefaultInstrumentationsOptions = initialIntrumentationsOptions\n): InstrumentationOption[] {\n return [new FetchInstrumentation(options), new XMLHttpRequestInstrumentation(options)];\n}\n"]}
1
+ {"version":3,"file":"getDefaultOTELInstrumentations.js","sourceRoot":"","sources":["../../src/getDefaultOTELInstrumentations.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAC5E,OAAO,EAAE,6BAA6B,EAAE,MAAM,iDAAiD,CAAC;AAIhG,MAAM,8BAA8B,GAAG;IACrC,UAAU,EAAE,EAAE;IACd,4BAA4B,EAAE,EAAE;CACjC,CAAC;AAEF,MAAM,UAAU,8BAA8B,CAC5C,UAA0C,8BAA8B;IAExE,MAAM,EAAE,2BAA2B,EAAE,yBAAyB,KAAuB,OAAO,EAAzB,aAAa,UAAK,OAAO,EAAtF,4DAA4E,CAAU,CAAC;IAE7F,OAAO;QACL,IAAI,oBAAoB,iCAAM,aAAa,GAAK,2BAA2B,EAAG;QAC9E,IAAI,6BAA6B,iCAAM,aAAa,GAAK,yBAAyB,EAAG;KACtF,CAAC;AACJ,CAAC","sourcesContent":["import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';\nimport { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';\n\nimport type { DefaultInstrumentationsOptions, InstrumentationOption } from './types';\n\nconst initialInstrumentationsOptions = {\n ignoreUrls: [],\n propagateTraceHeaderCorsUrls: [],\n};\n\nexport function getDefaultOTELInstrumentations(\n options: DefaultInstrumentationsOptions = initialInstrumentationsOptions\n): InstrumentationOption[] {\n const { fetchInstrumentationOptions, xhrInstrumentationOptions, ...sharedOptions } = options;\n\n return [\n new FetchInstrumentation({ ...sharedOptions, ...fetchInstrumentationOptions }),\n new XMLHttpRequestInstrumentation({ ...sharedOptions, ...xhrInstrumentationOptions }),\n ];\n}\n"]}
package/dist/esm/index.js CHANGED
@@ -3,4 +3,5 @@ export { FaroSessionSpanProcessor } from './sessionSpanProcessor';
3
3
  export { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';
4
4
  export { TracingInstrumentation } from './instrumentation';
5
5
  export { getSamplingDecision } from './sampler';
6
+ export { setSpanStatusOnFetchError, fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAElE,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAElF,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC","sourcesContent":["export { FaroTraceExporter } from './faroTraceExporter';\n\nexport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\n\nexport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\n\nexport { TracingInstrumentation } from './instrumentation';\n\nexport { getSamplingDecision } from './sampler';\n\nexport type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAElE,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAElF,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAIhD,OAAO,EAAE,yBAAyB,EAAE,wCAAwC,EAAE,MAAM,wBAAwB,CAAC","sourcesContent":["export { FaroTraceExporter } from './faroTraceExporter';\n\nexport { FaroSessionSpanProcessor } from './sessionSpanProcessor';\n\nexport { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations';\n\nexport { TracingInstrumentation } from './instrumentation';\n\nexport { getSamplingDecision } from './sampler';\n\nexport type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';\n\nexport { setSpanStatusOnFetchError, fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';\n"]}
@@ -8,6 +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, xhrCustomAttributeFunctionWithDefaults, } from './instrumentationUtils';
11
12
  import { getSamplingDecision } from './sampler';
12
13
  import { FaroSessionSpanProcessor } from './sessionSpanProcessor';
13
14
  // the providing of app name here is not great
@@ -21,7 +22,7 @@ export class TracingInstrumentation extends BaseInstrumentation {
21
22
  this.version = VERSION;
22
23
  }
23
24
  initialize() {
24
- var _a, _b, _c, _d, _e;
25
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
25
26
  const options = this.options;
26
27
  const attributes = {};
27
28
  if (this.config.app.name) {
@@ -60,6 +61,12 @@ export class TracingInstrumentation extends BaseInstrumentation {
60
61
  instrumentations: (_d = options.instrumentations) !== null && _d !== void 0 ? _d : getDefaultOTELInstrumentations({
61
62
  ignoreUrls: this.getIgnoreUrls(),
62
63
  propagateTraceHeaderCorsUrls: (_e = this.options.instrumentationOptions) === null || _e === void 0 ? void 0 : _e.propagateTraceHeaderCorsUrls,
64
+ fetchInstrumentationOptions: {
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
+ },
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
+ },
63
70
  }),
64
71
  });
65
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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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 { 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 }),\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,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,EAA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AE,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"]}
@@ -0,0 +1,40 @@
1
+ import { SpanStatusCode } from '@opentelemetry/api';
2
+ /**
3
+ * Adds HTTP status code to every span.
4
+ *
5
+ * The fetch instrumentation does not always set the span status to error as defined by the spec.
6
+ * To work around that issue we manually set the span status.
7
+ *
8
+ * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564
9
+ * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
10
+ */
11
+ export function setSpanStatusOnFetchError(span, _request, result) {
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) {
19
+ if (httpStatusCode == null) {
20
+ return;
21
+ }
22
+ const isError = httpStatusCode === 0;
23
+ const isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600;
24
+ if (isError || isClientOrServerError) {
25
+ span.setStatus({ code: SpanStatusCode.ERROR });
26
+ }
27
+ }
28
+ export function fetchCustomAttributeFunctionWithDefaults(callback) {
29
+ return (span, request, result) => {
30
+ setSpanStatusOnFetchError(span, request, result);
31
+ callback === null || callback === void 0 ? void 0 : callback(span, request, result);
32
+ };
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
+ }
40
+ //# sourceMappingURL=instrumentationUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentationUtils.js","sourceRoot":"","sources":["../../src/inst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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 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';\nimport type { Instrumentation } from '@opentelemetry/instrumentation';\nimport type { ResourceAttributes } from '@opentelemetry/resources';\nimport type { SpanProcessor } from '@opentelemetry/sdk-trace-web';\n\nimport type { Patterns } from '@grafana/faro-core';\nimport type { API } from '@grafana/faro-web-sdk';\n\n// type got remove by with experimental/v0.52.0 and is replaced by the following type:\n// See: https://github.com/open-telemetry/opentelemetry-js/releases/tag/experimental%2Fv0.52.0\nexport type InstrumentationOption = Instrumentation | Instrumentation[];\n\nexport interface FaroTraceExporterConfig {\n api: API;\n}\n\nexport interface TracingInstrumentationOptions {\n resourceAttributes?: ResourceAttributes;\n propagator?: TextMapPropagator;\n contextManager?: ContextManager;\n instrumentations?: InstrumentationOption[];\n spanProcessor?: SpanProcessor;\n instrumentationOptions?: {\n propagateTraceHeaderCorsUrls: MatchUrlDefinitions;\n };\n}\n\nexport type MatchUrlDefinitions = Patterns;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';\nimport type { Instrumentation } from '@opentelemetry/instrumentation';\nimport type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';\nimport type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';\nimport type { ResourceAttributes } from '@opentelemetry/resources';\nimport type { SpanProcessor } from '@opentelemetry/sdk-trace-web';\n\nimport type { Patterns } from '@grafana/faro-core';\nimport type { API } from '@grafana/faro-web-sdk';\n\n// type got remove by with experimental/v0.52.0 and is replaced by the following type:\n// See: https://github.com/open-telemetry/opentelemetry-js/releases/tag/experimental%2Fv0.52.0\nexport type InstrumentationOption = Instrumentation | Instrumentation[];\n\nexport interface FaroTraceExporterConfig {\n api: API;\n}\n\nexport interface TracingInstrumentationOptions {\n resourceAttributes?: ResourceAttributes;\n propagator?: TextMapPropagator;\n contextManager?: ContextManager;\n instrumentations?: InstrumentationOption[];\n spanProcessor?: SpanProcessor;\n instrumentationOptions?: Omit<DefaultInstrumentationsOptions, 'ignoreUrls'>;\n}\n\nexport type MatchUrlDefinitions = Patterns;\n\nexport type DefaultInstrumentationsOptions = {\n ignoreUrls?: MatchUrlDefinitions;\n propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;\n\n fetchInstrumentationOptions?: {\n applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;\n };\n\n xhrInstrumentationOptions?: {\n applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;\n };\n};\n"]}
@@ -1,7 +1,2 @@
1
- import type { InstrumentationOption, MatchUrlDefinitions } from './types';
2
- type DefaultInstrumentationsOptions = {
3
- ignoreUrls?: MatchUrlDefinitions;
4
- propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;
5
- };
1
+ import type { DefaultInstrumentationsOptions, InstrumentationOption } from './types';
6
2
  export declare function getDefaultOTELInstrumentations(options?: DefaultInstrumentationsOptions): InstrumentationOption[];
7
- export {};
@@ -4,3 +4,4 @@ export { getDefaultOTELInstrumentations } from './getDefaultOTELInstrumentations
4
4
  export { TracingInstrumentation } from './instrumentation';
5
5
  export { getSamplingDecision } from './sampler';
6
6
  export type { FaroTraceExporterConfig, TracingInstrumentationOptions } from './types';
7
+ export { setSpanStatusOnFetchError, fetchCustomAttributeFunctionWithDefaults } from './instrumentationUtils';
@@ -0,0 +1,20 @@
1
+ import { Span } from '@opentelemetry/api';
2
+ import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
3
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
4
+ export interface FetchError {
5
+ status?: number;
6
+ message: string;
7
+ }
8
+ /**
9
+ * Adds HTTP status code to every span.
10
+ *
11
+ * The fetch instrumentation does not always set the span status to error as defined by the spec.
12
+ * To work around that issue we manually set the span status.
13
+ *
14
+ * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564
15
+ * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status
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;
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;
@@ -1,5 +1,7 @@
1
1
  import type { ContextManager, TextMapPropagator } from '@opentelemetry/api';
2
2
  import type { Instrumentation } from '@opentelemetry/instrumentation';
3
+ import type { FetchCustomAttributeFunction } from '@opentelemetry/instrumentation-fetch';
4
+ import type { XHRCustomAttributeFunction } from '@opentelemetry/instrumentation-xml-http-request';
3
5
  import type { ResourceAttributes } from '@opentelemetry/resources';
4
6
  import type { SpanProcessor } from '@opentelemetry/sdk-trace-web';
5
7
  import type { Patterns } from '@grafana/faro-core';
@@ -14,8 +16,16 @@ export interface TracingInstrumentationOptions {
14
16
  contextManager?: ContextManager;
15
17
  instrumentations?: InstrumentationOption[];
16
18
  spanProcessor?: SpanProcessor;
17
- instrumentationOptions?: {
18
- propagateTraceHeaderCorsUrls: MatchUrlDefinitions;
19
- };
19
+ instrumentationOptions?: Omit<DefaultInstrumentationsOptions, 'ignoreUrls'>;
20
20
  }
21
21
  export type MatchUrlDefinitions = Patterns;
22
+ export type DefaultInstrumentationsOptions = {
23
+ ignoreUrls?: MatchUrlDefinitions;
24
+ propagateTraceHeaderCorsUrls?: MatchUrlDefinitions;
25
+ fetchInstrumentationOptions?: {
26
+ applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
27
+ };
28
+ xhrInstrumentationOptions?: {
29
+ applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;
30
+ };
31
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/faro-web-tracing",
3
- "version": "1.8.0",
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.0",
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": "ec21b36031c20c859d64fbd75a77986397ebaf68"
71
+ "gitHead": "ce860054636cc98bc3ad86420c3da19cccfc971a"
72
72
  }