UNPKG

@grafana/faro-web-tracing

Version:
106 lines 5.85 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.TracingInstrumentation = void 0; var api_1 = require("@opentelemetry/api"); var core_1 = require("@opentelemetry/core"); var instrumentation_1 = require("@opentelemetry/instrumentation"); var resources_1 = require("@opentelemetry/resources"); var sdk_trace_web_1 = require("@opentelemetry/sdk-trace-web"); var semantic_conventions_1 = require("@opentelemetry/semantic-conventions"); var incubating_1 = require("@opentelemetry/semantic-conventions/incubating"); var faro_web_sdk_1 = require("@grafana/faro-web-sdk"); var faroMetaAttributesSpanProcessor_1 = require("./faroMetaAttributesSpanProcessor"); var faroTraceExporter_1 = require("./faroTraceExporter"); var faroUserActionSpanProcessor_1 = require("./faroUserActionSpanProcessor"); var getDefaultOTELInstrumentations_1 = require("./getDefaultOTELInstrumentations"); var sampler_1 = require("./sampler"); // the providing of app name here is not great // should delay initialization and provide the full Faro config, // taking app name from it var TracingInstrumentation = /** @class */ (function (_super) { __extends(TracingInstrumentation, _super); function TracingInstrumentation(options) { if (options === void 0) { options = {}; } var _this = _super.call(this) || this; _this.options = options; _this.name = '@grafana/faro-web-tracing'; _this.version = faro_web_sdk_1.VERSION; return _this; } TracingInstrumentation.prototype.initialize = function () { var _this = this; var _a, _b, _c, _d; var options = this.options; var attributes = {}; if (this.config.app.name) { attributes[semantic_conventions_1.ATTR_SERVICE_NAME] = this.config.app.name; } if (this.config.app.namespace) { attributes[incubating_1.ATTR_SERVICE_NAMESPACE] = this.config.app.namespace; } if (this.config.app.version) { attributes[semantic_conventions_1.ATTR_SERVICE_VERSION] = this.config.app.version; } if (this.config.app.environment) { attributes[incubating_1.ATTR_DEPLOYMENT_ENVIRONMENT_NAME] = this.config.app.environment; /** * @deprecated will be removed in the future and has been replaced by ATTR_DEPLOYMENT_ENVIRONMENT_NAME (deployment.environment.name) */ attributes[semantic_conventions_1.SEMRESATTRS_DEPLOYMENT_ENVIRONMENT] = this.config.app.environment; } Object.assign(attributes, options.resourceAttributes); var resource = resources_1.Resource.default().merge(new resources_1.Resource(attributes)); var provider = new sdk_trace_web_1.WebTracerProvider({ resource: resource, sampler: { shouldSample: function () { return { decision: (0, sampler_1.getSamplingDecision)(_this.api.getSession()), }; }, }, spanProcessors: [ (_a = options.spanProcessor) !== null && _a !== void 0 ? _a : new faroUserActionSpanProcessor_1.FaroUserActionSpanProcessor(new faroMetaAttributesSpanProcessor_1.FaroMetaAttributesSpanProcessor(new sdk_trace_web_1.BatchSpanProcessor(new faroTraceExporter_1.FaroTraceExporter({ api: this.api }), { scheduledDelayMillis: TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS, maxExportBatchSize: 30, }), this.metas)), ], }); provider.register({ propagator: (_b = options.propagator) !== null && _b !== void 0 ? _b : new core_1.W3CTraceContextPropagator(), contextManager: options.contextManager, }); var _e = (_c = this.options.instrumentationOptions) !== null && _c !== void 0 ? _c : {}, propagateTraceHeaderCorsUrls = _e.propagateTraceHeaderCorsUrls, fetchInstrumentationOptions = _e.fetchInstrumentationOptions, xhrInstrumentationOptions = _e.xhrInstrumentationOptions; (0, instrumentation_1.registerInstrumentations)({ instrumentations: (_d = options.instrumentations) !== null && _d !== void 0 ? _d : (0, getDefaultOTELInstrumentations_1.getDefaultOTELInstrumentations)({ ignoreUrls: this.getIgnoreUrls(), propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls, fetchInstrumentationOptions: fetchInstrumentationOptions, xhrInstrumentationOptions: xhrInstrumentationOptions, }), }); this.api.initOTEL(api_1.trace, api_1.context); }; TracingInstrumentation.prototype.getIgnoreUrls = function () { return this.transports.transports.flatMap(function (transport) { return transport.getIgnoreUrls(); }); }; TracingInstrumentation.SCHEDULED_BATCH_DELAY_MS = 1000; return TracingInstrumentation; }(faro_web_sdk_1.BaseInstrumentation)); exports.TracingInstrumentation = TracingInstrumentation; //# sourceMappingURL=instrumentation.js.map