UNPKG

@codebayu/nuxt-plugins-signoz

Version:

Reusable plugin to implement signoz on Nuxt project

113 lines (112 loc) 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSpan = exports.initTracer = void 0; const api_1 = require("@opentelemetry/api"); const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http"); const sdk_trace_web_1 = require("@opentelemetry/sdk-trace-web"); const resources_1 = require("@opentelemetry/resources"); const semantic_conventions_1 = require("@opentelemetry/semantic-conventions"); const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base"); const context_zone_1 = require("@opentelemetry/context-zone"); const propagator_b3_1 = require("@opentelemetry/propagator-b3"); const instrumentation_1 = require("@opentelemetry/instrumentation"); const auto_instrumentations_web_1 = require("@opentelemetry/auto-instrumentations-web"); const initTracer = ({ serviceName, exporterUrl, attributes }) => { const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: exporterUrl }); const resource = new resources_1.Resource({ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName }); const provider = new sdk_trace_web_1.WebTracerProvider({ resource }); provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter)); provider.register({ contextManager: new context_zone_1.ZoneContextManager(), propagator: new propagator_b3_1.B3Propagator() }); const excludeHost = ['www.google-analytics.com', 'api-js.mixpanel.com', 'sdk-01.moengage.com', 'stats.g.doubleclick.net']; const customAttributes = (span) => { span.setAttribute('http.path', attributes.path); span.setAttribute('user.uuid', attributes.uuid); }; const customAttributesToResourceFetchSpan = (span, resource) => { span.setAttribute('resource.tcp.duration_ms', resource.connectEnd - resource.connectStart); }; (0, instrumentation_1.registerInstrumentations)({ instrumentations: [ (0, auto_instrumentations_web_1.getWebAutoInstrumentations)({ '@opentelemetry/instrumentation-document-load': { applyCustomAttributesOnSpan: { documentLoad: customAttributes, resourceFetch: customAttributesToResourceFetchSpan } }, '@opentelemetry/instrumentation-xml-http-request': { propagateTraceHeaderCorsUrls: [/.+/g], clearTimingResources: true, applyCustomAttributesOnSpan: (span, xhr) => { const attributes = span.attributes; const hostRequest = attributes['http.url']; const urlHost = new URL(hostRequest).hostname; const isExclude = excludeHost.some(host => urlHost.includes(host)); const spanName = `${attributes['http.method']} ${isExclude ? urlHost : xhr.__zone_symbol__xhrURL || attributes['http.url']}`; span.setAttribute('response', xhr.response); span.setAttribute('responseText', xhr.responseText); span.updateName(spanName); customAttributes(span); } }, '@opentelemetry/instrumentation-fetch': { propagateTraceHeaderCorsUrls: [/.+/g], clearTimingResources: true, applyCustomAttributesOnSpan: (span, request, result) => { const attributes = span.attributes; const hostRequest = attributes['http.host']; if (excludeHost.includes(hostRequest)) { return; } if (attributes.component === 'fetch') { span.updateName(`${attributes['http.method']} ${attributes['http.url']}`); } if (result instanceof Error) { span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: result.message }); span.recordException(result.stack || result.name); } customAttributes(span); } } }) ] }); return provider.getTracer(serviceName); }; exports.initTracer = initTracer; const createSpan = ({ tracer, name, func = () => { }, attributes, events }) => { const span = tracer.startSpan(name); return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), () => { // mapping custom attributes if (span.isRecording() && attributes) { for (const key in attributes) { if (attributes.hasOwnProperty(key)) { const value = attributes[key]; span.setAttribute(key, value); } } } // mapping custom attributes if (events) { span.addEvent(events.name, events.keyValue); } // execute function to trace try { const result = func(); span.end(); return result; } catch (error) { span.setStatus({ code: api_1.SpanStatusCode.ERROR }); span.end(); throw error; } }); }; exports.createSpan = createSpan;