Foreword
In the process of developing skywalking nginx probe focuses on the "Skywalking cross-link the process of information transfer protocol" and "Skywalking link data acquisition protocol." Achieve skywalking nginx probe has been completed and a version of the line in the production environment, interested friends can exchange. Based Skywalking v5.0.0-GA analysis, although some versions behind, but the core of the two protocols has not changed much. The official English title and document two agreements refer to the following links:
For ease of understanding, this article will Skywalking-Cross-Process-Propagation-Headers-Protocol-v1
be interpreted as 跨进程链路信息传递协议
, Trace-Data-Protocol
interpreted as Skywalking链路数据采集协议
. The official called on the two agreements have Chinese translation, self-inspection. "Cross-link messaging protocol process" itself is not difficult to understand, but in the realization of many factors to consider, this series will combine Java source code in-depth analysis of the probe. This article focuses on explaining 链路ID
the generation process.
Link cross-process information transfer protocol role
In the distributed system, the user initiates a request for data, a plurality of back-end system will process the data service node service request. One of the important role of link tracking system is to be able to order individual services in data processing nodes, logical relationships, long-time manager, topology and other information objectively real reaction out. A process system generally acts as a service node, 跨进程
describing the resulting. A complete distributed systems, various service nodes often have different components, different languages, different technology stack components. Services such as java, node service, nginx components and so on. To resolve the link data of different nodes generate, transfer, identification, resolution, coding and other issues, the agreement would have a set of specifications for different languages can process link data. So 跨进程链路信息传递协议
I was born.
Skywalking Java Agent source structure Introduction
Before talking about trace segment id generation logic section introduces Skywalking source structure of Java Agent. Skywalking v5.0.0-GA in, Java Agent Skywalking link information of the package is located abstraction source apm-sniffer/apm-agent-core
module. Link data analyzed as follows the processing logic of the module do herein.
-
Link data abstraction model function is encapsulated package agreement data link data abstraction, while generally compatible Opentracing specification. Key class has the following
org.apache.skywalking.apm.agent.core.context.trace.TraceSegment
org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef
org.apache.skywalking.apm.agent.core.context.trace.EntrySpan
org.apache.skywalking.apm.agent.core.context.trace.ExitSpan
org.apache.skywalking.apm.agent.core.context.ids.DistributedTraceIds
-
Cross-link data abstraction and encapsulation process focused class is
org.apache.skywalking.apm.agent.core.context.ContextCarrier
. I.e., encapsulation of a protocol specification is explained herein. -
Link data acquisition package key abstraction model class is
org.apache.skywalking.apm.agent.core.context.TracingContext
. TracingContext stored in a ThreadLocal, it contains all of the data link, and the control method of the data, mainly for the control Span. ContextCarrier while providing processing data, comprising:- Convert TraceSegment to ContextCarrier, namely org.apache.skywalking.apm.agent.core.context.TracingContext # inject
- Extracting data from ContextCarrier TraceSegment data, i.e. org.apache.skywalking.apm.agent.core.context.TracingContext # extract
-
Link data acquisition module is focused
org.apache.skywalking.apm.agent.core.context.ContextManager
class. ContextManager class is the hub of all kinds of skywalking agent plug-ins. Skywalking plug different components, such as mq, dubbo, tomcat, spring, etc. skywalking agent plug-ins are to create and control TracingContext, TraceSegment, EntrySpan, ExitSpan, ContextCarrier and other data by calling ContextManager. It can be said ContextManager control the life cycle of the data link in the agent. -
Focus link data upload module
org.apache.skywalking.apm.agent.core.remote
packageTraceSegmentServiceClient
classes. After the link data segment (TraceSegment) within the node ContextManager collection, data will be reported to the notification TraceSegmentServiceClient Collector Server. Which involvesTracingContextListener
the skywalking package内存MQ
assembly, we will analyze in detail later.
Each section above involve complex processing logic behind will be divided into articles 11 to do a detailed explanation.
Skywalking link ID generation process and a Java implementation
Official documents Skywalking-Cross-Process-Propagation- Headers-Protocol-v1 has made a detailed explanation, this paper Java implementation for further analysis.
1. Trace Segment Id protocol specification
The trace segment id is the unique id for the part of the distributed trace. Each id is only used in a single thread. The id includes three parts(Long), e.g. "1.2343.234234234
- The first one represents application instance id, which assigned by collector. (most likely just an integer value, would be helpful in protobuf)
- The second one represents thread id. (In Java most likely just an integer value, would be helpful in protobuf)
- The third one also has two parts 3.1. A timestamp, measured in milliseconds 3.2.A seq, in current thread, between 0(included) and 9999(included)
If you are using other language, you can generate your own id, but make sure it is unique and combined by three longs.
Details for
- trace segment is distributed call chain abstract, can be understood as each service node in a distributed system.
- Protocol requires that trace segment id consists of three parts, each part is a Long-type values. Three parts are connected by a dot data form trace segment id string.
- Trace segment id string generated, only three need to ensure a Long constituted by the ID, and can be globally unique. We did not do conventions for logical meaning three portions may be specified by the developer itself.
- java agent generated three-part original rule ID is described. That is: the first representing the application instance ID, the second part is thread number, and a third portion of the timestamp increment sequence.
2. Trace Segment Id generation process
First skywalking java agent provides a org.apache.skywalking.apm.agent.core.context.ids.ID
data structure for the class described in the protocol specification, and provides a method for checking the legitimacy and serialization method isValid transform ,? rewriting toString, equals, hashCode method. By grpc reported to the link acquisition data collector server transform provides a method for data sequence into protobuf. Such as org.apache.skywalking.apm.agent.core.context.trace.TraceSegment#transform
, org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef#transform
and so on.
Trace Segment Id creation occurred in the instantiation TracingContext
process. TracingContext.new()
FIG refer to the following sequence:
Trace Segment Id
process by calling
org.apache.skywalking.apm.agent.core.context.ids.GlobalIdGenerator#generate
meet the protocol specification ID string generation method.
3. Global(Distributed) Trace Id与Trace Segment Id
3.1 difference
There are two types of skywalking globally unique id, i.e. Global (Distributed) Trace Id and Trace Segment Id. Global (Distributed) Trace Id is the unique ID of a link in the distribution system. Trace Segment Id link refers to the link segment ID distribution system through which service node matter generated. Both are to-many relationship.
3.2 generation process
FIG show the above sequence, when instantiating TracingContext, instantiates TraceSegment, and in the instance of TraceSegment, while generating the Global (Distributed) Trace Id and Trace Segment Id, the following source code:
public TraceSegment() {
this.traceSegmentId = GlobalIdGenerator.generate();
this.spans = new LinkedList<AbstractTracingSpan>();
this.relatedGlobalTraces = new DistributedTraceIds();
this.relatedGlobalTraces.append(new NewDistributedTraceId());
}
复制代码
- Generating trace segment id wherein a first line of code
this.traceSegmentId = GlobalIdGenerator.generate();
, that is generated Trace Segment Id, called once GlobalIdGenerator.generate () method. In the first four lines of codenew NewDistributedTraceId()
to create the Global (Distributed) Trace Id. Source code as follows, showing the same callingGlobalIdGenerator.generate()
method.
public class NewDistributedTraceId extends DistributedTraceId {
public NewDistributedTraceId() {
super(GlobalIdGenerator.generate());
}
}
复制代码
- Generation Global (Distributed) Logical Link Trace Id global id of more complex, java agent encapsulates id for this
DistributedTraceIds
class, by adding append method classNewDistributedTraceId
object representing a Global (Distributed) Trace Id data. NewDistributedTraceId is a subclass of DistributedTraceId, and DistributedTraceId class is the class of the package ID, provides some utility methods.DistributedTraceIds
Represents a collection of related link id, in most cases contain only one link id.
About 3.3 GlobalIdGenerator
Id are two calls to generate the same way, that is org.apache.skywalking.apm.agent.core.context.ids.GlobalIdGenerator#generate
. GlobalIdGenerator logic package production string class global ID. Let's look at GlobalIdGenerator#generate
the source code.
public static ID generate() {
if (RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID == DictionaryUtil.nullValue()) {
throw new IllegalStateException();
}
IDContext context = THREAD_ID_SEQUENCE.get();
return new ID(
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID,
Thread.currentThread().getId(),
context.nextSeq()
);
}
复制代码
I.e., the above-described source is eventually returned package according to the protocol specification ID data structure, the first parameter is the current instance ID of the application, the second parameter is the current thread number. The third parameter is obtained by IDContext.nextSeq () method. The IDContext is obtained from ThreadContext in, so IDContext thread is unique. IDContext source is better understood, IDContext.nextSeq () Returns a string from the current timestamp and increasing the number of combinations of thread.
4. Summary
When summary, the start of the new link, generate TraceSegment example, the first call GlobalIdGenerator.generate()
as a Trace Segment Id
second call GlobalIdGenerator.generate()
as Global(Distributed) Trace Id
.
5. notice
org.apache.skywalking.apm.agent.core.context.trace.TraceSegment
Skywalking java agent is a logical link core classes will explain later separately.
Author: ECHINFO show senior -Frank