Skip to content

Commit

Permalink
Merge 24e5ea7 into 16371c5
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored Nov 21, 2022
2 parents 16371c5 + 24e5ea7 commit 9fdf794
Show file tree
Hide file tree
Showing 24 changed files with 251 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import androidx.core.app.FrameMetricsAggregator;
import io.sentry.MeasurementUnit;
import io.sentry.SentryLevel;
import io.sentry.android.core.internal.util.MainThreadChecker;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.protocol.MeasurementValue;
import io.sentry.protocol.SentryId;
import java.util.HashMap;
Expand Down Expand Up @@ -208,7 +208,7 @@ public synchronized void stop() {

private void runSafelyOnUiThread(final Runnable runnable, final String tag) {
try {
if (MainThreadChecker.isMainThread()) {
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
runnable.run();
} else {
handler.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.sentry.SentryLevel;
import io.sentry.android.core.cache.AndroidEnvelopeCache;
import io.sentry.android.core.internal.modules.AssetsModulesLoader;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.android.fragment.FragmentLifecycleIntegration;
import io.sentry.android.timber.SentryTimberIntegration;
Expand Down Expand Up @@ -130,6 +131,7 @@ static void initializeIntegrationsAndProcessors(
options.setTransactionProfiler(
new AndroidTransactionProfiler(context, options, buildInfoProvider, frameMetricsCollector));
options.setModulesLoader(new AssetsModulesLoader(context, options.getLogger()));
options.setMainThreadChecker(AndroidMainThreadChecker.getInstance());
}

private static void installDefaultIntegrations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.sentry.Integration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.android.core.internal.util.MainThreadChecker;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -56,7 +56,7 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
try {
Class.forName("androidx.lifecycle.DefaultLifecycleObserver");
Class.forName("androidx.lifecycle.ProcessLifecycleOwner");
if (MainThreadChecker.isMainThread()) {
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
addObserver(hub);
} else {
// some versions of the androidx lifecycle-process require this to be executed on the main
Expand Down Expand Up @@ -115,7 +115,7 @@ private void removeObserver() {
@Override
public void close() throws IOException {
if (watcher != null) {
if (MainThreadChecker.isMainThread()) {
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
removeObserver();
} else {
// some versions of the androidx lifecycle-process require this to be executed on the main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import io.sentry.SentryBaseEvent;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.ConnectivityChecker;
import io.sentry.android.core.internal.util.DeviceOrientations;
import io.sentry.android.core.internal.util.MainThreadChecker;
import io.sentry.android.core.internal.util.RootChecker;
import io.sentry.protocol.App;
import io.sentry.protocol.Device;
Expand Down Expand Up @@ -217,7 +217,7 @@ private void setThreads(final @NotNull SentryEvent event) {
if (event.getThreads() != null) {
for (SentryThread thread : event.getThreads()) {
if (thread.isCurrent() == null) {
thread.setCurrent(MainThreadChecker.isMainThread(thread));
thread.setCurrent(AndroidMainThreadChecker.getInstance().isMainThread(thread));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.sentry.android.core.internal.util;

import android.os.Looper;
import io.sentry.util.thread.IMainThreadChecker;
import org.jetbrains.annotations.ApiStatus;

/** Class that checks if a given thread is the Android Main/UI thread */
@ApiStatus.Internal
public final class AndroidMainThreadChecker implements IMainThreadChecker {

private static final AndroidMainThreadChecker instance = new AndroidMainThreadChecker();

public static AndroidMainThreadChecker getInstance() {
return instance;
}

private AndroidMainThreadChecker() {}

@Override
public boolean isMainThread(final long threadId) {
return Looper.getMainLooper().getThread().getId() == threadId;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class MainThreadCheckerTest {
class AndroidMainThreadCheckerTest {

@Test
fun `When calling isMainThread from the same thread, it should return true`() {
assertTrue(MainThreadChecker.isMainThread())
assertTrue(AndroidMainThreadChecker.getInstance().isMainThread)
}

@Test
fun `When calling isMainThread with the current thread, it should return true`() {
val thread = Thread.currentThread()
assertTrue(MainThreadChecker.isMainThread(thread))
assertTrue(AndroidMainThreadChecker.getInstance().isMainThread(thread))
}

@Test
fun `When calling isMainThread from a different thread, it should return false`() {
val thread = Thread()
assertFalse(MainThreadChecker.isMainThread(thread))
assertFalse(AndroidMainThreadChecker.getInstance().isMainThread(thread))
}

@Test
Expand All @@ -33,7 +33,7 @@ class MainThreadCheckerTest {
val sentryThread = SentryThread().apply {
id = thread.id
}
assertTrue(MainThreadChecker.isMainThread(sentryThread))
assertTrue(AndroidMainThreadChecker.getInstance().isMainThread(sentryThread))
}

@Test
Expand All @@ -42,6 +42,6 @@ class MainThreadCheckerTest {
val sentryThread = SentryThread().apply {
id = thread.id
}
assertFalse(MainThreadChecker.isMainThread(sentryThread))
assertFalse(AndroidMainThreadChecker.getInstance().isMainThread(sentryThread))
}
}
1 change: 0 additions & 1 deletion sentry-samples/sentry-samples-android/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# https://developer.android.com/studio/build/shrink-code#decode-stack-trace
-keepattributes LineNumberTable,SourceFile


# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames,includedescriptorclasses class * {
native <methods>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.sentry.Sentry;
import io.sentry.SpanStatus;
import io.sentry.UserFeedback;
import io.sentry.instrumentation.file.SentryFileOutputStream;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.User;
import io.sentry.samples.android.compose.ComposeActivity;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected void onCreate(Bundle savedInstanceState) {
view -> {
String fileName = Calendar.getInstance().getTimeInMillis() + "_file.txt";
File file = getApplication().getFileStreamPath(fileName);
try (final FileOutputStream fileOutputStream = new FileOutputStream(file);
try (final FileOutputStream fileOutputStream = new SentryFileOutputStream(file);
final OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(fileOutputStream);
final Writer writer = new BufferedWriter(outputStreamWriter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import retrofit2.Response
class ThirdFragment : Fragment(R.layout.third_fragment) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.findViewById<View>(R.id.third_button).setOnClickListener {
throw RuntimeException("Test")
}
val span = Sentry.getSpan()
val child = span?.startChild("calc")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/third_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tap_me"/>
Expand Down
26 changes: 26 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ public class io/sentry/SentryOptions {
public fun getInAppIncludes ()Ljava/util/List;
public fun getIntegrations ()Ljava/util/List;
public fun getLogger ()Lio/sentry/ILogger;
public fun getMainThreadChecker ()Lio/sentry/util/thread/IMainThreadChecker;
public fun getMaxAttachmentSize ()J
public fun getMaxBreadcrumbs ()I
public fun getMaxCacheItems ()I
Expand Down Expand Up @@ -1454,6 +1455,7 @@ public class io/sentry/SentryOptions {
public fun setHostnameVerifier (Ljavax/net/ssl/HostnameVerifier;)V
public fun setIdleTimeout (Ljava/lang/Long;)V
public fun setLogger (Lio/sentry/ILogger;)V
public fun setMainThreadChecker (Lio/sentry/util/thread/IMainThreadChecker;)V
public fun setMaxAttachmentSize (J)V
public fun setMaxBreadcrumbs (I)V
public fun setMaxCacheItems (I)V
Expand Down Expand Up @@ -1532,6 +1534,11 @@ public abstract interface class io/sentry/SentryOptions$TracesSamplerCallback {
public abstract fun sample (Lio/sentry/SamplingContext;)Ljava/lang/Double;
}

public final class io/sentry/SentryStackTraceFactory {
public fun <init> (Ljava/util/List;Ljava/util/List;)V
public fun getStackFrames ([Ljava/lang/StackTraceElement;)Ljava/util/List;
}

public final class io/sentry/SentryTraceHeader {
public static final field SENTRY_TRACE_HEADER Ljava/lang/String;
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Ljava/lang/Boolean;)V
Expand Down Expand Up @@ -3335,6 +3342,7 @@ public abstract class io/sentry/transport/TransportResult {
}

public final class io/sentry/util/CollectionUtils {
public static fun filterListEntries (Ljava/util/List;Lio/sentry/util/CollectionUtils$Predicate;)Ljava/util/List;
public static fun filterMapEntries (Ljava/util/Map;Lio/sentry/util/CollectionUtils$Predicate;)Ljava/util/Map;
public static fun map (Ljava/util/List;Lio/sentry/util/CollectionUtils$Mapper;)Ljava/util/List;
public static fun newArrayList (Ljava/util/List;)Ljava/util/List;
Expand Down Expand Up @@ -3445,6 +3453,24 @@ public final class io/sentry/util/StringUtils {
public static fun removeSurrounding (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
}

public abstract interface class io/sentry/util/thread/IMainThreadChecker {
public fun isMainThread ()Z
public abstract fun isMainThread (J)Z
public fun isMainThread (Lio/sentry/protocol/SentryThread;)Z
public fun isMainThread (Ljava/lang/Thread;)Z
}

public final class io/sentry/util/thread/MainThreadChecker : io/sentry/util/thread/IMainThreadChecker {
public static fun getInstance ()Lio/sentry/util/thread/MainThreadChecker;
public fun isMainThread (J)Z
}

public final class io/sentry/util/thread/NoOpMainThreadChecker : io/sentry/util/thread/IMainThreadChecker {
public fun <init> ()V
public static fun getInstance ()Lio/sentry/util/thread/NoOpMainThreadChecker;
public fun isMainThread (J)Z
}

public class io/sentry/vendor/Base64 {
public static final field CRLF I
public static final field DEFAULT I
Expand Down
9 changes: 9 additions & 0 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import io.sentry.protocol.User;
import io.sentry.transport.NoOpEnvelopeCache;
import io.sentry.util.FileUtils;
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.MainThreadChecker;
import io.sentry.util.thread.NoOpMainThreadChecker;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down Expand Up @@ -279,6 +282,12 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
options.setModulesLoader(new ResourcesModulesLoader(options.getLogger()));
}

final IMainThreadChecker mainThreadChecker = options.getMainThreadChecker();
// only override the MainThreadChecker if it's not already set by Android
if (mainThreadChecker instanceof NoOpMainThreadChecker) {
options.setMainThreadChecker(MainThreadChecker.getInstance());
}

return true;
}

Expand Down
12 changes: 12 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.sentry.util.Platform;
import io.sentry.util.SampleRateUtils;
import io.sentry.util.StringUtils;
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.NoOpMainThreadChecker;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -361,6 +363,8 @@ public class SentryOptions {
/** Modules (dependencies, packages) that will be send along with each event. */
private @NotNull IModulesLoader modulesLoader = NoOpModulesLoader.getInstance();

private @NotNull IMainThreadChecker mainThreadChecker = NoOpMainThreadChecker.getInstance();

/**
* Adds an event processor
*
Expand Down Expand Up @@ -1766,6 +1770,14 @@ public void setModulesLoader(final @Nullable IModulesLoader modulesLoader) {
this.modulesLoader = modulesLoader != null ? modulesLoader : NoOpModulesLoader.getInstance();
}

public @NotNull IMainThreadChecker getMainThreadChecker() {
return mainThreadChecker;
}

public void setMainThreadChecker(final @NotNull IMainThreadChecker mainThreadChecker) {
this.mainThreadChecker = mainThreadChecker;
}

/** The BeforeSend callback */
public interface BeforeSendCallback {

Expand Down
6 changes: 4 additions & 2 deletions sentry/src/main/java/io/sentry/SentryStackTraceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

/** class responsible for converting Java StackTraceElements to SentryStackFrames */
final class SentryStackTraceFactory {
@ApiStatus.Internal
public final class SentryStackTraceFactory {

/** list of inApp excludes */
private final @Nullable List<String> inAppExcludes;
Expand All @@ -29,7 +31,7 @@ public SentryStackTraceFactory(
* @return list of SentryStackFrames or null if none
*/
@Nullable
List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[] elements) {
public List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[] elements) {
List<SentryStackFrame> sentryStackFrames = null;

if (elements != null && elements.length > 0) {
Expand Down
Loading

0 comments on commit 9fdf794

Please sign in to comment.