UNPKG

bullmq-otel

Version:
174 lines 5.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BullMQOtel = exports.BullMQOTelSpan = void 0; const api_1 = require("@opentelemetry/api"); class BullMQOTelContextManager { constructor() { this.contextAPI = api_1.context; } getMetadata(ctx) { const metadata = {}; api_1.propagation.inject(ctx, metadata); return JSON.stringify(metadata); } fromMetadata(activeCtx, metadata) { const metadataObj = JSON.parse(metadata); const ctx = api_1.propagation.extract(activeCtx, metadataObj); return ctx; } with(ctx, fn) { return this.contextAPI.with(ctx, fn); } active() { return this.contextAPI.active(); } } class BullMQOtelTracer { constructor(tracer) { this.tracer = tracer; } startSpan(name, options, context) { const span = this.tracer.startSpan(name, options, context); return new BullMQOTelSpan(span); } } class BullMQOTelSpan { constructor(span) { this.span = span; } setSpanOnContext(ctx) { return api_1.trace.setSpan(ctx, this.span); } setAttribute(key, value) { this.span.setAttribute(key, value); } setAttributes(attributes) { this.span.setAttributes(attributes); } addEvent(name, attributes, time) { this.span.addEvent(name, attributes, time); } recordException(exception, time) { this.span.recordException(exception, time); } end() { this.span.end(); } } exports.BullMQOTelSpan = BullMQOTelSpan; class BullMQOTelCounter { constructor(counter) { this.counter = counter; } add(value, attributes) { this.counter.add(value, attributes); } } class BullMQOTelHistogram { constructor(histogram) { this.histogram = histogram; } record(value, attributes) { this.histogram.record(value, attributes); } } class BullMQOTelGauge { constructor(gauge) { this.gauge = gauge; } record(value, attributes) { this.gauge.record(value, attributes); } } class BullMQOTelMeter { constructor(meter) { this.meter = meter; this.counters = new Map(); this.histograms = new Map(); this.gauges = new Map(); } createCounter(name, options) { // Cache counters to avoid creating duplicates let counter = this.counters.get(name); if (!counter) { const otelCounter = this.meter.createCounter(name, options); counter = new BullMQOTelCounter(otelCounter); this.counters.set(name, counter); } return counter; } createHistogram(name, options) { // Cache histograms to avoid creating duplicates let histogram = this.histograms.get(name); if (!histogram) { const otelHistogram = this.meter.createHistogram(name, options); histogram = new BullMQOTelHistogram(otelHistogram); this.histograms.set(name, histogram); } return histogram; } createGauge(name, options) { // Cache gauges to avoid creating duplicates let gauge = this.gauges.get(name); if (!gauge) { const otelGauge = this.meter.createGauge(name, options); gauge = new BullMQOTelGauge(otelGauge); this.gauges.set(name, gauge); } return gauge; } } class BullMQOtel { /** * Creates a new BullMQOtel telemetry instance. * * @param tracerNameOrOptions - Either a tracer name string (for backward compatibility) * or a configuration options object * @param version - Version string (only used when first parameter is a string) * * @example * // Simple usage (backward compatible) * const telemetry = new BullMQOtel('my-app', '1.0.0'); * * @example * // With metrics enabled * const telemetry = new BullMQOtel({ * tracerName: 'my-app', * meterName: 'my-app', * version: '1.0.0', * enableMetrics: true, * }); */ constructor(tracerNameOrOptions, version) { var _a; let options; if (typeof tracerNameOrOptions === 'string') { // Backward compatible: (tracerName, version?) options = { tracerName: tracerNameOrOptions, version, enableMetrics: false, }; } else if (tracerNameOrOptions === undefined) { // No arguments provided options = { tracerName: 'bullmq', version, enableMetrics: false, }; } else { // New options object style options = Object.assign({ tracerName: 'bullmq', meterName: 'bullmq', enableMetrics: false, version }, tracerNameOrOptions); } this.tracer = new BullMQOtelTracer(api_1.trace.getTracer(options.tracerName, options.version)); this.contextManager = new BullMQOTelContextManager(); if (options.enableMetrics) { const otelMeter = api_1.metrics.getMeter((_a = options.meterName) !== null && _a !== void 0 ? _a : options.tracerName, options.version); this.meter = new BullMQOTelMeter(otelMeter); } } } exports.BullMQOtel = BullMQOtel; //# sourceMappingURL=bullMQOtel.js.map