What is distributed link tracking

In short, a technique developed for tracking links in distributed systems.

The application scenarios are as follows:

Application A,B,C,D, and E depend on each other in A hierarchical relationship. When A user sends A request to application A, but an exception is returned, we may need A server to troubleshoot this problem. In distributed architecture, each application deploys dozens or hundreds of servers, and at the end of the day, you may not find the trigger point of the problem.

Through distributed link tracking, combined with link analysis of platforms such as Hawk-Eye, we can quickly locate the machine address where the problem occurred.

So why can we pinpoint which machine so quickly?

  • TraceId Log collection. Collect exception logs to quickly find the cause of exceptions
  • TraceId Inserts machine IP addresses to quickly locate abnormal machines

The basic concept

A trace link is composed of multiple associated spans. As a whole, a link can be regarded as a directed acyclic graph, and the edge relationship between all spans is called References.

  • TraceId: Each link is uniquetraceId
  • SpanId: One for each nodespan, there is a hierarchical relationship

How do I implement a Tracer myself

SOFATracer link transparent transmission principle is referenced:

  • Cross-process pass-through, that is, how to transfer link data from one process to the downstream process
  • Passthrough in threads
    • How is tracer context information currently recovered after the current request cross-process invocation ends
    • How to implement transthreading, such as an asynchronous thread in the current thread scenario

TracerId & SpanId generate rules

TraceId generation rules:

Server IP + ID when time + since the increasing sequence + 0 ad1348f1403169275002100356696 number the current processCopy the code

SpanId generation rules:

The root node is 0, followed by a., which is continuously layered

Transparent transmission of TracerId across processes

Using SOFATracer as an example, describe how an Http request can be transmitted across processes.

SofaTracer source code address:

https://github.com/sofastack-guides/sofa-tracer-guides/tree/master/tracer-sample-with-springmvc

In an Http request, when the request passes through the Filter, SOFATracer mainly determines whether the header of the current request contains traceId and spanId

From the dependency package, we can see that there is only one filter

Header Contains Tracer information

Store context information in ThreadLocal

Header Does not contain Tracer information

Create a new context, generate traceId and spanId, and store it in ThreadLocal

TracerId is transparently transmitted across threads

With deep copy, create a new thread message and pass SofaTracerSpanContext to the child thread

public SofaTracerSpanContext cloneInstance(a) {
  // Rebuild an instance of SofaTracerSpanContext
  // tracerId,spanId,parentId and sampling information from the current parent thread are used as build build parameters
  SofaTracerSpanContext spanContext = new SofaTracerSpanContext(this.traceId, this.spanId,
  this.parentId, this.isSampled);
  // The system transparently transmits data
  spanContext.addSysBaggage(this.sysBaggage);
  // Transparent transmission of data
  spanContext.addBizBaggage(this.bizBaggage);
  spanContext.childContextIndex = this.childContextIndex;
  return spanContext;
}
Copy the code

Hand jerk off a DEMO

MyFilter

conclusion

The principle looks very simple, but the actual use, but also consider many aspects of the problem, if the log collection, log analysis, traceId generation rules and so on, interested in everyone can go to see the source: portal

Thank you for reading, I hope to help you, MY name is Jiuling, if you need to communicate with children, you can add me WX, JayCE-K, recently committed to helping more friends to join the factory, welcome to chat ~

Link: SOFA – tracer official documentation: www.sofastack.tech/projects/so…