UNPKG

@imqueue/opentelemetry-instrumentation-imqueue

Version:
170 lines 6.27 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.traceStart = traceStart; exports.traceEnd = traceEnd; exports.traced = traced; /*! * I'm Queue Software Project * Copyright (C) 2025 imqueue.com <support@imqueue.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * * If you want to use this code in a closed source (commercial) project, you can * purchase a proprietary commercial license. Please contact us at * <support@imqueue.com> to get commercial licensing options. */ const api_1 = require("@opentelemetry/api"); const path = __importStar(require("path")); const src_1 = require("./src"); __exportStar(require("./src/instrumentation"), exports); const traces = {}; const componentName = 'imq'; const defaultTracerName = 'basic'; // noinspection JSUnusedGlobalSymbols /** * Shorthand for making in-code traces. Starts datadog trace span with the * given name, and assigns it given tags (if passed). * * @example * ```typescript * import { * trace, * traceEnd, * } from '@imqueue/opentelemetry-instrumentation-imqueue'; * * trace('my-trace'); * // ... do some work * traceEnd('my-trace'); * ``` * * @param {string} name - trace name (datadog span name * @param {TraceAttributes} [tags] - datadog trace span tags, if passed * @param {string} tracerName */ function traceStart(name, tags, tracerName) { if (traces[name]) { throw new TypeError(`Trace with name ${name} has been already started!`); } traces[name] = api_1.trace.getTracer(tracerName || defaultTracerName).startSpan(name); } // noinspection JSUnusedGlobalSymbols /** * Shorthand for finishing datadog trace span. * * @param {string} name */ function traceEnd(name) { if (traces[name]) { traces[name].end(); delete traces[name]; } } const DEFAULT_TRACED_OPTIONS = { kind: src_1.TraceKind.SERVER, }; let pkgName = ''; try { pkgName = require(`${path.resolve('.')}${path.sep}package.json`).name; } catch (err) { /* ignore */ } // noinspection JSUnusedGlobalSymbols /** * Decorator factory, which return decorator function allowing to add tracing to * decorated method calls. */ function traced(options) { return (target, methodName, descriptor) => { const original = descriptor.value; const opts = Object.assign({}, DEFAULT_TRACED_OPTIONS, options || {}); const tracerInstance = api_1.trace.getTracer(opts.tracerName || defaultTracerName); descriptor.value = function (...args) { const className = this.constructor.name; const attributes = Object.assign({ [src_1.AttributeNames.SPAN_KIND]: opts.kind, [src_1.AttributeNames.RESOURCE_NAME]: `${className}.${String(methodName)}`, ...(pkgName ? { [src_1.AttributeNames.RESOURCE_NAME]: pkgName } : {}), [src_1.AttributeNames.COMPONENT]: componentName, }, opts.tags || {}); const span = tracerInstance.startSpan(src_1.SpanNames.METHOD_CALL, { attributes, kind: opts.kind === src_1.TraceKind.CLIENT ? api_1.SpanKind.CLIENT : api_1.SpanKind.SERVER, }); try { const result = original && original.apply(this, args); if (result && result.then) { // noinspection CommaExpressionJS return result.then((res) => (span.end(), res)) .catch((err) => handleError(span, err)); } span.end(); return result; } catch (err) { handleError(span, err); } }; }; } /** * Handles error gracefully, finishing tracing span before throwing * * @param {Span} span * @param {any} err * @throws {any} */ function handleError(span, err) { span.setAttribute(src_1.AttributeNames.ERROR_MESSAGE, err); span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: err?.message }); span.end(); throw err; } //# sourceMappingURL=index.js.map