Skip to content

Commit

Permalink
Merge cf1f4b2 into 327cc51
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanosiano authored Sep 20, 2024
2 parents 327cc51 + cf1f4b2 commit e312dff
Show file tree
Hide file tree
Showing 38 changed files with 187 additions and 132 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
- For example, (this is already a default behavior) to redact all `TextView`s and their subclasses (`RadioButton`, `EditText`, etc.): `options.experimental.sessionReplay.addRedactViewClass("android.widget.TextView")`
- If you're using code obfuscation, adjust your proguard-rules accordingly, so your custom view class name is not minified

### Behavioural Changes

- (Android) Replace thread id with kernel thread id in span data ([#3706](https://github.com/getsentry/sentry-java/pull/3706))

### Dependencies

- Bump OpenTelemetry to 1.41.0, OpenTelemetry Java Agent to 2.7.0 and Semantic Conventions to 1.25.0 ([#3668](https://github.com/getsentry/sentry-java/pull/3668))
Expand Down
4 changes: 2 additions & 2 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ public final class io/sentry/android/core/ViewHierarchyEventProcessor : io/sentr
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction;
public static fun snapshotViewHierarchy (Landroid/app/Activity;Lio/sentry/ILogger;)Lio/sentry/protocol/ViewHierarchy;
public static fun snapshotViewHierarchy (Landroid/app/Activity;Ljava/util/List;Lio/sentry/util/thread/IMainThreadChecker;Lio/sentry/ILogger;)Lio/sentry/protocol/ViewHierarchy;
public static fun snapshotViewHierarchy (Landroid/app/Activity;Ljava/util/List;Lio/sentry/util/thread/IThreadChecker;Lio/sentry/ILogger;)Lio/sentry/protocol/ViewHierarchy;
public static fun snapshotViewHierarchy (Landroid/view/View;)Lio/sentry/protocol/ViewHierarchy;
public static fun snapshotViewHierarchy (Landroid/view/View;Ljava/util/List;)Lio/sentry/protocol/ViewHierarchy;
public static fun snapshotViewHierarchyAsData (Landroid/app/Activity;Lio/sentry/util/thread/IMainThreadChecker;Lio/sentry/ISerializer;Lio/sentry/ILogger;)[B
public static fun snapshotViewHierarchyAsData (Landroid/app/Activity;Lio/sentry/util/thread/IThreadChecker;Lio/sentry/ISerializer;Lio/sentry/ILogger;)[B
}

public final class io/sentry/android/core/cache/AndroidEnvelopeCache : io/sentry/cache/EnvelopeCache {
Expand Down
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.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.protocol.MeasurementValue;
import io.sentry.protocol.SentryId;
import java.util.HashMap;
Expand Down Expand Up @@ -215,7 +215,7 @@ public synchronized void stop() {

private void runSafelyOnUiThread(final Runnable runnable, final String tag) {
try {
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
if (AndroidThreadChecker.getInstance().isMainThread()) {
runnable.run();
} else {
handler.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.sentry.android.core.internal.gestures.AndroidViewGestureTargetLocator;
import io.sentry.android.core.internal.modules.AssetsModulesLoader;
import io.sentry.android.core.internal.util.AndroidConnectionStatusProvider;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.android.core.performance.AppStartMetrics;
import io.sentry.android.fragment.FragmentLifecycleIntegration;
Expand Down Expand Up @@ -211,7 +211,7 @@ static void initializeIntegrationsAndProcessors(
options.setViewHierarchyExporters(viewHierarchyExporters);
}

options.setMainThreadChecker(AndroidMainThreadChecker.getInstance());
options.setThreadChecker(AndroidThreadChecker.getInstance());
if (options.getPerformanceCollectors().isEmpty()) {
options.addPerformanceCollector(new AndroidMemoryCollector());
options.addPerformanceCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.sentry.Integration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -58,7 +58,7 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
try {
Class.forName("androidx.lifecycle.DefaultLifecycleObserver");
Class.forName("androidx.lifecycle.ProcessLifecycleOwner");
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
if (AndroidThreadChecker.getInstance().isMainThread()) {
addObserver(scopes);
} else {
// some versions of the androidx lifecycle-process require this to be executed on the main
Expand Down Expand Up @@ -127,7 +127,7 @@ public void close() throws IOException {
if (watcher == null) {
return;
}
if (AndroidMainThreadChecker.getInstance().isMainThread()) {
if (AndroidThreadChecker.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 @@ -11,7 +11,7 @@
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.SentryReplayEvent;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.android.core.performance.AppStartMetrics;
import io.sentry.android.core.performance.TimeSpan;
import io.sentry.protocol.App;
Expand Down Expand Up @@ -214,7 +214,7 @@ private void setThreads(final @NotNull SentryEvent event, final @NotNull Hint hi
final boolean isHybridSDK = HintUtils.isFromHybridSdk(hint);

for (final SentryThread thread : event.getThreads()) {
final boolean isMainThread = AndroidMainThreadChecker.getInstance().isMainThread(thread);
final boolean isMainThread = AndroidThreadChecker.getInstance().isMainThread(thread);

// TODO: Fix https://github.com/getsentry/team-mobile/issues/47
if (thread.isCurrent() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static SentryId captureEnvelope(
deleteCurrentSessionFile(
options,
// should be sync if going to crash or already not a main thread
!maybeStartNewSession || !scopes.getOptions().getMainThreadChecker().isMainThread());
!maybeStartNewSession || !scopes.getOptions().getThreadChecker().isMainThread());
if (maybeStartNewSession) {
scopes.startSession();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.sentry.android.core.ActivityLifecycleIntegration.APP_START_WARM;
import static io.sentry.android.core.ActivityLifecycleIntegration.UI_LOAD_OP;

import android.os.Looper;
import io.sentry.EventProcessor;
import io.sentry.Hint;
import io.sentry.MeasurementUnit;
Expand All @@ -13,6 +12,7 @@
import io.sentry.SpanDataConvention;
import io.sentry.SpanId;
import io.sentry.SpanStatus;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.android.core.performance.ActivityLifecycleTimeSpan;
import io.sentry.android.core.performance.AppStartMetrics;
import io.sentry.android.core.performance.TimeSpan;
Expand Down Expand Up @@ -317,7 +317,7 @@ private static SentrySpan timeSpanToSentrySpan(
final @NotNull String operation) {

final Map<String, Object> defaultSpanData = new HashMap<>(2);
defaultSpanData.put(SpanDataConvention.THREAD_ID, Looper.getMainLooper().getThread().getId());
defaultSpanData.put(SpanDataConvention.THREAD_ID, AndroidThreadChecker.mainThreadId);
defaultSpanData.put(SpanDataConvention.THREAD_NAME, "main");

defaultSpanData.put(SpanDataConvention.CONTRIBUTES_TTID, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ScreenshotEventProcessor(

final byte[] screenshot =
takeScreenshot(
activity, options.getMainThreadChecker(), options.getLogger(), buildInfoProvider);
activity, options.getThreadChecker(), options.getLogger(), buildInfoProvider);
if (screenshot == null) {
return event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.sentry.SentryLevel;
import io.sentry.android.core.internal.gestures.ViewUtils;
import io.sentry.android.core.internal.util.AndroidCurrentDateProvider;
import io.sentry.android.core.internal.util.AndroidMainThreadChecker;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.android.core.internal.util.ClassUtil;
import io.sentry.android.core.internal.util.Debouncer;
import io.sentry.internal.viewhierarchy.ViewHierarchyExporter;
Expand All @@ -25,7 +25,7 @@
import io.sentry.util.HintUtils;
import io.sentry.util.JsonSerializationUtils;
import io.sentry.util.Objects;
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.IThreadChecker;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -101,7 +101,7 @@ public ViewHierarchyEventProcessor(final @NotNull SentryAndroidOptions options)
snapshotViewHierarchy(
activity,
options.getViewHierarchyExporters(),
options.getMainThreadChecker(),
options.getThreadChecker(),
options.getLogger());

if (viewHierarchy != null) {
Expand All @@ -113,13 +113,13 @@ public ViewHierarchyEventProcessor(final @NotNull SentryAndroidOptions options)

public static byte[] snapshotViewHierarchyAsData(
@Nullable Activity activity,
@NotNull IMainThreadChecker mainThreadChecker,
@NotNull IThreadChecker threadChecker,
@NotNull ISerializer serializer,
@NotNull ILogger logger) {

@Nullable
ViewHierarchy viewHierarchy =
snapshotViewHierarchy(activity, new ArrayList<>(0), mainThreadChecker, logger);
snapshotViewHierarchy(activity, new ArrayList<>(0), threadChecker, logger);

if (viewHierarchy == null) {
logger.log(SentryLevel.ERROR, "Could not get ViewHierarchy.");
Expand All @@ -144,14 +144,14 @@ public static byte[] snapshotViewHierarchyAsData(
public static ViewHierarchy snapshotViewHierarchy(
final @Nullable Activity activity, final @NotNull ILogger logger) {
return snapshotViewHierarchy(
activity, new ArrayList<>(0), AndroidMainThreadChecker.getInstance(), logger);
activity, new ArrayList<>(0), AndroidThreadChecker.getInstance(), logger);
}

@Nullable
public static ViewHierarchy snapshotViewHierarchy(
final @Nullable Activity activity,
final @NotNull List<ViewHierarchyExporter> exporters,
final @NotNull IMainThreadChecker mainThreadChecker,
final @NotNull IThreadChecker threadChecker,
final @NotNull ILogger logger) {

if (activity == null) {
Expand All @@ -172,7 +172,7 @@ public static ViewHierarchy snapshotViewHierarchy(
}

try {
if (mainThreadChecker.isMainThread()) {
if (threadChecker.isMainThread()) {
return snapshotViewHierarchy(decorView, exporters);
} else {
final CountDownLatch latch = new CountDownLatch(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package io.sentry.android.core.internal.util;

import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import io.sentry.protocol.SentryThread;
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.IThreadChecker;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

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

private static final AndroidMainThreadChecker instance = new AndroidMainThreadChecker();
private static final AndroidThreadChecker instance = new AndroidThreadChecker();
public static long mainThreadId = Process.myTid();

public static AndroidMainThreadChecker getInstance() {
public static AndroidThreadChecker getInstance() {
return instance;
}

private AndroidMainThreadChecker() {}
private AndroidThreadChecker() {
// The first time this class is loaded, we make sure to set the correct mainThreadId
new Handler(Looper.getMainLooper()).post(() -> mainThreadId = Process.myTid());
}

@Override
public boolean isMainThread(final long threadId) {
Expand All @@ -38,4 +44,9 @@ public boolean isMainThread(final @NotNull SentryThread sentryThread) {
final Long threadId = sentryThread.getId();
return threadId != null && isMainThread(threadId);
}

@Override
public long currentThreadSystemId() {
return Process.myTid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io.sentry.ILogger;
import io.sentry.SentryLevel;
import io.sentry.android.core.BuildInfoProvider;
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.IThreadChecker;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand All @@ -31,14 +31,13 @@ public class ScreenshotUtils {
final @NotNull Activity activity,
final @NotNull ILogger logger,
final @NotNull BuildInfoProvider buildInfoProvider) {
return takeScreenshot(
activity, AndroidMainThreadChecker.getInstance(), logger, buildInfoProvider);
return takeScreenshot(activity, AndroidThreadChecker.getInstance(), logger, buildInfoProvider);
}

@SuppressLint("NewApi")
public static @Nullable byte[] takeScreenshot(
final @NotNull Activity activity,
final @NotNull IMainThreadChecker mainThreadChecker,
final @NotNull IThreadChecker threadChecker,
final @NotNull ILogger logger,
final @NotNull BuildInfoProvider buildInfoProvider) {
// We are keeping BuildInfoProvider param for compatibility, as it's being used by
Expand Down Expand Up @@ -113,7 +112,7 @@ public class ScreenshotUtils {
}
} else {
final Canvas canvas = new Canvas(bitmap);
if (mainThreadChecker.isMainThread()) {
if (threadChecker.isMainThread()) {
view.draw(canvas);
latch.countDown();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.sentry.SentryOptions
import io.sentry.android.core.cache.AndroidEnvelopeCache
import io.sentry.android.core.internal.gestures.AndroidViewGestureTargetLocator
import io.sentry.android.core.internal.modules.AssetsModulesLoader
import io.sentry.android.core.internal.util.AndroidMainThreadChecker
import io.sentry.android.core.internal.util.AndroidThreadChecker
import io.sentry.android.fragment.FragmentLifecycleIntegration
import io.sentry.android.replay.ReplayIntegration
import io.sentry.android.timber.SentryTimberIntegration
Expand Down Expand Up @@ -582,10 +582,10 @@ class AndroidOptionsInitializerTest {
}

@Test
fun `AndroidMainThreadChecker is set to options`() {
fun `AndroidThreadChecker is set to options`() {
fixture.initSut()

assertTrue { fixture.sentryOptions.mainThreadChecker is AndroidMainThreadChecker }
assertTrue { fixture.sentryOptions.threadChecker is AndroidThreadChecker }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.sentry.SentryEvent
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.TypeCheckHint.ANDROID_ACTIVITY
import io.sentry.protocol.SentryException
import io.sentry.util.thread.IMainThreadChecker
import io.sentry.util.thread.IThreadChecker
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
Expand All @@ -35,7 +35,7 @@ class ScreenshotEventProcessorTest {
val window = mock<Window>()
val view = mock<View>()
val rootView = mock<View>()
val mainThreadChecker = mock<IMainThreadChecker>()
val threadChecker = mock<IThreadChecker>()
val options = SentryAndroidOptions().apply {
dsn = "https://[email protected]/proj"
}
Expand All @@ -52,12 +52,12 @@ class ScreenshotEventProcessorTest {
it.getArgument<Runnable>(0).run()
}

whenever(mainThreadChecker.isMainThread).thenReturn(true)
whenever(threadChecker.isMainThread).thenReturn(true)
}

fun getSut(attachScreenshot: Boolean = false): ScreenshotEventProcessor {
options.isAttachScreenshot = attachScreenshot
options.mainThreadChecker = mainThreadChecker
options.threadChecker = threadChecker

return ScreenshotEventProcessor(options, buildInfo)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ class ScreenshotEventProcessorTest {
@Test
fun `when screenshot event processor is called from background thread it executes on main thread`() {
val sut = fixture.getSut(true)
whenever(fixture.mainThreadChecker.isMainThread).thenReturn(false)
whenever(fixture.threadChecker.isMainThread).thenReturn(false)

CurrentActivityHolder.getInstance().setActivity(fixture.activity)

Expand Down
Loading

0 comments on commit e312dff

Please sign in to comment.