提交 57b69fb8 编写于 作者: wu-sheng's avatar wu-sheng

Support #299 and #301

上级 7a91ef7a
......@@ -33,6 +33,8 @@ message TraceSegmentReference {
int32 networkAddressId = 6;
string entryServiceName = 7;
int32 entryServiceId = 8;
string parentServiceName = 9;
int32 parentServiceId = 10;
}
message SpanObject {
......
......@@ -28,6 +28,8 @@ public class ContextCarrier implements Serializable {
private String entryOperationName;
private String parentOperationName;
/**
* {@link DistributedTraceId}
*/
......@@ -47,7 +49,8 @@ public class ContextCarrier implements Serializable {
this.getApplicationInstanceId() + "",
this.getPeerHost(),
this.getEntryOperationName(),
this.serializeDistributedTraceId());
this.getParentOperationName(),
this.getPrimaryDistributedTraceId());
} else {
return "";
}
......@@ -60,15 +63,16 @@ public class ContextCarrier implements Serializable {
*/
public ContextCarrier deserialize(String text) {
if (text != null) {
String[] parts = text.split("\\|", 6);
if (parts.length == 6) {
String[] parts = text.split("\\|", 7);
if (parts.length == 7) {
try {
this.traceSegmentId = parts[0];
this.spanId = Integer.parseInt(parts[1]);
this.applicationInstanceId = Integer.parseInt(parts[2]);
this.peerHost = parts[3];
this.entryOperationName = parts[4];
this.primaryDistributedTraceId = new PropagatedTraceId(parts[5]);
this.parentOperationName = parts[5];
this.primaryDistributedTraceId = new PropagatedTraceId(parts[6]);
} catch (NumberFormatException e) {
}
......@@ -88,6 +92,7 @@ public class ContextCarrier implements Serializable {
&& applicationInstanceId != DictionaryUtil.nullValue()
&& !StringUtil.isEmpty(peerHost)
&& !StringUtil.isEmpty(entryOperationName)
&& !StringUtil.isEmpty(parentOperationName)
&& primaryDistributedTraceId != null;
}
......@@ -103,6 +108,14 @@ public class ContextCarrier implements Serializable {
this.entryOperationName = entryOperationId + "";
}
void setParentOperationName(String parentOperationName) {
this.parentOperationName = '#' + parentOperationName;
}
void setParentOperationId(int parentOperationId) {
this.parentOperationName = parentOperationId + "";
}
public String getTraceSegmentId() {
return traceSegmentId;
}
......@@ -147,7 +160,11 @@ public class ContextCarrier implements Serializable {
this.primaryDistributedTraceId = distributedTraceIds.get(0);
}
private String serializeDistributedTraceId() {
private String getPrimaryDistributedTraceId() {
return primaryDistributedTraceId.toString();
}
public String getParentOperationName() {
return parentOperationName;
}
}
......@@ -2,7 +2,6 @@ package org.skywalking.apm.agent.core.context;
import java.util.List;
import org.skywalking.apm.agent.core.context.ids.DistributedTraceId;
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.skywalking.apm.util.StringUtil;
/**
......@@ -24,24 +23,38 @@ public class ContextSnapshot {
private String entryOperationName;
private String parentOperationName;
/**
* {@link DistributedTraceId}
*/
private DistributedTraceId primaryDistributedTraceId;
ContextSnapshot(String traceSegmentId, int spanId,
List<DistributedTraceId> distributedTraceIds, int entryServiceId, String entryOperationName) {
List<DistributedTraceId> distributedTraceIds) {
this.traceSegmentId = traceSegmentId;
this.spanId = spanId;
this.primaryDistributedTraceId = distributedTraceIds.get(0);
if (entryServiceId == DictionaryUtil.nullValue()) {
this.entryOperationName = "#" + entryOperationName;
} else {
this.entryOperationName = String.valueOf(entryServiceId);
if (distributedTraceIds != null) {
this.primaryDistributedTraceId = distributedTraceIds.get(0);
}
}
public void setEntryOperationName(String entryOperationName) {
this.entryOperationName = "#" + entryOperationName;
}
public void setEntryOperationId(int entryOperationId) {
this.entryOperationName = entryOperationId + "";
}
public void setParentOperationName(String parentOperationName) {
this.parentOperationName = "#" + parentOperationName;
}
public void setParentOperationId(int parentOperationId) {
this.parentOperationName = parentOperationId + "";
}
public DistributedTraceId getDistributedTraceId() {
return primaryDistributedTraceId;
}
......@@ -54,11 +67,16 @@ public class ContextSnapshot {
return spanId;
}
public String getParentOperationName() {
return parentOperationName;
}
public boolean isValid() {
return traceSegmentId != null
&& spanId > -1
&& primaryDistributedTraceId != null
&& !StringUtil.isEmpty(entryOperationName);
&& !StringUtil.isEmpty(entryOperationName)
&& !StringUtil.isEmpty(parentOperationName);
}
public String getEntryOperationName() {
......
......@@ -33,7 +33,7 @@ public class IgnoredTracerContext implements AbstractTracerContext {
}
@Override public ContextSnapshot capture() {
return new ContextSnapshot(null, -1, null, 0, null);
return new ContextSnapshot(null, -1, null);
}
@Override public void continued(ContextSnapshot snapshot) {
......
......@@ -96,8 +96,8 @@ public class TracingContext implements AbstractTracerContext {
String operationName;
if (refs != null && refs.size() > 0) {
TraceSegmentRef ref = refs.get(0);
operationId = ref.getOperationId();
operationName = ref.getOperationName();
operationId = ref.getEntryOperationId();
operationName = ref.getEntryOperationName();
} else {
AbstractTracingSpan firstSpan = first();
operationId = firstSpan.getOperationId();
......@@ -109,6 +109,13 @@ public class TracingContext implements AbstractTracerContext {
carrier.setEntryOperationId(operationId);
}
int parentOperationId = first().getOperationId();
if (parentOperationId == DictionaryUtil.nullValue()) {
carrier.setParentOperationName(first().getOperationName());
} else {
carrier.setParentOperationId(parentOperationId);
}
carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces());
}
......@@ -133,19 +140,32 @@ public class TracingContext implements AbstractTracerContext {
@Override
public ContextSnapshot capture() {
List<TraceSegmentRef> refs = this.segment.getRefs();
ContextSnapshot snapshot = new ContextSnapshot(segment.getTraceSegmentId(),
activeSpan().getSpanId(),
segment.getRelatedGlobalTraces());
int entryOperationId;
String entryOperationName;
AbstractTracingSpan firstSpan = first();
if (refs != null && refs.size() > 0) {
TraceSegmentRef ref = refs.get(0);
return new ContextSnapshot(segment.getTraceSegmentId(),
activeSpan().getSpanId(),
segment.getRelatedGlobalTraces(), ref.getOperationId(), ref.getOperationName()
);
entryOperationId = ref.getEntryOperationId();
entryOperationName = ref.getEntryOperationName();
} else {
AbstractTracingSpan firstSpan = first();
return new ContextSnapshot(segment.getTraceSegmentId(),
activeSpan().getSpanId(),
segment.getRelatedGlobalTraces(), firstSpan.getOperationId(), firstSpan.getOperationName()
);
entryOperationId = firstSpan.getOperationId();
entryOperationName = firstSpan.getOperationName();
}
if (entryOperationId == DictionaryUtil.nullValue()) {
snapshot.setEntryOperationName(entryOperationName);
} else {
snapshot.setEntryOperationId(entryOperationId);
}
if (firstSpan.getOperationId() == DictionaryUtil.nullValue()) {
snapshot.setParentOperationName(firstSpan.getOperationName());
} else {
snapshot.setParentOperationId(firstSpan.getOperationId());
}
return snapshot;
}
/**
......
......@@ -25,9 +25,13 @@ public class TraceSegmentRef {
private int peerId = DictionaryUtil.nullValue();
private String operationName;
private String entryOperationName;
private int operationId = DictionaryUtil.nullValue();
private int entryOperationId = DictionaryUtil.nullValue();
private String parentOperationName;
private int parentOperationId = DictionaryUtil.nullValue();
/**
* Transform a {@link ContextCarrier} to the <code>TraceSegmentRef</code>
......@@ -47,9 +51,15 @@ public class TraceSegmentRef {
}
String entryOperationName = carrier.getEntryOperationName();
if (entryOperationName.charAt(0) == '#') {
this.operationName = entryOperationName.substring(1);
this.entryOperationName = entryOperationName.substring(1);
} else {
this.entryOperationId = Integer.parseInt(entryOperationName);
}
String parentOperationName = carrier.getParentOperationName();
if (parentOperationName.charAt(0) == '#') {
this.parentOperationName = parentOperationName.substring(1);
} else {
this.operationId = Integer.parseInt(entryOperationName);
this.parentOperationId = Integer.parseInt(parentOperationName);
}
}
......@@ -59,18 +69,24 @@ public class TraceSegmentRef {
this.spanId = snapshot.getSpanId();
String entryOperationName = snapshot.getEntryOperationName();
if (entryOperationName.charAt(0) == '#') {
this.operationName = entryOperationName.substring(1);
this.entryOperationName = entryOperationName.substring(1);
} else {
this.operationId = Integer.parseInt(entryOperationName);
this.entryOperationId = Integer.parseInt(entryOperationName);
}
String parentOperationName = snapshot.getParentOperationName();
if (parentOperationName.charAt(0) == '#') {
this.parentOperationName = parentOperationName.substring(1);
} else {
this.parentOperationId = Integer.parseInt(parentOperationName);
}
}
public String getOperationName() {
return operationName;
public String getEntryOperationName() {
return entryOperationName;
}
public int getOperationId() {
return operationId;
public int getEntryOperationId() {
return entryOperationId;
}
public TraceSegmentReference transform() {
......@@ -89,10 +105,15 @@ public class TraceSegmentRef {
refBuilder.setParentTraceSegmentId(traceSegmentId);
refBuilder.setParentSpanId(spanId);
if (operationId == DictionaryUtil.nullValue()) {
refBuilder.setEntryServiceName(operationName);
if (entryOperationId == DictionaryUtil.nullValue()) {
refBuilder.setEntryServiceName(entryOperationName);
} else {
refBuilder.setEntryServiceId(entryOperationId);
}
if (parentOperationId == DictionaryUtil.nullValue()) {
refBuilder.setParentServiceName(parentOperationName);
} else {
refBuilder.setEntryServiceId(operationId);
refBuilder.setParentServiceId(parentOperationId);
}
return refBuilder.build();
}
......
package org.skywalking.apm.agent.core.context;
import com.google.instrumentation.trace.Span;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
import org.skywalking.apm.agent.core.context.tag.Tags;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
......@@ -82,7 +79,7 @@ public class ContextManagerTest {
@Test
public void createMultipleEntrySpan() {
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2");
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
assertTrue(contextCarrier.isValid());
AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testFirstEntry", contextCarrier);
......@@ -115,8 +112,8 @@ public class ContextManagerTest {
TraceSegmentRef ref = actualSegment.getRefs().get(0);
assertThat(TraceSegmentRefHelper.getPeerHost(ref), is("192.168.1.8 :18002"));
assertThat(ref.getOperationName(), is("/portal/"));
assertThat(ref.getOperationId(), is(0));
assertThat(ref.getEntryOperationName(), is("/portal/"));
assertThat(ref.getEntryOperationId(), is(0));
List<AbstractTracingSpan> spanList = SegmentHelper.getSpan(actualSegment);
assertThat(spanList.size(), is(2));
......@@ -205,7 +202,7 @@ public class ContextManagerTest {
@Test
public void testTransform() throws InvalidProtocolBufferException {
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2");
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
assertTrue(contextCarrier.isValid());
AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testFirstEntry", contextCarrier);
......
......@@ -148,7 +148,7 @@ public class DubboInterceptorTest {
@Test
public void testProviderWithAttachment() throws Throwable {
when(rpcContext.isConsumerSide()).thenReturn(false);
when(rpcContext.getAttachment(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2");
when(rpcContext.getAttachment(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
......@@ -160,7 +160,7 @@ public class DubboInterceptorTest {
when(rpcContext.isConsumerSide()).thenReturn(false);
FieldSetter.setStaticValue(BugFixActive.class, "ACTIVE", true);
testParam.setTraceContext("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2");
testParam.setTraceContext("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
dubboInterceptor.beforeMethod(enhancedInstance, "invoke", allArguments, argumentTypes, methodInterceptResult);
dubboInterceptor.afterMethod(enhancedInstance, "invoke", allArguments, argumentTypes, result);
......
......@@ -91,7 +91,7 @@ public class MotanProviderInterceptorTest {
@Test
public void testInvokerWithRefSegment() throws Throwable {
HashMap attachments = new HashMap();
attachments.put(Config.Plugin.Propagation.HEADER_NAME, "S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2");
attachments.put(Config.Plugin.Propagation.HEADER_NAME, "S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
when(request.getAttachments()).thenReturn(attachments);
invokeInterceptor.beforeMethod(enhancedInstance, "execute", arguments, argumentType, null);
......
......@@ -90,7 +90,7 @@ public class ResinV3InterceptorTest {
@Test
public void testWithSerializedContextData() throws Throwable {
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2");
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
interceptor.beforeMethod(enhancedInstance, "service", arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, "service", arguments, argumentType, null);
......
......@@ -92,7 +92,7 @@ public class ResinV4InterceptorTest {
@Test
public void testWithSerializedContextData() throws Throwable {
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2");
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
interceptor.beforeMethod(enhancedInstance, "service", arguments, argumentType, methodInterceptResult);
interceptor.afterMethod(enhancedInstance, "service", arguments, argumentType, null);
......
......@@ -82,7 +82,7 @@ public class TomcatInterceptorTest {
@Test
public void testWithSerializedContextData() throws Throwable {
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|T.1499176688386.581928182.80935.69.2");
when(request.getHeader(Config.Plugin.Propagation.HEADER_NAME)).thenReturn("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8:18002|#/portal/|#/portal/|T.1499176688386.581928182.80935.69.2");
tomcatInterceptor.beforeMethod(enhancedInstance, "invoke", arguments, argumentType, methodInterceptResult);
tomcatInterceptor.afterMethod(enhancedInstance, "invoke", arguments, argumentType, null);
......
......@@ -181,7 +181,7 @@ public class SkywalkingSpanActivationTest {
.withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), 8080);
startSpan();
extractInterceptor.afterMethod(enhancedInstance, "extract",
new Object[] {"S.1499746282749.1100157028.88023.1.1|0|1|#127.0.0.1:8080|#testOperationName|T.1499746282768.1100157028.88023.1.2"}, new Class[] {String.class}, null);
new Object[] {"S.1499746282749.1100157028.88023.1.1|0|1|#127.0.0.1:8080|#testOperationName|#testOperationName|T.1499746282768.1100157028.88023.1.2"}, new Class[] {String.class}, null);
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册