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

Fix bugs and refactor EntrySpan and Exitspan.

上级 ef8b5aaa
......@@ -3,6 +3,7 @@ package org.skywalking.apm.agent.core.context;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.skywalking.apm.agent.core.context.trace.EntrySpan;
......@@ -290,19 +291,19 @@ public class TracingContext implements AbstractTracerContext {
.find(remotePeer).doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(final int applicationId) {
public Object doProcess(final int peerId) {
return DictionaryManager.findOperationNameCodeSection()
.findOrPrepare4Register(applicationId, operationName)
.findOnly(RemoteDownstreamConfig.Agent.APPLICATION_ID, operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int operationId) {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationId, applicationId);
return new ExitSpan(spanIdGenerator++, parentSpanId, operationId, peerId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer);
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, peerId);
}
});
}
......@@ -310,7 +311,20 @@ public class TracingContext implements AbstractTracerContext {
new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer);
return DictionaryManager.findOperationNameCodeSection()
.findOnly(RemoteDownstreamConfig.Agent.APPLICATION_ID, operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int operationId) {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationId, remotePeer);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer);
}
});
}
});
push(exitSpan);
......
package org.skywalking.apm.agent.core.context.trace;
import org.skywalking.apm.agent.core.dictionary.DictionaryManager;
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.skywalking.apm.agent.core.dictionary.PossibleFound;
import org.skywalking.apm.network.trace.component.Component;
/**
......@@ -18,19 +16,16 @@ import org.skywalking.apm.network.trace.component.Component;
*
* @author wusheng
*/
public class EntrySpan extends AbstractTracingSpan {
private int stackDepth;
public class EntrySpan extends StackBasedTracingSpan {
private int currentMaxDepth;
public EntrySpan(int spanId, int parentSpanId, String operationName) {
super(spanId, parentSpanId, operationName);
this.stackDepth = 0;
this.currentMaxDepth = 0;
}
public EntrySpan(int spanId, int parentSpanId, int operationId) {
super(spanId, parentSpanId, operationId);
this.stackDepth = 0;
this.currentMaxDepth = 0;
}
......@@ -81,31 +76,6 @@ public class EntrySpan extends AbstractTracingSpan {
}
}
@Override
public boolean finish(TraceSegment owner) {
if (--stackDepth == 0) {
if (this.operationId == DictionaryUtil.nullValue()) {
this.operationId = (Integer)DictionaryManager.findOperationNameCodeSection()
.findOrPrepare4Register(owner.getApplicationId(), operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int value) {
return value;
}
},
new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return DictionaryUtil.nullValue();
}
}
);
}
return super.finish(owner);
} else {
return false;
}
}
@Override
public AbstractTracingSpan setOperationName(String operationName) {
if (stackDepth == currentMaxDepth) {
......
......@@ -17,32 +17,34 @@ import org.skywalking.apm.network.trace.component.Component;
*
* @author wusheng
*/
public class ExitSpan extends AbstractTracingSpan {
private int stackDepth;
public class ExitSpan extends StackBasedTracingSpan {
private String peer;
private int peerId;
public ExitSpan(int spanId, int parentSpanId, String operationName, String peer) {
super(spanId, parentSpanId, operationName);
this.stackDepth = 0;
this.peer = peer;
this.peerId = DictionaryUtil.nullValue();
}
public ExitSpan(int spanId, int parentSpanId, int operationId, int peerId) {
super(spanId, parentSpanId, operationId);
this.stackDepth = 0;
this.peer = null;
this.peerId = peerId;
}
public ExitSpan(int spanId, int parentSpanId, int operationId, String peer) {
super(spanId, parentSpanId, operationId);
this.stackDepth = 0;
this.peer = peer;
this.peerId = DictionaryUtil.nullValue();
}
public ExitSpan(int spanId, int parentSpanId, String operationName, int peerId) {
super(spanId, parentSpanId, operationName);
this.peer = null;
this.peerId = peerId;
}
/**
* Set the {@link #startTime}, when the first start, which means the first service provided.
*/
......@@ -62,15 +64,6 @@ public class ExitSpan extends AbstractTracingSpan {
return this;
}
@Override
public boolean finish(TraceSegment owner) {
if (--stackDepth == 0) {
return super.finish(owner);
} else {
return false;
}
}
@Override
public AbstractTracingSpan setLayer(SpanLayer layer) {
if (stackDepth == 1) {
......
package org.skywalking.apm.agent.core.context.trace;
import org.skywalking.apm.agent.core.dictionary.DictionaryManager;
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.skywalking.apm.agent.core.dictionary.PossibleFound;
/**
* The <code>StackBasedTracingSpan</code> represents a span with an inside stack construction.
*
* This kind of span can start and finish multi times in a stack-like invoke line.
*
* @author wusheng
*/
public abstract class StackBasedTracingSpan extends AbstractTracingSpan {
protected int stackDepth;
protected StackBasedTracingSpan(int spanId, int parentSpanId, String operationName) {
super(spanId, parentSpanId, operationName);
this.stackDepth = 0;
}
protected StackBasedTracingSpan(int spanId, int parentSpanId, int operationId) {
super(spanId, parentSpanId, operationId);
this.stackDepth = 0;
}
@Override
public boolean finish(TraceSegment owner) {
if (--stackDepth == 0) {
if (this.operationId == DictionaryUtil.nullValue()) {
this.operationId = (Integer)DictionaryManager.findOperationNameCodeSection()
.findOrPrepare4Register(owner.getApplicationId(), operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int value) {
return value;
}
},
new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return DictionaryUtil.nullValue();
}
}
);
}
return super.finish(owner);
} else {
return false;
}
}
}
......@@ -8,7 +8,7 @@ import org.skywalking.apm.agent.core.context.trace.LogDataEntity;
public class AbstractTracingSpanHelper {
public static int getParentSpanId(AbstractTracingSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "parentSpanId");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "parentSpanId");
} catch (Exception e) {
}
......@@ -17,7 +17,7 @@ public class AbstractTracingSpanHelper {
public static List<LogDataEntity> getLogs(AbstractTracingSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "logs");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "logs");
} catch (Exception e) {
}
......
......@@ -10,9 +10,10 @@ public class FieldGetter {
return (T)field.get(instance);
}
public static <T> T getParentFieldValue(Object instance,
public static <T> T get2LevelParentFieldValue(Object instance,
String fieldName) throws IllegalAccessException, NoSuchFieldException {
Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName);
Field field = instance.getClass().getSuperclass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T)field.get(instance);
}
......
......@@ -7,7 +7,7 @@ public class SpanHelper {
public static SpanLayer getLayer(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "layer");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "layer");
} catch (Exception e) {
}
......@@ -16,7 +16,7 @@ public class SpanHelper {
public static int getComponentId(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "componentId");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "componentId");
} catch (Exception e) {
}
......
......@@ -16,4 +16,12 @@ public class FieldGetter {
field.setAccessible(true);
return (T)field.get(instance);
}
public static <T> T get2LevelParentFieldValue(Object instance,
String fieldName) throws IllegalAccessException, NoSuchFieldException {
Field field = instance.getClass().getSuperclass().getSuperclass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T)field.get(instance);
}
}
......@@ -10,8 +10,13 @@ import org.skywalking.apm.agent.core.context.util.KeyValuePair;
public class SpanHelper {
public static int getParentSpanId(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "parentSpanId");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "parentSpanId");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "parentSpanId");
} catch (Exception e1) {
}
}
return -9999;
......@@ -19,8 +24,13 @@ public class SpanHelper {
public static List<LogDataEntity> getLogs(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "logs");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "logs");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "logs");
} catch (Exception e1) {
}
}
return Collections.emptyList();
......@@ -28,8 +38,13 @@ public class SpanHelper {
public static List<KeyValuePair> getTags(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "tags");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "tags");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "tags");
} catch (Exception e1) {
}
}
return Collections.emptyList();
......@@ -37,8 +52,13 @@ public class SpanHelper {
public static SpanLayer getLayer(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "layer");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "layer");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "layer");
} catch (Exception e1) {
}
}
return null;
......@@ -46,8 +66,13 @@ public class SpanHelper {
public static String getComponentName(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "componentName");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "componentName");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "componentName");
} catch (Exception e1) {
}
}
return null;
......@@ -55,8 +80,13 @@ public class SpanHelper {
public static int getComponentId(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "componentId");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "componentId");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "componentId");
} catch (Exception e1) {
}
}
return -1;
......@@ -64,8 +94,13 @@ public class SpanHelper {
public static boolean getErrorOccurred(AbstractSpan tracingSpan) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "errorOccurred");
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "errorOccurred");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "errorOccurred");
} catch (Exception e1) {
}
}
return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册