Skip to content

Commit

Permalink
[pinpoint-apm#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 17, 2022
1 parent f5a472e commit e227461
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 36 deletions.
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> continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace newAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);
Trace newAsyncTraceObject(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 continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace newTraceObject();

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 Down Expand Up @@ -78,7 +80,7 @@ private Trace newAsyncTrace(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.newAsyncTraceObject(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,20 +43,20 @@ public DefaultAsyncTraceContext(Provider<BaseTraceFactory> baseTraceFactoryProvi
}

@Override
public Reference<Trace> continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
public Reference<Trace> continueAsyncTraceObject(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 = baseTraceFactory.continueAsyncTraceObject(traceRoot, localAsyncId, canSampled);

bind(reference, trace);
return reference;
}

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


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 continueAsyncTraceObject(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 @@ -109,7 +109,7 @@ private SpanEvent traceBlockBegin0(final int stackId) {
// Set properties for the case when stackFrame is not used as part of Span.
final SpanEvent spanEvent = newSpanEvent(stackId);
this.callStack.push(spanEvent);
return spanEvent;
return callStack.peek();
}

private void stackDump(String caused) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.scope.DefaultTraceScopePool;

import java.util.Objects;

public class DisableAsyncChildTrace implements Trace {
// private static final int ASYNC_BEGIN_STACK_ID = 1001;
public static final String UNSUPPORTED_OPERATION = "disable async child trace";

private boolean closed = false;

private DefaultTraceScopePool scopePool;

private final TraceRoot traceRoot;
private final LocalAsyncId localAsyncId;

public DisableAsyncChildTrace(final TraceRoot traceRoot, final LocalAsyncId localAsyncId) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.localAsyncId = Objects.requireNonNull(localAsyncId, "localAsyncId");
}

@Override
public SpanEventRecorder traceBlockBegin() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}

@Override
public SpanEventRecorder traceBlockBegin(int stackId) {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}

@Override
public void traceBlockEnd() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}

@Override
public void traceBlockEnd(int stackId) {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}

@Override
public boolean isRootStack() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}

@Override
public int getCallStackFrameId() {
return 0;
}

private TraceRoot getTraceRoot() {
return this.traceRoot;
}

@Override
public long getId() {
return getTraceRoot().getLocalTransactionId();
}

@Override
public long getStartTime() {
return getTraceRoot().getTraceStartTime();
}

@Override
public TraceId getTraceId() {
return getTraceRoot().getTraceId();
}

@Override
public boolean canSampled() {
return false;
}

@Override
public boolean isRoot() {
return this.getTraceId().isRoot();
}

@Override
public boolean isAsync() {
return true;
}

@Override
public SpanRecorder getSpanRecorder() {
return null;
}

@Override
public SpanEventRecorder currentSpanEventRecorder() {
return null;
}

@Override
public boolean isClosed() {
return closed;
}

@Override
public void close() {
if (closed) {
return;
}
closed = true;
}

@Override
public TraceScope getScope(String name) {
if (scopePool == null) {
return null;
}
return scopePool.get(name);
}

@Override
public TraceScope addScope(String name) {
if (scopePool == null) {
scopePool = new DefaultTraceScopePool();
}
return scopePool.add(name);
}

@Override
public String toString() {
return "DisableAsyncChildTrace{" +
"traceRoot=" + getTraceRoot() +
", localAsyncId=" + localAsyncId +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public Trace continueAsyncTraceObject(TraceId traceId) {
}

@Override
public Trace continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
public Trace continueAsyncTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
if (logger.isDebugEnabled()) {
logger.debug("continueAsyncTraceObject(traceRoot:{}, localAsyncId:{})", traceRoot, localAsyncId);
logger.debug("continueAsyncTraceObject(traceRoot:{}, localAsyncId:{}, canSampled:{})", traceRoot, localAsyncId, canSampled);
}

return baseTraceFactory.continueAsyncTraceObject(traceRoot, localAsyncId);
return baseTraceFactory.continueAsyncTraceObject(traceRoot, localAsyncId, canSampled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public AsyncId getAsyncIdObject() {
return asyncIdObject;
}

public boolean isDummy() {
return false;
}

@Override
public String toString() {
return "SpanEvent{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ private static class DummySpanEvent extends SpanEvent {
public String toString() {
return "DummySpanEvent";
}

@Override
public boolean isDummy() {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class StatefulAsyncContext extends DefaultAsyncContext implements AsyncSt
private final AsyncState asyncState;


public StatefulAsyncContext(AsyncTraceContext asyncTraceContext, TraceRoot traceRoot, AsyncId asyncId, int asyncMethodApiId, AsyncState asyncState) {
super(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId);
public StatefulAsyncContext(AsyncTraceContext asyncTraceContext, TraceRoot traceRoot, AsyncId asyncId, int asyncMethodApiId, AsyncState asyncState, boolean canSampled) {
super(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, canSampled);
this.asyncState = Objects.requireNonNull(asyncState, "asyncState");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public AsyncContext recordNextAsyncContext(boolean asyncStateSupport) {
if (asyncStateSupport) {
final TraceRoot traceRoot = this.traceRoot;
final AsyncId asyncIdObject = getNextAsyncId();
final boolean canSampled = canSampleNextAsyncContext();

final AsyncState asyncState = this.asyncState;
asyncState.setup();
final AsyncContext asyncContext = asyncContextFactory.newAsyncContext(traceRoot, asyncIdObject, asyncState);
final AsyncContext asyncContext = asyncContextFactory.newAsyncContext(traceRoot, asyncIdObject, canSampled, asyncState);
return asyncContext;
}
return recordNextAsyncContext();
Expand Down
Loading

0 comments on commit e227461

Please sign in to comment.