From bbfd37776c0b4b66350128435fe61978371ec767 Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 6 Jan 2023 18:50:33 +0900 Subject: [PATCH] [#9575] ... --- .../profiler/context/AsyncChildTrace.java | 8 +- .../profiler/context/AsyncContextFactory.java | 4 + .../profiler/context/AsyncTraceContext.java | 3 + .../profiler/context/BaseTraceFactory.java | 5 +- .../context/DefaultAsyncContextFactory.java | 10 + .../context/DefaultAsyncTraceContext.java | 22 +- .../context/DefaultBaseTraceFactory.java | 34 ++-- .../context/DisableAsyncChildTrace.java | 49 +++-- .../profiler/context/DisableAsyncContext.java | 41 ++++ .../profiler/context/DisableAsyncState.java | 23 +++ .../profiler/context/DisableTrace.java | 34 +++- .../context/LoggingBaseTraceFactory.java | 21 +- .../context/StatefulAsyncContext.java | 4 +- .../context/id/LocalTraceRootImpl.java | 6 +- .../context/id/RemoteTraceRootImpl.java | 4 +- .../recorder/DefaultRecorderFactory.java | 8 + .../context/recorder/DefaultSpanRecorder.java | 2 - .../recorder/DisableSpanEventRecorder.java | 189 ++++++++++++++++++ .../context/recorder/DisableSpanRecorder.java | 9 +- .../context/recorder/RecorderFactory.java | 2 + .../profiler/context/AsyncContextTest.java | 5 +- .../context/DefaultAsyncTraceContextTest.java | 5 +- .../profiler/context/id/LocalTraceTest.java | 11 +- .../recorder/DefaultSpanRecorderTest.java | 4 +- 24 files changed, 431 insertions(+), 72 deletions(-) create mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncContext.java create mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncState.java create mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanEventRecorder.java diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncChildTrace.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncChildTrace.java index 6870177cde721..4eecfd5c8de9f 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncChildTrace.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncChildTrace.java @@ -52,8 +52,12 @@ public class AsyncChildTrace implements Trace { private final TraceRoot traceRoot; private final LocalAsyncId localAsyncId; - public AsyncChildTrace(final TraceRoot traceRoot, CallStack callStack, Storage storage, - SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder, final LocalAsyncId localAsyncId) { + public AsyncChildTrace(final TraceRoot traceRoot, + CallStack callStack, + Storage storage, + SpanRecorder spanRecorder, + WrappedSpanEventRecorder wrappedSpanEventRecorder, + final LocalAsyncId localAsyncId) { this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot"); this.callStack = Objects.requireNonNull(callStack, "callStack"); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncContextFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncContextFactory.java index 29debce17586c..faaaf4798d0d5 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncContextFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncContextFactory.java @@ -18,6 +18,7 @@ import com.navercorp.pinpoint.bootstrap.context.AsyncContext; import com.navercorp.pinpoint.bootstrap.context.AsyncState; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; /** @@ -31,4 +32,7 @@ public interface AsyncContextFactory { AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled, AsyncState asyncState); + AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot); + + AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot, AsyncState asyncState); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncTraceContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncTraceContext.java index 670099402bab6..b682d7c9f1335 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncTraceContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncTraceContext.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.profiler.context; import com.navercorp.pinpoint.bootstrap.context.Trace; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; @@ -31,6 +32,8 @@ public interface AsyncTraceContext { Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled); + Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot); + Reference currentRawTraceObject(); Reference currentTraceObject(); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/BaseTraceFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/BaseTraceFactory.java index 8ffd60fe84c59..d64edc4cce8a0 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/BaseTraceFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/BaseTraceFactory.java @@ -19,6 +19,7 @@ import com.navercorp.pinpoint.bootstrap.context.Trace; import com.navercorp.pinpoint.bootstrap.context.TraceId; import com.navercorp.pinpoint.common.annotations.InterfaceAudience; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; /** @@ -34,7 +35,9 @@ public interface BaseTraceFactory { @InterfaceAudience.LimitedPrivate("vert.x") Trace continueAsyncTraceObject(TraceId traceId); - Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled); + Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId); + + Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot); Trace newTraceObject(); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncContextFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncContextFactory.java index 22fdcbb113754..7a2d4654efdee 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncContextFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncContextFactory.java @@ -20,6 +20,7 @@ import com.navercorp.pinpoint.bootstrap.context.AsyncState; import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; import com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; import com.navercorp.pinpoint.profiler.context.method.PredefinedMethodDescriptorRegistry; @@ -71,5 +72,14 @@ public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolea return new StatefulAsyncContext(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, asyncState, canSampled); } + @Override + public AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot) { + return new DisableAsyncContext(traceRoot, asyncTraceContext); + } + + @Override + public AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot, AsyncState asyncState) { + return new DisableAsyncContext(traceRoot, asyncTraceContext, asyncState); + } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContext.java index a1a6c44c4d763..b01fd5c2dcd33 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContext.java @@ -18,11 +18,13 @@ import com.google.inject.Provider; import com.navercorp.pinpoint.bootstrap.context.Trace; -import java.util.Objects; import com.navercorp.pinpoint.exception.PinpointException; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Objects; /** @@ -54,8 +56,22 @@ public Reference continueAsyncContextTraceObject(TraceRoot traceRoot, Loc @Override public Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) { + if (canSampled) { + return continueAsyncContextTraceObject(traceRoot, localAsyncId); + } else { + return continueDisableAsyncContextTraceObject(traceRoot); + } + } + + public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) { + final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get(); + return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId); + } + + @Override + public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) { final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get(); - return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId, canSampled); + return baseTraceFactory.continueDisableAsyncContextTraceObject(traceRoot); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java index 6f6fed786b888..f96f32e2539f4 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.profiler.context; import com.navercorp.pinpoint.bootstrap.context.AsyncState; +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; @@ -146,21 +147,25 @@ Trace newTraceObject(TraceSampler.State state) { // internal async trace. @Override - public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean sampling) { - if (sampling) { - final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId); - final Storage storage = storageFactory.createStorage(spanChunkFactory); + public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) { + final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId); + final Storage storage = storageFactory.createStorage(spanChunkFactory); - final CallStack callStack = callStackFactory.newCallStack(); + final CallStack callStack = callStackFactory.newCallStack(); - final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot); + final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot); - final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot); + final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot); - return new AsyncChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId); - } else { - return new DisableAsyncChildTrace(traceRoot, localAsyncId); - } + return new AsyncChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId); + } + + @Override + public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) { + final AsyncState asyncState = new DisableAsyncState(); + SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot); + SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, asyncState); + return new DisableAsyncChildTrace(traceRoot, spanRecorder, spanEventRecorder); } @@ -178,7 +183,7 @@ public Trace continueAsyncTraceObject(final TraceId traceId) { final CallStack callStack = callStackFactory.newCallStack(); final ActiveTraceHandle handle = registerActiveTrace(traceRoot); - final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory); + final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory); final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage); final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot()); @@ -217,7 +222,7 @@ Trace newAsyncTraceObject(TraceSampler.State state) { final CallStack callStack = callStackFactory.newCallStack(); final ActiveTraceHandle handle = registerActiveTrace(traceRoot); - final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory); + final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory); final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage); @@ -244,9 +249,10 @@ public Trace disableSampling() { private Trace newLocalTrace(long nextDisabledId) { final LocalTraceRoot traceRoot = traceRootFactory.newDisableTraceRoot(nextDisabledId); final SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot); + final SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, new DisableAsyncState()); final long traceStartTime = traceRoot.getTraceStartTime(); final long threadId = Thread.currentThread().getId(); final ActiveTraceHandle activeTraceHandle = registerActiveTrace(nextDisabledId, traceStartTime, threadId); - return new DisableTrace(traceRoot, spanRecorder, activeTraceHandle, uriStatStorage); + return new DisableTrace(traceRoot, spanRecorder, spanEventRecorder, activeTraceHandle, uriStatStorage); } } \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncChildTrace.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncChildTrace.java index c814c0de7205e..e24b4f6dea395 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncChildTrace.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncChildTrace.java @@ -34,41 +34,59 @@ public class DisableAsyncChildTrace implements Trace { private DefaultTraceScopePool scopePool; private final LocalTraceRoot traceRoot; - private final LocalAsyncId localAsyncId; + private int depth; - public DisableAsyncChildTrace(final LocalTraceRoot traceRoot, final LocalAsyncId localAsyncId) { + private final SpanRecorder spanRecorder; + private final SpanEventRecorder spanEventRecorder; + + public DisableAsyncChildTrace(final LocalTraceRoot traceRoot, SpanRecorder spanRecorder, SpanEventRecorder spanEventRecorder) { this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot"); - this.localAsyncId = Objects.requireNonNull(localAsyncId, "localAsyncId"); + this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder"); + this.spanEventRecorder = Objects.requireNonNull(spanEventRecorder, "spanEventRecorder"); } @Override public SpanEventRecorder traceBlockBegin() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + return traceBlockBegin(DEFAULT_STACKID); } @Override public SpanEventRecorder traceBlockBegin(int stackId) { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + push(); + return getSpanEventRecorder(); } @Override public void traceBlockEnd() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + traceBlockBegin(DEFAULT_STACKID); } + @Override public void traceBlockEnd(int stackId) { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + pop(); + } + + private SpanEventRecorder getSpanEventRecorder() { + return spanEventRecorder; + } + + private int push() { + return this.depth++; + } + + private void pop() { + this.depth--; } @Override public boolean isRootStack() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + return depth == 0; } @Override public int getCallStackFrameId() { - return 0; + return DEFAULT_STACKID; } private LocalTraceRoot getTraceRoot() { @@ -82,7 +100,7 @@ public long getId() { @Override public long getStartTime() { - return getTraceRoot().getTraceStartTime(); + return traceRoot.getTraceStartTime(); } @Override @@ -97,7 +115,7 @@ public boolean canSampled() { @Override public boolean isRoot() { - return this.getTraceId().isRoot(); + return false; } @Override @@ -107,12 +125,12 @@ public boolean isAsync() { @Override public SpanRecorder getSpanRecorder() { - return null; + return spanRecorder; } @Override public SpanEventRecorder currentSpanEventRecorder() { - return null; + return spanEventRecorder; } @Override @@ -148,8 +166,7 @@ public TraceScope addScope(String name) { @Override public String toString() { return "DisableAsyncChildTrace{" + - "traceRoot=" + getTraceRoot() + - ", localAsyncId=" + localAsyncId + - '}'; + "traceRoot=" + traceRoot + + '}'; } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncContext.java new file mode 100644 index 0000000000000..991252a1ae4ca --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncContext.java @@ -0,0 +1,41 @@ +package com.navercorp.pinpoint.profiler.context; + +import com.navercorp.pinpoint.bootstrap.context.AsyncContext; +import com.navercorp.pinpoint.bootstrap.context.Trace; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; + +import java.util.Objects; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DisableAsyncContext implements AsyncContext { + private final LocalTraceRoot traceRoot; + private final AsyncTraceContext asyncTraceContext; + + public DisableAsyncContext(LocalTraceRoot traceRoot, AsyncTraceContext asyncTraceContext) { + this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot"); + this.asyncTraceContext = Objects.requireNonNull(asyncTraceContext, "asyncTraceContext"); + } + + @Override + public Trace continueAsyncTraceObject() { + final Reference reference = asyncTraceContext.currentRawTraceObject(); + final Trace nestedTrace = reference.get(); + if (nestedTrace != null) { + return nestedTrace; + } + return asyncTraceContext.continueDisableAsyncContextTraceObject(traceRoot); + } + + @Override + public Trace currentAsyncTraceObject() { + final Reference reference = asyncTraceContext.currentRawTraceObject(); + return reference.get(); + } + + @Override + public void close() { + asyncTraceContext.removeTraceObject(); + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncState.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncState.java new file mode 100644 index 0000000000000..f01d3d0008eda --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableAsyncState.java @@ -0,0 +1,23 @@ +package com.navercorp.pinpoint.profiler.context; + +import com.navercorp.pinpoint.bootstrap.context.AsyncState; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DisableAsyncState implements AsyncState { + @Override + public void setup() { + + } + + @Override + public boolean await() { + return false; + } + + @Override + public void finish() { + + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableTrace.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableTrace.java index 7261b81afdd20..182bc25f0d73a 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableTrace.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DisableTrace.java @@ -36,7 +36,7 @@ */ public class DisableTrace implements Trace { - public static final String UNSUPPORTED_OPERATION = "disable trace"; + public static final String UNSUPPORTED_OPERATION = "disable trace"; public static final long DISABLE_TRACE_OBJECT_ID = -1; private final LocalTraceRoot traceRoot; @@ -45,11 +45,16 @@ public class DisableTrace implements Trace { private final ActiveTraceHandle handle; private final UriStatStorage uriStatStorage; + private int depth; + + private SpanEventRecorder spanEventRecorder; + private boolean closed = false; - public DisableTrace(LocalTraceRoot traceRoot, SpanRecorder spanRecorder, ActiveTraceHandle handle, UriStatStorage uriStatStorage) { + public DisableTrace(LocalTraceRoot traceRoot, SpanRecorder spanRecorder, SpanEventRecorder spanEventRecorder, ActiveTraceHandle handle, UriStatStorage uriStatStorage) { this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot"); this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder"); + this.spanEventRecorder = Objects.requireNonNull(spanEventRecorder, "spanEventRecorder"); this.handle = Objects.requireNonNull(handle, "handle"); this.uriStatStorage = Objects.requireNonNull(uriStatStorage, "uriStatStorage"); @@ -68,24 +73,34 @@ public long getStartTime() { @Override public SpanEventRecorder traceBlockBegin() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + return traceBlockBegin(DEFAULT_STACKID); } @Override public SpanEventRecorder traceBlockBegin(int stackId) { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + push(); + return this.spanEventRecorder; } @Override public void traceBlockEnd() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + traceBlockBegin(DEFAULT_STACKID); } @Override public void traceBlockEnd(int stackId) { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + pop(); } + private int push() { + return this.depth++; + } + + private void pop() { + this.depth--; + } + + @Override public TraceId getTraceId() { throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); @@ -109,7 +124,7 @@ public boolean isAsync() { @Override public boolean isRootStack() { - throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + return depth == 0; } @@ -144,7 +159,7 @@ private Shared getShared() { @Override public int getCallStackFrameId() { - return 0; + return DEFAULT_STACKID; } @Override @@ -154,9 +169,10 @@ public SpanRecorder getSpanRecorder() { @Override public SpanEventRecorder currentSpanEventRecorder() { - return null; + return spanEventRecorder; } + @Override public TraceScope getScope(String name) { if (scopePool == null) { diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/LoggingBaseTraceFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/LoggingBaseTraceFactory.java index 8abb3d1d2418a..c3e3cec1f1cd7 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/LoggingBaseTraceFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/LoggingBaseTraceFactory.java @@ -19,10 +19,12 @@ import com.navercorp.pinpoint.bootstrap.context.Trace; import com.navercorp.pinpoint.bootstrap.context.TraceId; import com.navercorp.pinpoint.common.annotations.InterfaceAudience; -import java.util.Objects; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Objects; /** * @author Woonduk Kang(emeroad) @@ -72,12 +74,21 @@ public Trace continueAsyncTraceObject(TraceId traceId) { } @Override - public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) { + public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) { + if (logger.isDebugEnabled()) { + logger.debug("continueAsyncTraceObject(traceRoot:{}, localAsyncId:{})", traceRoot, localAsyncId); + } + + return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId); + } + + @Override + public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) { if (logger.isDebugEnabled()) { - logger.debug("continueAsyncTraceObject(traceRoot:{}, localAsyncId:{}, canSampled:{})", traceRoot, localAsyncId, canSampled); + logger.debug("continueDisableAsyncContextTraceObject(traceRoot:{})", traceRoot); } - return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId, canSampled); + return baseTraceFactory.continueDisableAsyncContextTraceObject(traceRoot); } @Override diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/StatefulAsyncContext.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/StatefulAsyncContext.java index 14c2ecc8666cc..0bad8a5a13a9c 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/StatefulAsyncContext.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/StatefulAsyncContext.java @@ -18,15 +18,15 @@ import com.navercorp.pinpoint.bootstrap.context.AsyncState; import com.navercorp.pinpoint.bootstrap.context.AsyncStateSupport; -import java.util.Objects; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; +import java.util.Objects; + /** * @author Woonduk Kang(emeroad) */ public class StatefulAsyncContext extends DefaultAsyncContext implements AsyncStateSupport { - private final AsyncState asyncState; diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceRootImpl.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceRootImpl.java index d7dd0c390d89e..322fa560db6be 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceRootImpl.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceRootImpl.java @@ -44,9 +44,11 @@ public Shared getShared() { @Override public String toString() { - return "DefaultLocalTraceRoot{" + - ", agentId='" + agentId + '\'' + + return "LocalTraceRootImpl{" + + "agentId='" + agentId + '\'' + + ", localTransactionId=" + localTransactionId + ", traceStartTime=" + traceStartTime + + ", shared=" + shared + '}'; } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/RemoteTraceRootImpl.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/RemoteTraceRootImpl.java index 04487fe5685c1..aac3050cf35d1 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/RemoteTraceRootImpl.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/RemoteTraceRootImpl.java @@ -39,10 +39,12 @@ public TraceId getTraceId() { @Override public String toString() { - return "DefaultRemoteTraceRoot{" + + return "RemoteTraceRootImpl{" + "traceId=" + traceId + ", agentId='" + agentId + '\'' + + ", localTransactionId=" + localTransactionId + ", traceStartTime=" + traceStartTime + + ", shared=" + shared + '}'; } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultRecorderFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultRecorderFactory.java index d2c839ca547df..0aa8a469a73e5 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultRecorderFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultRecorderFactory.java @@ -77,4 +77,12 @@ public WrappedSpanEventRecorder newWrappedSpanEventRecorder(TraceRoot traceRoot, final AsyncContextFactory asyncContextFactory = asyncContextFactoryProvider.get(); return new WrappedAsyncSpanEventRecorder(traceRoot, asyncContextFactory, stringMetaDataService, sqlMetaDataService, errorHandler, asyncState); } + + @Override + public DisableSpanEventRecorder newDisableSpanEventRecorder(LocalTraceRoot traceRoot, AsyncState asyncState) { + Objects.requireNonNull(asyncState, "asyncState"); + + final AsyncContextFactory asyncContextFactory = asyncContextFactoryProvider.get(); + return new DisableSpanEventRecorder(traceRoot, asyncContextFactory, asyncState); + } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorder.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorder.java index 7aa52461ea709..6aac858897b18 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorder.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorder.java @@ -29,9 +29,7 @@ import org.apache.logging.log4j.Logger; /** - * * @author jaehong.kim - * */ public class DefaultSpanRecorder extends AbstractRecorder implements SpanRecorder { private static final Logger logger = LogManager.getLogger(DefaultTrace.class.getName()); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanEventRecorder.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanEventRecorder.java new file mode 100644 index 0000000000000..65950713c7c93 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanEventRecorder.java @@ -0,0 +1,189 @@ +package com.navercorp.pinpoint.profiler.context.recorder; + +import com.navercorp.pinpoint.bootstrap.context.AsyncContext; +import com.navercorp.pinpoint.bootstrap.context.AsyncState; +import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor; +import com.navercorp.pinpoint.bootstrap.context.ParsingResult; +import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder; +import com.navercorp.pinpoint.common.trace.AnnotationKey; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.common.util.DataType; +import com.navercorp.pinpoint.profiler.context.AsyncContextFactory; +import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot; + +import java.util.Objects; + +/** + * @author Woonduk Kang(emeroad) + */ +public class DisableSpanEventRecorder implements SpanEventRecorder { + + public static final String UNSUPPORTED_OPERATION = "DisableSpanEventRecorder"; + private final LocalTraceRoot traceRoot; + private final AsyncContextFactory asyncContextFactory; + private final AsyncState asyncState; + + public DisableSpanEventRecorder(LocalTraceRoot traceRoot, + AsyncContextFactory asyncContextFactory, AsyncState asyncState) { + this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot"); + this.asyncContextFactory = Objects.requireNonNull(asyncContextFactory, "asyncContextFactory"); + this.asyncState = Objects.requireNonNull(asyncState, "asyncState"); + } + + @Override + public void recordTime(boolean time) { + + } + + @Override + public void recordException(Throwable throwable) { + + } + + @Override + public void recordException(boolean markError, Throwable throwable) { + + } + + @Override + public void recordApiId(int apiId) { + + } + + @Override + public void recordApi(MethodDescriptor methodDescriptor) { + + } + + @Override + public void recordApi(MethodDescriptor methodDescriptor, Object[] args) { + + } + + @Override + public void recordApi(MethodDescriptor methodDescriptor, Object args, int index) { + + } + + @Override + public void recordApi(MethodDescriptor methodDescriptor, Object[] args, int start, int end) { + + } + + @Override + public void recordApiCachedString(MethodDescriptor methodDescriptor, String args, int index) { + + } + + @Override + public ParsingResult recordSqlInfo(String sql) { + return null; + } + + @Override + public void recordSqlParsingResult(ParsingResult parsingResult) { + + } + + @Override + public void recordSqlParsingResult(ParsingResult parsingResult, String bindValue) { + + } + + @Override + public void recordServiceType(ServiceType serviceType) { + + } + + @Override + public void recordDestinationId(String destinationId) { + + } + + @Override + public void recordEndPoint(String endPoint) { + + } + + @Override + public void recordNextSpanId(long spanId) { + + } + + @Override + public AsyncContext recordNextAsyncContext() { + return asyncContextFactory.newDisableAsyncContext(this.traceRoot); + } + + @Override + public AsyncContext recordNextAsyncContext(boolean asyncStateSupport) { + return asyncContextFactory.newDisableAsyncContext(this.traceRoot, asyncState); + } + + @Override + public void recordAttribute(AnnotationKey key, String value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, int value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, Integer value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, long value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, Long value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, boolean value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, double value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, byte[] value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, DataType value) { + + } + + @Override + public void recordAttribute(AnnotationKey key, Object value) { + + } + + @Override + public Object attachFrameObject(Object frameObject) { + return null; + } + + @Override + public Object getFrameObject() { + throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + } + + @Override + public Object detachFrameObject() { + throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); + } + + +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanRecorder.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanRecorder.java index f8c34ac5d60dc..8e2280d91e7d2 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanRecorder.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/DisableSpanRecorder.java @@ -32,6 +32,9 @@ * @author Woonduk Kang(emeroad) */ public class DisableSpanRecorder implements SpanRecorder { + + public static final String UNSUPPORTED_OPERATION = "DisableSpanRecorder"; + private final LocalTraceRoot traceRoot; private final IgnoreErrorHandler ignoreErrorHandler; @@ -213,16 +216,16 @@ private Shared getShared() { @Override public Object attachFrameObject(Object frameObject) { - return null; + throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); } @Override public Object getFrameObject() { - return null; + throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); } @Override public Object detachFrameObject() { - return null; + throw new UnsupportedOperationException(UNSUPPORTED_OPERATION); } } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/RecorderFactory.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/RecorderFactory.java index 530aad72b65d5..23ba8bb4ef4bf 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/RecorderFactory.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/recorder/RecorderFactory.java @@ -36,4 +36,6 @@ public interface RecorderFactory { WrappedSpanEventRecorder newWrappedSpanEventRecorder(TraceRoot traceRoot); WrappedSpanEventRecorder newWrappedSpanEventRecorder(TraceRoot traceRoot, AsyncState asyncState); + + DisableSpanEventRecorder newDisableSpanEventRecorder(LocalTraceRoot traceRoot, AsyncState asyncState); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/AsyncContextTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/AsyncContextTest.java index f5e84060cf107..ab6240ee7ed7c 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/AsyncContextTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/AsyncContextTest.java @@ -19,7 +19,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -36,7 +35,7 @@ protected AsyncTraceContext newAsyncTraceContext() { BaseTraceFactory baseTraceFactory = mock(DefaultBaseTraceFactory.class); BaseTraceFactoryProvider baseTraceFactoryProvider = mock(BaseTraceFactoryProvider.class); - when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class), eq(true))) + when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class))) .thenAnswer(new Answer() { @Override public Trace answer(InvocationOnMock invocationOnMock) { @@ -45,7 +44,7 @@ public Trace answer(InvocationOnMock invocationOnMock) { return trace; } }); - when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class), eq(false))) + when(baseTraceFactory.continueDisableAsyncContextTraceObject(any(TraceRoot.class))) .thenAnswer(new Answer() { @Override public Trace answer(InvocationOnMock invocationOnMock) { diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContextTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContextTest.java index 89f6360da8d72..0e696130f3b31 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContextTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultAsyncTraceContextTest.java @@ -18,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -35,7 +34,7 @@ protected AsyncTraceContext newAsyncTraceContext() { BaseTraceFactory baseTraceFactory = mock(DefaultBaseTraceFactory.class); BaseTraceFactoryProvider baseTraceFactoryProvider = mock(BaseTraceFactoryProvider.class); - when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class), eq(true))) + when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class))) .thenAnswer(new Answer() { @Override public Trace answer(InvocationOnMock invocationOnMock) { @@ -44,7 +43,7 @@ public Trace answer(InvocationOnMock invocationOnMock) { return trace; } }); - when(baseTraceFactory.continueAsyncContextTraceObject(any(TraceRoot.class), any(LocalAsyncId.class), eq(false))) + when(baseTraceFactory.continueDisableAsyncContextTraceObject(any(TraceRoot.class))) .thenAnswer(new Answer() { @Override public Trace answer(InvocationOnMock invocationOnMock) { diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceTest.java index aca552faf47c7..353037b53bfee 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/id/LocalTraceTest.java @@ -5,8 +5,9 @@ import com.navercorp.pinpoint.bootstrap.context.Trace; import com.navercorp.pinpoint.profiler.context.DisableAsyncChildTrace; import com.navercorp.pinpoint.profiler.context.DisableTrace; -import com.navercorp.pinpoint.profiler.context.LocalAsyncId; import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle; +import com.navercorp.pinpoint.profiler.context.recorder.DisableSpanEventRecorder; +import com.navercorp.pinpoint.profiler.context.recorder.DisableSpanRecorder; import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -73,11 +74,13 @@ private Trace newTrace(LocalTraceRoot traceRoot) { ActiveTraceHandle activeTraceHandle = mock(ActiveTraceHandle.class); UriStatStorage uriStatStorage = mock(UriStatStorage.class); SpanRecorder spanRecorder = mock(SpanRecorder.class); - return new DisableTrace(traceRoot, spanRecorder, activeTraceHandle, uriStatStorage); + SpanEventRecorder spanEventRecorder = mock(DisableSpanEventRecorder.class); + return new DisableTrace(traceRoot, spanRecorder, spanEventRecorder, activeTraceHandle, uriStatStorage); } private Trace newChildTrace(LocalTraceRoot traceRoot) { - LocalAsyncId localAsyncId = mock(LocalAsyncId.class); - return new DisableAsyncChildTrace(traceRoot, localAsyncId); + SpanRecorder spanRecorder = mock(DisableSpanRecorder.class); + SpanEventRecorder spanEventRecorder = mock(DisableSpanEventRecorder.class); + return new DisableAsyncChildTrace(traceRoot, spanRecorder, spanEventRecorder); } } \ No newline at end of file diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorderTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorderTest.java index 71d9759f4d7e3..4bd5c84429fb3 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorderTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/recorder/DefaultSpanRecorderTest.java @@ -54,7 +54,7 @@ public class DefaultSpanRecorderTest { private final IgnoreErrorHandler errorHandler = new BypassErrorHandler(); @Test - public void testRecordApiId() { + public void testRecordApiId() throws Exception { Span span = new Span(traceRoot); SpanRecorder recorder = new DefaultSpanRecorder(span, true, stringMetaDataService, sqlMetaDataService, errorHandler); @@ -66,7 +66,7 @@ public void testRecordApiId() { } @Test - public void testRecordEndPoint() { + public void testRecordEndPoint() throws Exception { when(traceRoot.getShared()).thenReturn(shared);