Add parts of ignore mechanism, and this is part of new sampling mechanism. All of these are providing higher performance.

This commit is contained in:
wusheng 2017-06-11 22:35:30 +08:00
parent f0e626ff2c
commit cf3d2a4026
7 changed files with 150 additions and 16 deletions

View File

@ -23,4 +23,6 @@ public interface AbstractTracerContext {
void stopSpan(Span span);
void stopSpan(Span span, Long endTime);
void dispose();
}

View File

@ -49,12 +49,16 @@ public class ContextCarrier implements Serializable {
* @return the serialization string.
*/
public String serialize() {
return StringUtil.join('|',
this.getTraceSegmentId(),
this.getSpanId() + "",
this.getApplicationCode(),
this.getPeerHost(),
this.serializeDistributedTraceIds());
if (this.isValid()) {
return StringUtil.join('|',
this.getTraceSegmentId(),
this.getSpanId() + "",
this.getApplicationCode(),
this.getPeerHost(),
this.serializeDistributedTraceIds());
} else {
return "";
}
}
/**

View File

@ -17,7 +17,7 @@ import org.skywalking.apm.agent.core.context.trace.TraceSegment;
*
* @author wusheng
*/
public class ContextManager implements TracerContextListener, BootService {
public class ContextManager implements TracerContextListener, BootService, IgnoreTracerContextListener {
private static ThreadLocal<AbstractTracerContext> CONTEXT = new ThreadLocal<AbstractTracerContext>();
private static AbstractTracerContext get() {
@ -75,21 +75,18 @@ public class ContextManager implements TracerContextListener, BootService {
return get().activeSpan();
}
public static void stopSpan(Span span) {
get().stopSpan(span);
}
public static void stopSpan(Long endTime) {
get().stopSpan(activeSpan(), endTime);
}
public static void stopSpan() {
stopSpan(activeSpan());
get().stopSpan(activeSpan());
}
@Override
public void bootUp() {
TracerContext.ListenerManager.add(this);
IgnoreTracerContext.ListenerManager.add(this);
}
@Override
@ -97,13 +94,23 @@ public class ContextManager implements TracerContextListener, BootService {
CONTEXT.remove();
}
@Override
public void afterFinished(IgnoreTracerContext traceSegment) {
CONTEXT.remove();
}
/**
* The <code>ContextSwitcher</code> gives the chance to switch {@link AbstractTracerContext} in {@link #CONTEXT}.
* The <code>ContextSwitcher</code> gives the chance to switch {@link AbstractTracerContext} in {@link #CONTEXT},
* for ignore, sampling, and analytic trace.
*/
enum ContextSwitcher {
public enum ContextSwitcher {
INSTANCE;
void toNew(AbstractTracerContext context) {
public void toNew(AbstractTracerContext context) {
AbstractTracerContext existedContext = CONTEXT.get();
if (existedContext != null) {
existedContext.dispose();
}
CONTEXT.set(context);
}
}

View File

@ -0,0 +1,104 @@
package org.skywalking.apm.agent.core.context;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.context.trace.Span;
/**
* The <code>IgnoreTracerContext</code> represent a context should be ignored.
* So it just maintains the stack with integer depth.
* All operations through this <code>IgnoreTracerContext</code> will be ignored, with low gc cost.
*
* TODO: Can't return null span
*
* @author wusheng
*/
public class IgnoreTracerContext implements AbstractTracerContext {
private int stackDepth;
public IgnoreTracerContext(int initStackDepth) {
this.stackDepth = initStackDepth;
}
@Override
public void inject(ContextCarrier carrier) {
}
@Override
public void extract(ContextCarrier carrier) {
}
@Override
public String getGlobalTraceId() {
return "[Ignored Trace]";
}
@Override
public Span createSpan(String operationName, boolean isLeaf) {
stackDepth++;
return null;
}
@Override
public Span createSpan(String operationName, long startTime, boolean isLeaf) {
return createSpan(operationName, isLeaf);
}
@Override
public Span activeSpan() {
return null;
}
@Override
public void stopSpan(Span span) {
stackDepth--;
if (stackDepth == 0) {
}
}
@Override
public void stopSpan(Span span, Long endTime) {
stopSpan(span);
}
@Override
public void dispose() {
}
public static class ListenerManager {
private static List<IgnoreTracerContextListener> LISTENERS = new LinkedList<IgnoreTracerContextListener>();
/**
* Add the given {@link IgnoreTracerContextListener} to {@link #LISTENERS} list.
*
* @param listener the new listener.
*/
public static synchronized void add(IgnoreTracerContextListener listener) {
LISTENERS.add(listener);
}
/**
* Notify the {@link IgnoreTracerContext.ListenerManager} about the given {@link IgnoreTracerContext} have
* finished. And trigger {@link IgnoreTracerContext.ListenerManager} to notify all {@link #LISTENERS} 's {@link
* IgnoreTracerContextListener#afterFinished(IgnoreTracerContext)}
*
* @param ignoreTracerContext
*/
static void notifyFinish(IgnoreTracerContext ignoreTracerContext) {
for (IgnoreTracerContextListener listener : LISTENERS) {
listener.afterFinished(ignoreTracerContext);
}
}
/**
* Clear the given {@link IgnoreTracerContextListener}
*/
public static synchronized void remove(IgnoreTracerContextListener listener) {
LISTENERS.remove(listener);
}
}
}

View File

@ -0,0 +1,9 @@
package org.skywalking.apm.agent.core.context;
/**
* @author wusheng
*/
public interface IgnoreTracerContextListener {
void afterFinished(IgnoreTracerContext traceSegment);
}

View File

@ -126,6 +126,12 @@ public final class TracerContext implements AbstractTracerContext {
}
}
@Override
public void dispose() {
this.segment = null;
this.activeSpanStack = null;
}
/**
* Finish this context, and notify all {@link TracerContextListener}s, managed by {@link ListenerManager}
*/

View File

@ -14,6 +14,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.context.ContextManager;
import org.skywalking.apm.agent.core.context.IgnoreTracerContext;
import org.skywalking.apm.agent.core.context.tag.BooleanTagItem;
import org.skywalking.apm.agent.core.context.tag.IntTagItem;
import org.skywalking.apm.agent.core.context.tag.StringTagItem;
@ -199,7 +201,7 @@ public class Span {
if (operationName != null) {
int suffixIdx = operationName.lastIndexOf(".");
if (suffixIdx > -1 && Config.Agent.IGNORE_SUFFIX.contains(operationName.substring(suffixIdx))) {
ContextManager.ContextSwitcher.INSTANCE.toNew(new IgnoreTracerContext(1));
}
}
}