UNPKG

@vtex/diagnostics-nodejs

Version:

Diagnostics library for Node.js applications

87 lines 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addBaggageToContext = addBaggageToContext; exports.addBaggageItemsToContext = addBaggageItemsToContext; exports.getBaggageFromContext = getBaggageFromContext; exports.getAllBaggageFromContext = getAllBaggageFromContext; exports.setBaggage = setBaggage; exports.addBaggage = addBaggage; exports.getBaggage = getBaggage; exports.getActiveBaggage = getActiveBaggage; exports.applyBaggageToActiveContext = applyBaggageToActiveContext; exports.applyBaggageItemsToActiveContext = applyBaggageItemsToActiveContext; const api_1 = require("@opentelemetry/api"); function addBaggageToContext(ctx, key, value) { const baggage = api_1.propagation.getBaggage(ctx) || api_1.propagation.createBaggage(); const updatedBaggage = baggage.setEntry(key, { value }); return api_1.propagation.setBaggage(ctx, updatedBaggage); } function addBaggageItemsToContext(ctx, items) { let baggage = api_1.propagation.getBaggage(ctx) || api_1.propagation.createBaggage(); for (const [key, value] of Object.entries(items)) { baggage = baggage.setEntry(key, { value }); } return api_1.propagation.setBaggage(ctx, baggage); } function getBaggageFromContext(ctx, key) { const baggage = api_1.propagation.getBaggage(ctx); if (!baggage) return undefined; const entry = baggage.getEntry(key); return entry?.value; } function getAllBaggageFromContext(ctx) { const baggage = api_1.propagation.getBaggage(ctx); if (!baggage) return {}; const result = {}; baggage.getAllEntries().forEach(([key, entry]) => { result[key] = entry.value; }); return result; } function setBaggage(key, value) { const activeSpan = api_1.trace.getActiveSpan(); if (activeSpan) { activeSpan.setAttribute(key, value); } const currentContext = api_1.context.active(); const baggage = api_1.propagation.getBaggage(currentContext) || api_1.propagation.createBaggage(); const updatedBaggage = baggage.setEntry(key, { value }); const updatedContext = api_1.propagation.setBaggage(currentContext, updatedBaggage); const contextManager = api_1.context._getContextManager(); if (contextManager && contextManager._asyncLocalStorage) { contextManager._asyncLocalStorage.enterWith(updatedContext); } } function addBaggage(items) { const activeSpan = api_1.trace.getActiveSpan(); if (activeSpan) { for (const [key, value] of Object.entries(items)) { activeSpan.setAttribute(key, value); } } const currentContext = api_1.context.active(); let baggage = api_1.propagation.getBaggage(currentContext) || api_1.propagation.createBaggage(); for (const [key, value] of Object.entries(items)) { baggage = baggage.setEntry(key, { value }); } const updatedContext = api_1.propagation.setBaggage(currentContext, baggage); const contextManager = api_1.context._getContextManager(); if (contextManager && contextManager._asyncLocalStorage) { contextManager._asyncLocalStorage.enterWith(updatedContext); } } function getBaggage(key) { return getBaggageFromContext(api_1.context.active(), key); } function getActiveBaggage() { return getAllBaggageFromContext(api_1.context.active()); } function applyBaggageToActiveContext(key, value) { setBaggage(key, value); } function applyBaggageItemsToActiveContext(items) { addBaggage(items); } //# sourceMappingURL=baggage.js.map