Merge pull request #671 from ascrutae/feature/trace-context

[Agent] Cannot propagate the context across threads
This commit is contained in:
吴晟 Wu Sheng 2017-12-14 11:38:56 +08:00 committed by GitHub
commit eee5e65798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 34 deletions

View File

@ -16,7 +16,6 @@
*
*/
package org.apache.skywalking.apm.agent.core.context;
import java.util.LinkedList;
@ -50,6 +49,7 @@ import org.apache.skywalking.apm.agent.core.sampling.SamplingService;
* ContextCarrier} or {@link ContextSnapshot}.
*
* @author wusheng
* @author zhang xin
*/
public class TracingContext implements AbstractTracerContext {
/**
@ -159,7 +159,7 @@ public class TracingContext implements AbstractTracerContext {
this.segment.relatedGlobalTraces(carrier.getDistributedTraceId());
AbstractSpan span = this.activeSpan();
if (span instanceof EntrySpan) {
((EntrySpan)span).ref(ref);
span.ref(ref);
}
}
@ -213,7 +213,9 @@ public class TracingContext implements AbstractTracerContext {
*/
@Override
public void continued(ContextSnapshot snapshot) {
this.segment.ref(new TraceSegmentRef(snapshot));
TraceSegmentRef segmentRef = new TraceSegmentRef(snapshot);
this.segment.ref(segmentRef);
this.activeSpan().ref(segmentRef);
this.segment.relatedGlobalTraces(snapshot.getDistributedTraceId());
}

View File

@ -16,7 +16,6 @@
*
*/
package org.apache.skywalking.apm.agent.core.context.trace;
import java.util.Map;
@ -110,4 +109,11 @@ public interface AbstractSpan {
String getOperationName();
AbstractSpan setOperationId(int operationId);
/**
* Reference other trace segment.
*
* @param ref segment ref
*/
void ref(TraceSegmentRef ref);
}

View File

@ -16,7 +16,6 @@
*
*/
package org.apache.skywalking.apm.agent.core.context.trace;
import java.util.LinkedList;
@ -64,6 +63,13 @@ public abstract class AbstractTracingSpan implements AbstractSpan {
*/
protected List<LogDataEntity> logs;
/**
* The refs of parent trace segments, except the primary one. For most RPC call, {@link #refs} contains only one
* element, but if this segment is a start span of batch process, the segment faces multi parents, at this moment,
* we use this {@link #refs} to link them.
*/
protected List<TraceSegmentRef> refs;
protected AbstractTracingSpan(int spanId, int parentSpanId, String operationName) {
this.operationName = operationName;
this.operationId = DictionaryUtil.nullValue();
@ -273,7 +279,21 @@ public abstract class AbstractTracingSpan implements AbstractSpan {
spanBuilder.addLogs(log.transform());
}
}
if (this.refs != null) {
for (TraceSegmentRef ref : this.refs) {
spanBuilder.addRefs(ref.transform());
}
}
return spanBuilder;
}
@Override public void ref(TraceSegmentRef ref) {
if (refs == null) {
refs = new LinkedList<TraceSegmentRef>();
}
if (!refs.contains(ref)) {
refs.add(ref);
}
}
}

View File

@ -16,13 +16,9 @@
*
*/
package org.apache.skywalking.apm.agent.core.context.trace;
import java.util.LinkedList;
import java.util.List;
import org.apache.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.apache.skywalking.apm.network.proto.SpanObject;
import org.apache.skywalking.apm.network.trace.component.Component;
/**
@ -38,12 +34,6 @@ import org.apache.skywalking.apm.network.trace.component.Component;
* @author wusheng
*/
public class EntrySpan extends StackBasedTracingSpan {
/**
* The refs of parent trace segments, except the primary one. For most RPC call, {@link #refs} contains only one
* element, but if this segment is a start span of batch process, the segment faces multi parents, at this moment,
* we use this {@link #refs} to link them.
*/
private List<TraceSegmentRef> refs;
private int currentMaxDepth;
@ -136,25 +126,6 @@ public class EntrySpan extends StackBasedTracingSpan {
return false;
}
@Override public SpanObject.Builder transform() {
SpanObject.Builder builder = super.transform();
if (refs != null) {
for (TraceSegmentRef ref : refs) {
builder.addRefs(ref.transform());
}
}
return builder;
}
public void ref(TraceSegmentRef ref) {
if (refs == null) {
refs = new LinkedList<TraceSegmentRef>();
}
if (!refs.contains(ref)) {
refs.add(ref);
}
}
private void clearWhenRestart() {
this.componentId = DictionaryUtil.nullValue();
this.componentName = null;

View File

@ -99,4 +99,7 @@ public class NoopSpan implements AbstractSpan {
@Override public AbstractSpan setOperationId(int operationId) {
return this;
}
@Override public void ref(TraceSegmentRef ref) {
}
}