Skip to content

Commit

Permalink
[#9631] Integrate ExceptionContext lifecycle into ExceptionRecordingS…
Browse files Browse the repository at this point in the history
…ervice
  • Loading branch information
emeroad committed Jan 17, 2024
1 parent 447e328 commit 5fdd297
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.storage.Storage;

Expand All @@ -16,9 +15,8 @@ public AsyncDefaultTrace(Span span,
Storage storage,
SpanRecorder spanRecorder,
WrappedSpanEventRecorder wrappedSpanEventRecorder,
ExceptionContext exceptionContext,
AsyncState asyncState) {
super(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, exceptionContext, CloseListener.EMPTY);
super(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY);

Check warning on line 19 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/AsyncDefaultTrace.java#L19

Added line #L19 was not covered by tests
this.asyncState = Objects.requireNonNull(asyncState, "asyncState");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.scope.DefaultTraceScopePool;
Expand All @@ -47,8 +46,6 @@ public class ChildTrace implements Trace {
private final SpanRecorder spanRecorder;
private final WrappedSpanEventRecorder wrappedSpanEventRecorder;

private final ExceptionContext exceptionContext;

private boolean closed = false;
// lazy initialize
private DefaultTraceScopePool scopePool;
Expand All @@ -58,7 +55,6 @@ public class ChildTrace implements Trace {

public ChildTrace(final TraceRoot traceRoot, CallStack<SpanEvent> callStack, Storage storage,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder,
ExceptionContext exceptionContext,
final LocalAsyncId localAsyncId) {

this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
Expand All @@ -67,7 +63,6 @@ public ChildTrace(final TraceRoot traceRoot, CallStack<SpanEvent> callStack, Sto

this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder");
this.exceptionContext = Objects.requireNonNull(exceptionContext, "exceptionRecordingContext");

this.localAsyncId = Objects.requireNonNull(localAsyncId, "localAsyncId");
traceBlockBegin(ASYNC_BEGIN_STACK_ID);
Expand All @@ -79,7 +74,7 @@ private TraceRoot getTraceRoot() {
}

private SpanEventRecorder wrappedSpanEventRecorder(WrappedSpanEventRecorder wrappedSpanEventRecorder, SpanEvent spanEvent) {
wrappedSpanEventRecorder.setWrapped(spanEvent, this.exceptionContext);
wrappedSpanEventRecorder.setWrapped(spanEvent);

Check warning on line 77 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ChildTrace.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ChildTrace.java#L77

Added line #L77 was not covered by tests
return wrappedSpanEventRecorder;
}

Expand Down Expand Up @@ -185,7 +180,7 @@ public void close0() {
} else {
logSpan();
}

this.wrappedSpanEventRecorder.close();

Check warning on line 183 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ChildTrace.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/ChildTrace.java#L183

Added line #L183 was not covered by tests
this.storage.close();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContextFactory;
import com.navercorp.pinpoint.profiler.context.id.ListenableAsyncState;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.LoggingAsyncState;
Expand Down Expand Up @@ -61,7 +59,6 @@ public class DefaultBaseTraceFactory implements BaseTraceFactory {
private final TraceRootFactory traceRootFactory;

private final ActiveTraceRepository activeTraceRepository;
private final ExceptionContextFactory exceptionContextFactory;
private final UriStatStorage uriStatStorage;

public DefaultBaseTraceFactory(TraceRootFactory traceRootFactory,
Expand All @@ -70,7 +67,6 @@ public DefaultBaseTraceFactory(TraceRootFactory traceRootFactory,
TraceSampler traceSampler,
SpanFactory spanFactory, RecorderFactory recorderFactory,
ActiveTraceRepository activeTraceRepository,
ExceptionContextFactory exceptionContextFactory,
UriStatStorage uriStatStorage) {

this.traceRootFactory = Objects.requireNonNull(traceRootFactory, "traceRootFactory");
Expand All @@ -81,7 +77,6 @@ public DefaultBaseTraceFactory(TraceRootFactory traceRootFactory,
this.spanFactory = Objects.requireNonNull(spanFactory, "spanFactory");
this.recorderFactory = Objects.requireNonNull(recorderFactory, "recorderFactory");
this.activeTraceRepository = Objects.requireNonNull(activeTraceRepository, "activeTraceRepository");
this.exceptionContextFactory = Objects.requireNonNull(exceptionContextFactory, "exceptionContextFactory");
this.uriStatStorage = Objects.requireNonNull(uriStatStorage, "uriStatStorage");

}
Expand Down Expand Up @@ -136,9 +131,8 @@ public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId l

final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newChildTraceSpanEventRecorder(traceRoot);
final ExceptionContext exceptionContext = exceptionContextFactory.newExceptionContext(traceRoot);

return new ChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, exceptionContext, localAsyncId);
return new ChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId);

Check warning on line 135 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java#L135

Added line #L135 was not covered by tests
}

@Override
Expand Down Expand Up @@ -202,11 +196,10 @@ private DefaultTrace newDefaultTrace(TraceRoot traceRoot) {

final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final ExceptionContext exceptionContext = exceptionContextFactory.newExceptionContext(traceRoot);

final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
final CloseListener closeListener = new DefaultCloseListener(traceRoot, handle, uriStatStorage);
return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, exceptionContext, closeListener);
return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, closeListener);
}

private AsyncDefaultTrace newAsyncDefaultTrace(TraceRoot traceRoot) {
Expand All @@ -221,9 +214,8 @@ private AsyncDefaultTrace newAsyncDefaultTrace(TraceRoot traceRoot) {

final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);
final ExceptionContext exceptionContext = exceptionContextFactory.newExceptionContext(traceRoot);

return new AsyncDefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, exceptionContext, asyncState);
return new AsyncDefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, asyncState);

Check warning on line 218 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultBaseTraceFactory.java#L218

Added line #L218 was not covered by tests
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.scope.DefaultTraceScopePool;
Expand All @@ -50,8 +49,6 @@ public class DefaultTrace implements Trace {
private final SpanRecorder spanRecorder;
private final WrappedSpanEventRecorder wrappedSpanEventRecorder;

private final ExceptionContext exceptionContext;

private boolean closed = false;
// lazy initialize
private DefaultTraceScopePool scopePool;
Expand All @@ -61,15 +58,13 @@ public class DefaultTrace implements Trace {
private final CloseListener closeListener;

public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder,
ExceptionContext exceptionContext) {
this(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, exceptionContext, CloseListener.EMPTY);
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder) {
this(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, CloseListener.EMPTY);

Check warning on line 62 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/DefaultTrace.java#L62

Added line #L62 was not covered by tests
}

public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage,
SpanRecorder spanRecorder,
WrappedSpanEventRecorder wrappedSpanEventRecorder,
ExceptionContext exceptionContext,
CloseListener closeListener) {

this.span = Objects.requireNonNull(span, "span");
Expand All @@ -78,7 +73,6 @@ public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage,

this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder");
this.exceptionContext = Objects.requireNonNull(exceptionContext, "exceptionRecordingContext");

this.closeListener = closeListener;

Expand All @@ -95,7 +89,7 @@ private TraceRoot getTraceRoot() {
}

private SpanEventRecorder wrappedSpanEventRecorder(WrappedSpanEventRecorder wrappedSpanEventRecorder, SpanEvent spanEvent) {
wrappedSpanEventRecorder.setWrapped(spanEvent, this.exceptionContext);
wrappedSpanEventRecorder.setWrapped(spanEvent);
return wrappedSpanEventRecorder;
}

Expand Down Expand Up @@ -201,7 +195,7 @@ public void close() {
logSpan();
}

this.exceptionContext.flush();
this.wrappedSpanEventRecorder.close();
this.storage.close();
this.closeListener.close(afterTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionWrapperFactory;
import com.navercorp.pinpoint.profiler.context.exception.sampler.ExceptionTraceSampler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

Expand All @@ -32,23 +30,21 @@
*/
public class DefaultExceptionRecordingService implements ExceptionRecordingService {

private final Logger logger = LogManager.getLogger(getClass());

private final boolean IS_DEBUG = logger.isDebugEnabled();

private final ExceptionTraceSampler exceptionTraceSampler;
private final ExceptionWrapperFactory exceptionWrapperFactory;
private final ExceptionContext exceptionContext;

public DefaultExceptionRecordingService(ExceptionTraceSampler exceptionTraceSampler,
ExceptionWrapperFactory exceptionWrapperFactory
ExceptionWrapperFactory exceptionWrapperFactory,
ExceptionContext exceptionContext
) {
this.exceptionTraceSampler = exceptionTraceSampler;
this.exceptionWrapperFactory = exceptionWrapperFactory;
this.exceptionTraceSampler = Objects.requireNonNull(exceptionTraceSampler, "exceptionTraceSampler");
this.exceptionWrapperFactory = Objects.requireNonNull(exceptionWrapperFactory, "exceptionWrapperFactory");
this.exceptionContext = Objects.requireNonNull(exceptionContext, "exceptionContext");
}

public void recordException(ExceptionContext context, Throwable current, long startTime) {
Objects.requireNonNull(context);

public void recordException(Throwable current, long startTime) {
final ExceptionContext context = this.exceptionContext;
ExceptionRecordingState state = context.stateOf(current);
ExceptionTraceSampler.SamplingState samplingState = getSamplingState(state, context);
state.checkAndApply(context, current, startTime, samplingState, exceptionWrapperFactory);
Expand All @@ -68,7 +64,8 @@ private ExceptionTraceSampler.SamplingState getSamplingState(
return ExceptionTraceSampler.DISABLED;
}

public void recordExceptionIdAnnotation(SpanEvent spanEvent, ExceptionContext context) {
public void recordExceptionIdAnnotation(SpanEvent spanEvent) {
final ExceptionContext context = this.exceptionContext;

Check warning on line 68 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java#L68

Added line #L68 was not covered by tests
if (context.hasValidExceptionId()) {
Annotation<Long> linkId = Annotations.of(AnnotationKey.EXCEPTION_CHAIN_ID.getCode(), context.getExceptionId());
spanEvent.addAnnotation(linkId);
Expand All @@ -77,18 +74,18 @@ public void recordExceptionIdAnnotation(SpanEvent spanEvent, ExceptionContext co

@Override
public void recordException(
ExceptionContext exceptionContext,
SpanEvent spanEvent,
Throwable throwable
) {
this.recordException(
exceptionContext,
throwable,
spanEvent.getStartTime()
);
this.recordExceptionIdAnnotation(
spanEvent,
exceptionContext
);
this.recordExceptionIdAnnotation(spanEvent);
}

Check warning on line 85 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java#L84-L85

Added lines #L84 - L85 were not covered by tests

@Override
public void close() {
this.exceptionContext.flush();

Check warning on line 89 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingService.java#L89

Added line #L89 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 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.exception;

import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContextFactory;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionWrapperFactory;
import com.navercorp.pinpoint.profiler.context.exception.sampler.ExceptionTraceSampler;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

import java.util.Objects;

public class DefaultExceptionRecordingServiceFactory implements ExceptionRecordingServiceFactory {

private final ExceptionTraceSampler exceptionTraceSampler;
private final ExceptionWrapperFactory exceptionWrapperFactory;
private final ExceptionContextFactory exceptionContextFactory;

public DefaultExceptionRecordingServiceFactory(ExceptionTraceSampler exceptionTraceSampler,
ExceptionWrapperFactory exceptionWrapperFactory,
ExceptionContextFactory exceptionContextFactory) {
this.exceptionTraceSampler = Objects.requireNonNull(exceptionTraceSampler, "exceptionTraceSampler");
this.exceptionWrapperFactory = Objects.requireNonNull(exceptionWrapperFactory, "exceptionWrapperFactory");
this.exceptionContextFactory = Objects.requireNonNull(exceptionContextFactory, "exceptionContextFactory");

Check warning on line 39 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java#L36-L39

Added lines #L36 - L39 were not covered by tests

}

Check warning on line 41 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java#L41

Added line #L41 was not covered by tests

@Override
public ExceptionRecordingService newService(TraceRoot traceRoot) {
Objects.requireNonNull(traceRoot, "traceRoot");
ExceptionContext exceptionContext = this.exceptionContextFactory.newExceptionContext(traceRoot);
return new DefaultExceptionRecordingService(exceptionTraceSampler, exceptionWrapperFactory, exceptionContext);

Check warning on line 47 in profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java

View check run for this annotation

Codecov / codecov/patch

profiler/src/main/java/com/navercorp/pinpoint/profiler/context/exception/DefaultExceptionRecordingServiceFactory.java#L45-L47

Added lines #L45 - L47 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 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.exception;

import com.navercorp.pinpoint.profiler.context.exception.disabled.DisabledExceptionRecordingService;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

public class DisableExceptionRecordingServiceFactory implements ExceptionRecordingServiceFactory {

public static final ExceptionRecordingServiceFactory INSTANCE = new DisableExceptionRecordingServiceFactory();

public DisableExceptionRecordingServiceFactory() {
}

@Override
public ExceptionRecordingService newService(TraceRoot traceRoot) {
return DisabledExceptionRecordingService.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package com.navercorp.pinpoint.profiler.context.exception;

import com.navercorp.pinpoint.profiler.context.SpanEvent;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;

/**
* @author intr3p1d
*/
public interface ExceptionRecordingService {
public interface ExceptionRecordingService extends AutoCloseable {

void recordException(ExceptionContext exceptionContext, SpanEvent spanEvent, Throwable throwable);
void recordException(SpanEvent spanEvent, Throwable throwable);

void close();
}
Loading

0 comments on commit 5fdd297

Please sign in to comment.