Skip to content

Instantly share code, notes, and snippets.

@azl397985856
Last active July 16, 2020 07:38
Show Gist options
  • Save azl397985856/2f5bf2c8fb6640d37eec1319faf3b5ec to your computer and use it in GitHub Desktop.
Save azl397985856/2f5bf2c8fb6640d37eec1319faf3b5ec to your computer and use it in GitHub Desktop.
traceId-spanId 全链路追踪
// 根据 traceId 可以找出一个链路的所有请求
// 根据 spanId 可以就上面找到的所有请求画出调用树🌲
function a({ traceId, spanId, random }) {
if (random > 0.5) {
b({ traceId, spanId: spanId + "." + 1, random: Math.random() });
} else {
c({ traceId, spanId: spanId + "." + 1, random: Math.random() });
}
}
function b({ traceId, spanId, random }) {
if (random > 0.5) {
d({ traceId, spanId: spanId + "." + 1, random: Math.random() });
} else {
c({ traceId, spanId: spanId + "." + 1, random: Math.random() });
c({ traceId, spanId: spanId + "." + 2, random: Math.random() });
c({ traceId, spanId: spanId + "." + 3, random: Math.random() });
}
}
function c({ traceId, spanId }) {
if (Math.random() > 0.5) return a({ traceId, spanId, random: Math.random() });
console.log("c:", { traceId, spanId });
}
function d({ traceId, spanId }) {
console.log("d", { traceId, spanId });
}
function getIp() {
return "192.168.2.1";
}
function getPid() {
return "11309";
}
function getTraceId() {
return getIp() + new Date().getTime() + i++ + getPid();
}
let i = 100000;
function test() {
const traceId = getTraceId();
a({
traceId,
spanId: "0",
random: Math.random(),
});
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment