TraceId and spanId generation rule

Update time: 2019-07-25

TraceId generation rule

SOFATracer uses TraceId to concatenate the call logs of a request on each server. The TraceId is typically generated by the first server that receives the request. The generation rule is: server IP + generated time + incremental sequence + current process ID, such as:

  • The first 8 digits 0ad1348f is the IP of the machine that generates TraceId. This is a hexadecimal number, in which every two digits represents a part of IP. Based on the number, we can get a common IP address like by converting every two digits into a decimal number. According to this rule, you can also figure out the first server that the request goes through.
  • The next 13 digits 1403169275002 is the time to generate the TraceId.
  • The next 4 digits 1003 is an auto-incrementing sequence that increases from 1000 to 9000. After reaching 9000, it returns to 1000 and then restarts to increase.
  • The last 5 digits 56696 is the current process ID. Its role in tracerId is to prevent the TraceId conflicts caused by multiple processes in a single machine.

Currently, TraceId’s generated rules refer to Taobao’s Hawkeye components.

SpanId generation rule

The SpanId in SOFATracer represents where the current call is in the entire calling link. If a Web system A receives a user request, then in the SOFATracer MVC log of this system, the recorded SpanId is 0, which means the root node of the entire call. If the system A processes this request and needs to call system B, C, and D through RPC, then the SpanIds in the SOFATracer RPC client log of system A are 0.1, 0.2, and 0.3 respectively. And in the SOFATracer RPC server logs of the system B, C, and D, the SpanIds are also 0.1, 0.2 and 0.3 respectively. If system C calls system E and F when processing the request, then in the corresponding SOFATracer RPC client log of system C, the SpanIds are 0.2.1 and 0.2.2. And the SpanIds in the SOFATracer RPC server logs of system E and F are also 0.2.1 and 0.2.2. As we can known from above, if all SpanIds in a call can be collected to compose a complete link tree.

We assume that the TraceId generated in a distributed call is 0a1234 (much longer in practice). Then, according to the generation process of SpanId, the call link tree is as shown in the following figure:


Currently, SpanId’s generated rules refer to Taobao’s Hawkeye components.