Skip to content

Commit

Permalink
Crash handler join() async flush result (#368)
Browse files Browse the repository at this point in the history
* ensure that the async result is joined

* spotless
  • Loading branch information
breedx-splk authored Oct 3, 2022
1 parent d146504 commit 31b481d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

class CrashReporter {
Expand Down Expand Up @@ -73,7 +75,8 @@ public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
.setStatus(StatusCode.ERROR)
.end();
// do our best to make sure the crash makes it out of the VM
sdkTracerProvider.forceFlush();
CompletableResultCode result = sdkTracerProvider.forceFlush();
result.join(10, TimeUnit.SECONDS);
// preserve any existing behavior:
if (existingHandler != null) {
existingHandler.uncaughtException(t, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import androidx.annotation.NonNull;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -40,16 +43,20 @@
public class CrashReporterTest {
@Rule public OpenTelemetryRule otelTesting = OpenTelemetryRule.create();
private Tracer tracer;
private SdkTracerProvider sdkTracerProvider;
private CompletableResultCode flushResult;

@Before
public void setup() {
tracer = otelTesting.getOpenTelemetry().getTracer("testTracer");
sdkTracerProvider = mock(SdkTracerProvider.class);
flushResult = mock(CompletableResultCode.class);
when(sdkTracerProvider.forceFlush()).thenReturn(flushResult);
}

@Test
public void crashReportingSpan() {
TestDelegateHandler existingHandler = new TestDelegateHandler();
SdkTracerProvider sdkTracerProvider = mock(SdkTracerProvider.class);
CrashReporter.CrashReportingExceptionHandler crashReporter =
new CrashReporter.CrashReportingExceptionHandler(
tracer, sdkTracerProvider, existingHandler);
Expand All @@ -74,11 +81,11 @@ public void crashReportingSpan() {

assertTrue(existingHandler.wasDelegatedTo.get());
verify(sdkTracerProvider).forceFlush();
verify(flushResult).join(10, TimeUnit.SECONDS);
}

@Test
public void multipleErrorsDuringACrash() {
SdkTracerProvider sdkTracerProvider = mock(SdkTracerProvider.class);
CrashReporter.CrashReportingExceptionHandler crashReporter =
new CrashReporter.CrashReportingExceptionHandler(tracer, sdkTracerProvider, null);

Expand Down

0 comments on commit 31b481d

Please sign in to comment.