UNPKG

nestjs-otel

Version:
74 lines (73 loc) 2.72 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WideEventService = void 0; const common_1 = require("@nestjs/common"); const wide_event_context_1 = require("./wide-event.context"); /** * Accumulates attributes for the wide event of the current request. * * All methods are no-ops when called outside a request handled by the * WideEventInterceptor. * * @publicApi */ let WideEventService = class WideEventService { /** * Set a single attribute on the current wide event. */ set(key, value) { (0, wide_event_context_1.getWideEventBag)()?.set(key, value); } /** * Set multiple attributes on the current wide event. */ setMany(attributes) { const bag = (0, wide_event_context_1.getWideEventBag)(); if (!bag) { return; } for (const [key, value] of Object.entries(attributes)) { if (value !== undefined) { bag.set(key, value); } } } /** * Increment a numeric attribute on the current wide event. * Starts from 0 when the attribute is absent or not a number. */ increment(key, by = 1) { const bag = (0, wide_event_context_1.getWideEventBag)(); if (!bag) { return; } const current = bag.get(key); bag.set(key, (typeof current === "number" ? current : 0) + by); } /** * Start a timer for the given attribute. The returned function stops * the timer and records the elapsed time in milliseconds. */ startTimer(key) { const bag = (0, wide_event_context_1.getWideEventBag)(); const start = performance.now(); return () => { if (!bag) { return; } const elapsed = performance.now() - start; const current = bag.get(key); bag.set(key, (typeof current === "number" ? current : 0) + elapsed); }; } }; exports.WideEventService = WideEventService; exports.WideEventService = WideEventService = __decorate([ (0, common_1.Injectable)() ], WideEventService);