From 44466a874c8addc4e21d73e4c6512532fe4ca208 Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 6 Jan 2023 19:54:27 +0900 Subject: [PATCH] [#9575] Refactor DefaultTrace.close() --- .../profiler/context/AsyncDefaultTrace.java | 2 +- .../profiler/context/CloseListener.java | 12 ++++ .../context/DefaultBaseTraceFactory.java | 10 ++-- .../context/DefaultCloseListener.java | 57 +++++++++++++++++++ .../profiler/context/DefaultTrace.java | 51 +++-------------- .../profiler/context/DefaultTraceTest.java | 5 +- .../pinpoint/profiler/context/TraceTest.java | 13 +---- 7 files changed, 84 insertions(+), 66 deletions(-) create mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/context/CloseListener.java create mode 100644 profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultCloseListener.java diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java index ae808bad66a0..b34db7fa7c64 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java @@ -16,7 +16,7 @@ public AsyncDefaultTrace(Span span, SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder, AsyncState asyncState) { - super(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, null, null); + super(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY); this.asyncState = Objects.requireNonNull(asyncState, "asyncState"); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/CloseListener.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/CloseListener.java new file mode 100644 index 000000000000..64b656a0f085 --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/CloseListener.java @@ -0,0 +1,12 @@ +package com.navercorp.pinpoint.profiler.context; + +public interface CloseListener { + void close(Span span); + + CloseListener EMPTY = new CloseListener() { + @Override + public void close(Span span) { + } + }; + +} 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 2f85ab7e046b..d9dc6800e911 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 @@ -94,9 +94,8 @@ public Trace continueTraceObject(final TraceId traceId) { final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span); final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot); final ActiveTraceHandle handle = registerActiveTrace(traceRoot); - - return new DefaultTrace(span, callStack, storage, spanRecorder, - wrappedSpanEventRecorder, handle, uriStatStorage); + final CloseListener closeListener = new DefaultCloseListener(handle, uriStatStorage); + return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, closeListener); } else { return newLocalTrace(state.nextId()); } @@ -135,9 +134,8 @@ Trace newTraceObject(TraceSampler.State state) { final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot); final ActiveTraceHandle handle = registerActiveTrace(traceRoot); - - return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, - handle, uriStatStorage); + final CloseListener closeListener = new DefaultCloseListener(handle, uriStatStorage); + return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, closeListener); } else { return newLocalTrace(state.nextId()); } diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultCloseListener.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultCloseListener.java new file mode 100644 index 000000000000..ee72ab70858d --- /dev/null +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultCloseListener.java @@ -0,0 +1,57 @@ +package com.navercorp.pinpoint.profiler.context; + +import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle; +import com.navercorp.pinpoint.profiler.context.id.Shared; +import com.navercorp.pinpoint.profiler.context.id.TraceRoot; +import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage; + +import javax.annotation.Nullable; + +public class DefaultCloseListener implements CloseListener { + + @Nullable + private final ActiveTraceHandle activeTraceHandle; + @Nullable + private final UriStatStorage uriStatStorage; + + public DefaultCloseListener(ActiveTraceHandle activeTraceHandle, UriStatStorage uriStatStorage) { + this.activeTraceHandle = activeTraceHandle; + this.uriStatStorage = uriStatStorage; + } + + @Override + public void close(Span span) { + final long end = span.getStartTime() + span.getElapsedTime(); + recordUriTemplate(span, end); + purgeActiveTrace(end); + } + + + private void recordUriTemplate(Span span, long afterTime) { + if (uriStatStorage == null) { + return; + } + + TraceRoot traceRoot = span.getTraceRoot(); + Shared shared = traceRoot.getShared(); + String uriTemplate = shared.getUriTemplate(); + long traceStartTime = traceRoot.getTraceStartTime(); + + boolean status = getStatus(shared.getErrorCode()); + uriStatStorage.store(uriTemplate, status, traceStartTime, afterTime); + } + + private boolean getStatus(int errorCode) { + if (errorCode == 0) { + return true; + } + return false; + } + + private void purgeActiveTrace(long currentTime) { + final ActiveTraceHandle copy = this.activeTraceHandle; + if (copy != null) { + copy.purge(currentTime); + } + } +} diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java index eeb35a390fc5..755a0ccdee70 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java @@ -23,17 +23,13 @@ import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope; import com.navercorp.pinpoint.common.annotations.VisibleForTesting; import com.navercorp.pinpoint.exception.PinpointException; -import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle; -import com.navercorp.pinpoint.profiler.context.id.Shared; import com.navercorp.pinpoint.profiler.context.id.TraceRoot; import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder; import com.navercorp.pinpoint.profiler.context.scope.DefaultTraceScopePool; import com.navercorp.pinpoint.profiler.context.storage.Storage; -import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import javax.annotation.Nullable; import java.util.Objects; /** @@ -58,20 +54,17 @@ public class DefaultTrace implements Trace { private DefaultTraceScopePool scopePool; private final Span span; - @Nullable - private final ActiveTraceHandle activeTraceHandle; - @Nullable - private final UriStatStorage uriStatStorage; + private final CloseListener closeListener; public DefaultTrace(Span span, CallStack callStack, Storage storage, SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder) { - this(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, null, null); + this(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY); } public DefaultTrace(Span span, CallStack callStack, Storage storage, - SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder, - ActiveTraceHandle activeTraceHandle, - UriStatStorage uriStatStorage) { + SpanRecorder spanRecorder, + WrappedSpanEventRecorder wrappedSpanEventRecorder, + CloseListener closeListener) { this.span = Objects.requireNonNull(span, "span"); this.callStack = Objects.requireNonNull(callStack, "callStack"); @@ -80,8 +73,7 @@ public DefaultTrace(Span span, CallStack callStack, Storage storage, this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder"); this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder"); - this.activeTraceHandle = activeTraceHandle; - this.uriStatStorage = uriStatStorage; + this.closeListener = closeListener; setCurrentThread(); } @@ -202,39 +194,10 @@ public void close() { logSpan(span); } - this.storage.close(); - purgeActiveTrace(afterTime); - recordUriTemplate(afterTime); - } - - private void recordUriTemplate(long afterTime) { - if (uriStatStorage == null) { - return; - } - - TraceRoot traceRoot = this.getTraceRoot(); - Shared shared = traceRoot.getShared(); - String uriTemplate = shared.getUriTemplate(); - long traceStartTime = traceRoot.getTraceStartTime(); - - boolean status = getStatus(shared.getErrorCode()); - uriStatStorage.store(uriTemplate, status, traceStartTime, afterTime); + this.closeListener.close(span); } - private boolean getStatus(int errorCode) { - if (errorCode == 0) { - return true; - } - return false; - } - - private void purgeActiveTrace(long currentTime) { - final ActiveTraceHandle copy = this.activeTraceHandle; - if (copy != null) { - copy.purge(currentTime); - } - } void flush() { this.storage.flush(); diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceTest.java index 1426964ae4fe..d0890bc45bb3 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/DefaultTraceTest.java @@ -19,7 +19,6 @@ 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.profiler.context.active.ActiveTraceHandle; import com.navercorp.pinpoint.profiler.context.errorhandler.BypassErrorHandler; import com.navercorp.pinpoint.profiler.context.errorhandler.IgnoreErrorHandler; import com.navercorp.pinpoint.profiler.context.id.Shared; @@ -27,7 +26,6 @@ import com.navercorp.pinpoint.profiler.context.recorder.DefaultSpanRecorder; import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder; import com.navercorp.pinpoint.profiler.context.storage.Storage; -import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage; import com.navercorp.pinpoint.profiler.logging.Log4j2LoggerBinderInitializer; import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService; @@ -156,12 +154,11 @@ private Trace newTrace(final int maxCallStackDepth) { SpanFactory spanFactory = new DefaultSpanFactory(); Storage storage = mock(Storage.class); - UriStatStorage uriStatStorage = mock(UriStatStorage.class); final Span span = spanFactory.newSpan(traceRoot); final SpanRecorder spanRecorder = new DefaultSpanRecorder(span, stringMetaDataService, sqlMetaDataService, errorHandler); final WrappedSpanEventRecorder wrappedSpanEventRecorder = new WrappedSpanEventRecorder(traceRoot, asyncContextFactory, stringMetaDataService, sqlMetaDataService, errorHandler); - return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, ActiveTraceHandle.EMPTY_HANDLE, uriStatStorage); + return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY); } } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TraceTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TraceTest.java index 2c7b5ddd0c19..7ba2ce1ea0b6 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TraceTest.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/TraceTest.java @@ -19,7 +19,6 @@ 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.profiler.context.active.ActiveTraceHandle; import com.navercorp.pinpoint.profiler.context.errorhandler.BypassErrorHandler; import com.navercorp.pinpoint.profiler.context.errorhandler.IgnoreErrorHandler; import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId; @@ -27,7 +26,6 @@ import com.navercorp.pinpoint.profiler.context.recorder.DefaultSpanRecorder; import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder; import com.navercorp.pinpoint.profiler.context.storage.Storage; -import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage; import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService; import org.apache.logging.log4j.LogManager; @@ -76,12 +74,9 @@ public void trace() { SpanRecorder spanRecorder = new DefaultSpanRecorder(span, stringMetaDataService, sqlMetaDataService, errorHandler); WrappedSpanEventRecorder wrappedSpanEventRecorder = new WrappedSpanEventRecorder(traceRoot, asyncContextFactory, stringMetaDataService, sqlMetaDataService, errorHandler); - AsyncContextFactory asyncContextFactory = mock(AsyncContextFactory.class); - Storage storage = mock(Storage.class); - UriStatStorage uriStatStorage = mock(UriStatStorage.class); - Trace trace = new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, ActiveTraceHandle.EMPTY_HANDLE, uriStatStorage); + Trace trace = new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY); trace.traceBlockBegin(); // get data form db @@ -110,13 +105,9 @@ public void popEventTest() { WrappedSpanEventRecorder wrappedSpanEventRecorder = new WrappedSpanEventRecorder(traceRoot, asyncContextFactory, stringMetaDataService, sqlMetaDataService, errorHandler); - AsyncContextFactory asyncContextFactory = mock(AsyncContextFactory.class); - Storage storage = mock(Storage.class); - UriStatStorage uriStatStorage = mock(UriStatStorage.class); - Trace trace = new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, - ActiveTraceHandle.EMPTY_HANDLE, uriStatStorage); + Trace trace = new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY); trace.close();