Skip to content

Commit

Permalink
[#8642] Disable async trace to limit span event
Browse files Browse the repository at this point in the history
  • Loading branch information
kangji authored and emeroad committed Feb 18, 2022
1 parent 7c85321 commit 1a501e8
Show file tree
Hide file tree
Showing 25 changed files with 525 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ private SpanEvent newSpanEvent(int stackId) {

@VisibleForTesting
SpanEvent dummySpanEvent() {
return callStack.getFactory().dummyInstance();
return callStack.getFactory().disableInstance();
}

@VisibleForTesting
boolean isDummySpanEvent(final SpanEvent spanEvent) {
return callStack.getFactory().isDummy(spanEvent);
return callStack.getFactory().isDisable(spanEvent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface AsyncContextFactory {

AsyncId newAsyncId();

AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId);
AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled);

AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, AsyncState asyncState);
AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled, AsyncState asyncState);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public interface AsyncTraceContext {

// Reference<Trace> continueAsyncTraceObject(TraceRoot traceRoot, int asyncId, short asyncSequence);

Reference<Trace> continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);
Reference<Trace> continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace newAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);
Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Reference<Trace> currentRawTraceObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface BaseTraceFactory {
@InterfaceAudience.LimitedPrivate("vert.x")
Trace continueAsyncTraceObject(TraceId traceId);

Trace continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);
Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace newTraceObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ interface Factory<T> {

T newInstance();

T dummyInstance();
T disableInstance();

boolean isDummy(T element);
boolean isDisable(T element);

void markDepth(T element, int index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ public class DefaultAsyncContext implements AsyncContext {

private final TraceRoot traceRoot;
private final AsyncId asyncId;
private final boolean canSampled;

private final AsyncTraceContext asyncTraceContext;

private final int asyncMethodApiId;


public DefaultAsyncContext(AsyncTraceContext asyncTraceContext, TraceRoot traceRoot, AsyncId asyncId, int asyncMethodApiId) {
public DefaultAsyncContext(AsyncTraceContext asyncTraceContext, TraceRoot traceRoot, AsyncId asyncId, int asyncMethodApiId, boolean canSampled) {
this.asyncTraceContext = Objects.requireNonNull(asyncTraceContext, "asyncTraceContext");
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.asyncId = Objects.requireNonNull(asyncId, "asyncId");


this.asyncMethodApiId = asyncMethodApiId;
this.canSampled = canSampled;
}


Expand All @@ -71,14 +73,14 @@ public Trace continueAsyncTraceObject() {
return null;
}

return newAsyncTrace(reference);
return newAsyncContextTrace(reference);
}

private Trace newAsyncTrace(Reference<Trace> reference) {
private Trace newAsyncContextTrace(Reference<Trace> reference) {
// final int asyncId = this.asyncId.getAsyncId();
// final short asyncSequence = this.asyncId.nextAsyncSequence();
final LocalAsyncId localAsyncId = this.asyncId.nextLocalAsyncId();
final Trace asyncTrace = asyncTraceContext.newAsyncTraceObject(traceRoot, localAsyncId);
final Trace asyncTrace = asyncTraceContext.newAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);


bind(reference, asyncTrace);
Expand All @@ -104,10 +106,16 @@ private Trace newAsyncTrace(Reference<Trace> reference) {

// first block.
final SpanEventRecorder recorder = asyncTrace.currentSpanEventRecorder();
recorder.recordServiceType(ServiceType.ASYNC);
recorder.recordApiId(asyncMethodApiId);

return asyncTrace;
if (recorder != null) {
recorder.recordServiceType(ServiceType.ASYNC);
recorder.recordApiId(asyncMethodApiId);
}

if (asyncTrace.canSampled()) {
return asyncTrace;
}
return null;
}

private void bind(Reference<Trace> reference, Trace asyncTrace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ public AsyncId newAsyncId() {
}

@Override
public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId) {
public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled) {
Objects.requireNonNull(traceRoot, "traceRoot");
Objects.requireNonNull(asyncId, "asyncId");

return new DefaultAsyncContext(asyncTraceContext, traceRoot, asyncId, this.asyncMethodApiId);
return new DefaultAsyncContext(asyncTraceContext, traceRoot, asyncId, this.asyncMethodApiId, canSampled);
}

@Override
public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, AsyncState asyncState) {
public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled, AsyncState asyncState) {
Objects.requireNonNull(traceRoot, "traceRoot");
Objects.requireNonNull(asyncId, "asyncId");
Objects.requireNonNull(asyncState, "asyncState");

return new StatefulAsyncContext(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, asyncState);
return new StatefulAsyncContext(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, asyncState, canSampled);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,25 @@ public DefaultAsyncTraceContext(Provider<BaseTraceFactory> baseTraceFactoryProvi
}

@Override
public Reference<Trace> continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
public Reference<Trace> continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
final Reference<Trace> reference = checkAndGet();

final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
final Trace trace = baseTraceFactory.continueAsyncTraceObject(traceRoot, localAsyncId);
final Trace trace = newAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);

bind(reference, trace);
return reference;
}

@Override
public Trace newAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
public Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
return baseTraceFactory.continueAsyncTraceObject(traceRoot, localAsyncId);
return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);
}


@Override
public Reference<Trace> currentRawTraceObject() {
final Reference<Trace> reference = binder.get();
return reference;
return binder.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,24 @@ public Trace newTraceObject() {

// internal async trace.
@Override
public Trace continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {

final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
if (canSampled) {
final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId);
final Storage storage = storageFactory.createStorage(spanChunkFactory);

final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();

final boolean samplingEnable = true;
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot, samplingEnable);
final boolean samplingEnable = true;
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot, samplingEnable);

final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);

final Trace asyncTrace = new AsyncChildTrace(traceRoot, callStack, storage, samplingEnable, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
final Trace asyncTrace = new AsyncChildTrace(traceRoot, callStack, storage, samplingEnable, spanRecorder, wrappedSpanEventRecorder, localAsyncId);

return asyncTrace;
return asyncTrace;
} else {
return new DisableAsyncChildTrace(traceRoot, localAsyncId);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void checkExtend(final int size) {
public T pop() {
if (isOverflow() && overflowIndex > 0) {
overflowIndex--;
return factory.dummyInstance();
return factory.disableInstance();
}

final T spanEvent = peek();
Expand All @@ -123,7 +123,7 @@ public T peek() {
}

if (isOverflow() && overflowIndex > 0) {
return factory.dummyInstance();
return factory.disableInstance();
}
return stack[index - 1];
}
Expand Down Expand Up @@ -168,7 +168,7 @@ private boolean isSequenceOverflow() {
@Override
public T newInstance() {
if (isOverflow()) {
return factory.dummyInstance();
return factory.disableInstance();
} else {
return factory.newInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ private SpanEvent newSpanEvent(int stackId) {

@VisibleForTesting
SpanEvent dummySpanEvent() {
return callStack.getFactory().dummyInstance();
return callStack.getFactory().disableInstance();
}

@VisibleForTesting
boolean isDummySpanEvent(final SpanEvent spanEvent) {
return callStack.getFactory().isDummy(spanEvent);
return callStack.getFactory().isDisable(spanEvent);
}

@Override
Expand Down
Loading

0 comments on commit 1a501e8

Please sign in to comment.