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

Refactor create span methods

上级 18fed69e
package org.skywalking.apm.agent.core.context;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.SpanType;
/**
* The <code>AbstractTracerContext</code> represents the tracer context manager.
......@@ -15,9 +14,11 @@ public interface AbstractTracerContext {
String getGlobalTraceId();
AbstractSpan createSpan(String operationName, SpanType spanType);
AbstractSpan createEntrySpan(String operationName);
AbstractSpan createSpan(String operationName, SpanType spanType, Injectable injectable);
AbstractSpan createLocalSpan(String operationName);
AbstractSpan createExitSpan(String operationName, String remotePeer);
AbstractSpan activeSpan();
......
......@@ -5,7 +5,6 @@ import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.conf.Config;
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.SpanType;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.skywalking.apm.agent.core.sampling.SamplingService;
......@@ -77,7 +76,7 @@ public class ContextManager implements TracingContextListener, BootService, Igno
}
}
public static AbstractSpan createSpan(String operationName, ContextCarrier carrier) {
public static AbstractSpan createEntrySpan(String operationName, ContextCarrier carrier) {
if (carrier == null) {
throw new IllegalArgumentException("ContextCarrier can't be null.");
}
......@@ -90,21 +89,21 @@ public class ContextManager implements TracingContextListener, BootService, Igno
} else {
context = getOrCreate(operationName, false);
}
return context.createSpan(operationName, SpanType.ENTRY);
return context.createEntrySpan(operationName);
}
public static AbstractSpan createSpan(String operationName) {
public static AbstractSpan createLocalSpan(String operationName) {
AbstractTracerContext context = getOrCreate(operationName, false);
return context.createSpan(operationName, SpanType.LOCAL);
return context.createLocalSpan(operationName);
}
public static AbstractSpan createSpan(String operationName, Injectable injectable) {
if (injectable == null) {
throw new IllegalArgumentException("Injectable can't be null.");
public static AbstractSpan createExitSpan(String operationName, ContextCarrier carrier, String remotePeer) {
if (carrier == null) {
throw new IllegalArgumentException("ContextCarrier can't be null.");
}
AbstractTracerContext context = getOrCreate(operationName, false);
AbstractSpan span = context.createSpan(operationName, SpanType.EXIT);
context.inject(injectable.getCarrier());
AbstractSpan span = context.createExitSpan(operationName, remotePeer);
context.inject(carrier);
return span;
}
......
......@@ -4,7 +4,6 @@ import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.skywalking.apm.agent.core.context.trace.NoopSpan;
import org.skywalking.apm.agent.core.context.trace.SpanType;
/**
* The <code>IgnoredTracerContext</code> represent a context should be ignored.
......@@ -38,12 +37,19 @@ public class IgnoredTracerContext implements AbstractTracerContext {
}
@Override
public AbstractSpan createSpan(String operationName, SpanType spanType) {
return createSpan(operationName, spanType, null);
public AbstractSpan createEntrySpan(String operationName) {
stackDepth++;
return NOOP_SPAN;
}
@Override
public AbstractSpan createLocalSpan(String operationName) {
stackDepth++;
return NOOP_SPAN;
}
@Override
public AbstractSpan createSpan(String operationName, SpanType spanType, Injectable injectable) {
public AbstractSpan createExitSpan(String operationName, String remotePeer) {
stackDepth++;
return NOOP_SPAN;
}
......
......@@ -8,7 +8,6 @@ import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.skywalking.apm.agent.core.context.trace.EntrySpan;
import org.skywalking.apm.agent.core.context.trace.ExitSpan;
import org.skywalking.apm.agent.core.context.trace.LocalSpan;
import org.skywalking.apm.agent.core.context.trace.SpanType;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
import org.skywalking.apm.agent.core.dictionary.DictionaryManager;
......@@ -77,88 +76,81 @@ public class TracingContext implements AbstractTracerContext {
}
@Override
public AbstractSpan createSpan(String operationName, SpanType spanType) {
return createSpan(operationName, spanType, null);
public AbstractSpan createEntrySpan(final String operationName) {
AbstractTracingSpan parentSpan = peek();
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
if (parentSpan == null) {
return (AbstractTracingSpan)DictionaryManager.findOperationNameCodeSection()
.find(segment.getApplicationId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int operationId) {
return new EntrySpan(spanIdGenerator++, parentSpanId, operationId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return new EntrySpan(spanIdGenerator++, parentSpanId, operationName);
}
});
} else if (parentSpan.isEntry()) {
return parentSpan;
} else {
throw new IllegalStateException("The Entry Span can't be the child of Non-Entry Span");
}
}
@Override
public AbstractSpan createSpan(String operationName, SpanType spanType, Injectable injectable) {
public AbstractSpan createLocalSpan(final String operationName) {
AbstractTracingSpan parentSpan = peek();
AbstractTracingSpan span = createByType(spanIdGenerator++, -1, operationName,
spanType, injectable.getPeer(), parentSpan);
return span.start();
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
return (AbstractTracingSpan)DictionaryManager.findOperationNameCodeSection()
.find(segment.getApplicationId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int operationId) {
return new LocalSpan(spanIdGenerator++, parentSpanId, operationId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new LocalSpan(spanIdGenerator++, parentSpanId, operationName);
}
});
}
private AbstractTracingSpan createByType(final int spanId, final int parentSpanId,
final String operationName, SpanType spanType,
final String peerHost, AbstractTracingSpan parentSpan) {
switch (spanType) {
case LOCAL:
return (AbstractTracingSpan)DictionaryManager.findOperationNameCodeSection()
.find(segment.getApplicationId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override
public AbstractSpan createExitSpan(final String operationName, final String remotePeer) {
AbstractTracingSpan parentSpan = peek();
if (parentSpan != null && parentSpan.isExit()) {
return parentSpan;
} else {
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
return (AbstractTracingSpan)DictionaryManager.findApplicationCodeSection()
.find(remotePeer).doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int operationId) {
return new LocalSpan(spanId, parentSpanId, operationId);
public Object doProcess(final int applicationId) {
return DictionaryManager.findOperationNameCodeSection()
.find(applicationId, operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int peerId) {
return new ExitSpan(spanIdGenerator++, parentSpanId, applicationId, peerId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanIdGenerator++, parentSpanId, applicationId, remotePeer);
}
});
}
}, new PossibleFound.NotFoundAndObtain() {
},
new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new LocalSpan(spanId, parentSpanId, operationName);
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer);
}
});
case EXIT:
if (parentSpan != null && parentSpan.isExit()) {
return parentSpan;
} else {
return (AbstractTracingSpan)DictionaryManager.findApplicationCodeSection()
.find(peerHost).doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(final int applicationId) {
return DictionaryManager.findOperationNameCodeSection()
.find(applicationId, operationName)
.doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(int peerId) {
return new ExitSpan(spanId, parentSpanId, applicationId, peerId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanId, parentSpanId, applicationId, peerHost);
}
});
}
},
new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanId, parentSpanId, operationName, peerHost);
}
});
}
case ENTRY:
if (parentSpan.isEntry()) {
return parentSpan;
} else if (parentSpan == null) {
return (AbstractTracingSpan)DictionaryManager.findOperationNameCodeSection()
.find(segment.getApplicationId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int operationId) {
return new EntrySpan(spanId, parentSpanId, operationId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return new EntrySpan(spanId, parentSpanId, operationName);
}
});
} else {
throw new IllegalStateException("The Entry Span can't be the child of Non-Entry Span");
}
default:
throw new IllegalStateException("Unsupported Span type:" + spanType);
}
}
......
package org.skywalking.apm.agent.core.context.trace;
/**
* @author wusheng
*/
public enum SpanType {
ENTRY,
EXIT,
LOCAL
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册