opentelemetry-propagation-utils
Version:
open telemetry propagation utils
112 lines • 4.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@opentelemetry/api");
const START_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.start_span');
const END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');
const patchArrayFilter = (messages, tracer, loopContext) => {
const origFunc = messages.filter;
const patchedFunc = function (..._args) {
const newArray = origFunc.apply(this, arguments);
patchArrayForProcessSpans(newArray, tracer, loopContext);
return newArray;
};
Object.defineProperty(messages, 'filter', {
enumerable: false,
value: patchedFunc,
});
};
const patchArrayFunction = (messages, functionName, tracer, loopContext) => {
const origFunc = messages[functionName];
const patchedFunc = function (callback, thisArg) {
const wrappedCallback = function (message) {
var _a;
const messageSpan = (_a = message === null || message === void 0 ? void 0 : message[START_SPAN_FUNCTION]) === null || _a === void 0 ? void 0 : _a.call(message);
if (!messageSpan)
return callback.apply(this, arguments);
const res = api_1.context.with(api_1.trace.setSpan(loopContext, messageSpan), () => {
var _a;
try {
return callback.apply(this, arguments);
}
catch (err) {
throw err;
}
finally {
(_a = message[END_SPAN_FUNCTION]) === null || _a === void 0 ? void 0 : _a.call(message);
}
});
if (typeof res === 'object') {
Object.defineProperty(res, START_SPAN_FUNCTION, Object.getOwnPropertyDescriptor(message, START_SPAN_FUNCTION));
Object.defineProperty(res, END_SPAN_FUNCTION, Object.getOwnPropertyDescriptor(message, END_SPAN_FUNCTION));
}
return res;
};
const funcResult = origFunc.call(this, wrappedCallback, thisArg);
if (Array.isArray(funcResult))
patchArrayForProcessSpans(funcResult, tracer, loopContext);
return funcResult;
};
Object.defineProperty(messages, functionName, {
enumerable: false,
value: patchedFunc,
});
};
const patchArrayForProcessSpans = (messages, tracer, loopContext = api_1.context.active()) => {
patchArrayFunction(messages, 'forEach', tracer, loopContext);
patchArrayFunction(messages, 'map', tracer, loopContext);
patchArrayFilter(messages, tracer, loopContext);
};
const startMessagingProcessSpan = (message, name, attributes, parentContext, propagatedContext, tracer, processHook) => {
const links = [];
const spanContext = api_1.trace.getSpanContext(propagatedContext);
if (spanContext) {
links.push({
context: spanContext,
});
}
const spanName = `${name} process`;
const processSpan = tracer.startSpan(spanName, {
kind: api_1.SpanKind.CONSUMER,
attributes: Object.assign(Object.assign({}, attributes), { ['messaging.operation']: 'process' }),
links,
}, parentContext);
Object.defineProperty(message, START_SPAN_FUNCTION, {
enumerable: false,
writable: true,
value: () => processSpan,
});
Object.defineProperty(message, END_SPAN_FUNCTION, {
enumerable: false,
writable: true,
value: () => {
processSpan.end();
Object.defineProperty(message, END_SPAN_FUNCTION, {
enumerable: false,
writable: true,
value: () => { },
});
},
});
if (processHook) {
try {
processHook(processSpan, message);
}
catch (_a) { }
}
return processSpan;
};
const patchMessagesArrayToStartProcessSpans = ({ messages, tracer, parentContext, messageToSpanDetails, processHook, }) => {
messages.forEach((message) => {
const { attributes, name, parentContext: propagatedContext } = messageToSpanDetails(message);
Object.defineProperty(message, START_SPAN_FUNCTION, {
enumerable: false,
writable: true,
value: () => startMessagingProcessSpan(message, name, attributes, parentContext, propagatedContext, tracer, processHook),
});
});
};
exports.default = {
patchMessagesArrayToStartProcessSpans,
patchArrayForProcessSpans,
};
//# sourceMappingURL=pubsub-propagation.js.map
;