autotel
Version:
Write Once, Observe Anywhere
204 lines (201 loc) • 7.02 kB
JavaScript
;
require('./chunk-JEQ2X3Z6.cjs');
var api = require('@opentelemetry/api');
var natsAdapter = {
producer: {
customAttributes: (_ctx, args) => {
const msg = args[0];
const attrs = {};
if (msg?.subject) attrs["nats.subject"] = msg.subject;
if (msg?.replyTo) attrs["nats.reply_to"] = msg.replyTo;
if (msg?.stream) attrs["nats.stream"] = msg.stream;
return attrs;
}
},
consumer: {
headersFrom: (msg) => {
const natsMsg = msg;
const headers = natsMsg.headers;
if (!headers) return;
if (typeof headers.toJSON === "function") {
const json = headers.toJSON();
if (json && typeof json === "object") {
return json;
}
}
if (typeof headers.get === "function") {
const result = {};
const traceHeaders = [
"traceparent",
"tracestate",
"baggage",
"x-b3-traceid",
"x-b3-spanid",
"x-b3-sampled",
"b3"
];
for (const key of traceHeaders) {
const value = headers.get(key);
if (value) {
result[key] = value;
}
}
if (Object.keys(result).length > 0) {
return result;
}
}
if (typeof headers.entries === "function") {
const result = {};
for (const [key, value] of headers.entries()) {
if (typeof key === "string" && typeof value === "string") {
result[key] = value;
}
}
if (Object.keys(result).length > 0) {
return result;
}
}
return;
},
customAttributes: (_ctx, msg) => {
const natsMsg = msg;
const attrs = {};
if (natsMsg.subject) attrs["nats.subject"] = natsMsg.subject;
if (natsMsg.reply) attrs["nats.reply_to"] = natsMsg.reply;
if (natsMsg.info?.stream) attrs["nats.stream"] = natsMsg.info.stream;
if (natsMsg.info?.consumer)
attrs["nats.consumer"] = natsMsg.info.consumer;
if (natsMsg.info?.redeliveryCount !== void 0) {
attrs["nats.delivered_count"] = natsMsg.info.redeliveryCount;
}
if (natsMsg.info?.pending !== void 0) {
attrs["nats.pending"] = natsMsg.info.pending;
}
return attrs;
}
}
};
var temporalAdapter = {
producer: {
customAttributes: (_ctx, args) => {
const info = args[0];
const attrs = {};
if (info?.workflowId) attrs["temporal.workflow_id"] = info.workflowId;
if (info?.runId) attrs["temporal.run_id"] = info.runId;
if (info?.taskQueue) attrs["temporal.task_queue"] = info.taskQueue;
if (info?.workflowType)
attrs["temporal.workflow_type"] = info.workflowType;
return attrs;
}
},
consumer: {
customAttributes: (_ctx, msg) => {
const info = msg;
const attrs = {};
if (info.workflowId) attrs["temporal.workflow_id"] = info.workflowId;
if (info.runId) attrs["temporal.run_id"] = info.runId;
if (info.activityId) attrs["temporal.activity_id"] = info.activityId;
if (info.taskQueue) attrs["temporal.task_queue"] = info.taskQueue;
if (info.attempt !== void 0) attrs["temporal.attempt"] = info.attempt;
if (info.activityType)
attrs["temporal.activity_type"] = info.activityType;
return attrs;
}
}
};
var cloudflareQueuesAdapter = {
consumer: {
customAttributes: (_ctx, msg) => {
const cfMsg = msg;
const attrs = {};
if (cfMsg.id) attrs["cloudflare.queue.message_id"] = cfMsg.id;
if (cfMsg.timestamp) {
attrs["cloudflare.queue.timestamp_ms"] = cfMsg.timestamp.getTime();
}
if (cfMsg.attempts !== void 0) {
attrs["cloudflare.queue.attempts"] = cfMsg.attempts;
}
return attrs;
}
}
};
function datadogContextExtractor(headers) {
const traceIdDecimal = headers["x-datadog-trace-id"];
const spanIdDecimal = headers["x-datadog-parent-id"];
const samplingPriority = headers["x-datadog-sampling-priority"];
if (!traceIdDecimal || !spanIdDecimal) return null;
let otelTraceId;
let otelSpanId;
try {
otelTraceId = BigInt(traceIdDecimal).toString(16).padStart(32, "0");
otelSpanId = BigInt(spanIdDecimal).toString(16).padStart(16, "0");
} catch {
return null;
}
const sampled = samplingPriority ? Number.parseInt(samplingPriority, 10) > 0 : true;
return {
traceId: otelTraceId,
spanId: otelSpanId,
traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
isRemote: true
};
}
function b3ContextExtractor(headers) {
const b3Single = headers["b3"] || headers["B3"];
if (b3Single) {
if (b3Single === "0") return null;
const parts = b3Single.split("-");
const traceId2 = parts[0];
const spanId2 = parts[1];
const sampledFlag = parts[2];
if (traceId2 && spanId2) {
const sampled2 = sampledFlag !== "0" && sampledFlag !== "d";
return {
traceId: traceId2.padStart(32, "0"),
spanId: spanId2.padStart(16, "0"),
traceFlags: sampled2 ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
isRemote: true
};
}
}
const traceId = headers["x-b3-traceid"] || headers["X-B3-TraceId"] || headers["X-B3-Traceid"];
const spanId = headers["x-b3-spanid"] || headers["X-B3-SpanId"] || headers["X-B3-Spanid"];
const sampledHeader = headers["x-b3-sampled"] || headers["X-B3-Sampled"] || headers["x-b3-flags"] || headers["X-B3-Flags"];
if (!traceId || !spanId) return null;
const sampled = sampledHeader === "1" || sampledHeader === "true" || sampledHeader === void 0;
return {
traceId: traceId.padStart(32, "0"),
spanId: spanId.padStart(16, "0"),
traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
isRemote: true
};
}
function xrayContextExtractor(headers) {
const xrayHeader = headers["x-amzn-trace-id"] || headers["X-Amzn-Trace-Id"];
if (!xrayHeader) return null;
const rootMatch = xrayHeader.match(/Root=1-([a-f0-9]{8})-([a-f0-9]{24})/i);
const parentMatch = xrayHeader.match(/Parent=([a-f0-9]{16})/i);
const sampledMatch = xrayHeader.match(/Sampled=([01])/);
if (!rootMatch || !parentMatch) return null;
const timestamp = rootMatch[1];
const random = rootMatch[2];
const parentId = parentMatch[1];
if (!timestamp || !random || !parentId) return null;
const traceId = `${timestamp}${random}`;
const spanId = parentId;
const sampled = sampledMatch ? sampledMatch[1] === "1" : true;
return {
traceId,
spanId,
traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
isRemote: true
};
}
exports.b3ContextExtractor = b3ContextExtractor;
exports.cloudflareQueuesAdapter = cloudflareQueuesAdapter;
exports.datadogContextExtractor = datadogContextExtractor;
exports.natsAdapter = natsAdapter;
exports.temporalAdapter = temporalAdapter;
exports.xrayContextExtractor = xrayContextExtractor;
//# sourceMappingURL=messaging-adapters.cjs.map
//# sourceMappingURL=messaging-adapters.cjs.map