Skip to content

Commit

Permalink
Hints are now used via a Hints object and passed into beforeSend and …
Browse files Browse the repository at this point in the history
…EventProcessor as @NotNull Hints object (#2045)

* Replace hint Map with Hints object

* Add changelog

* Fix typo in changelog

* Fix link in changelog

* Attachments can be manipulated via hints (#2046)

* Store attachments in hints and allow manipulation in beforeSend and eventProcessor

* Add changelog

* Add tests for breadcrumbs and attachments via hints

* Update CHANGELOG.md

Co-authored-by: Philipp Hofmann <philipp.hofmann@sentry.io>

* Rename AttachmentContainer to Attachments

* Use long for test

* Move attachments into Hints

* Fix kotlin/java interop for Hints

* Convert screenshot from map entry to property

* Rename hint name param

* Rename clear to clearAttachments

* Use kotlin short version access for getScreenshot

* Move AttachmentsTest into HintsTest; add param names

* Make primitiveMapping table static

* Use ArrayList as there should not be a synchronization issue for hints

Co-authored-by: Philipp Hofmann <philipp.hofmann@sentry.io>

* Rename Hints to Hint

* Remove commented out code

* Code Review

* Rename SentryFallbackConsumer as it is Hints specific

Co-authored-by: Philipp Hofmann <philipp.hofmann@sentry.io>
  • Loading branch information
adinauer and philipphofmann authored May 23, 2022
1 parent 191ca19 commit 134eb16
Show file tree
Hide file tree
Showing 92 changed files with 1,526 additions and 860 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- Allow setting native Android SDK name during build ([#2035](https://github.com/getsentry/sentry-java/pull/2035))
- Include application permissions in Android events ([#2018](https://github.com/getsentry/sentry-java/pull/2018))
- Automatically create transactions for UI events ([#1975](https://github.com/getsentry/sentry-java/pull/1975))
- Hints are now used via a Hint object and passed into beforeSend and EventProcessor as @NotNull Hint object ([#2045](https://github.com/getsentry/sentry-java/pull/2045))
- Attachments can be manipulated via hint ([#2046](https://github.com/getsentry/sentry-java/pull/2046))

### Fixes

Expand Down Expand Up @@ -1660,4 +1662,4 @@ Features from the current SDK like `ANR` are also available (by default triggere
Packages were released on [`bintray`](https://dl.bintray.com/getsentry/sentry-android/io/sentry/), [`jcenter`](https://jcenter.bintray.com/io/sentry/sentry-android/)

We'd love to get feedback and we'll work in getting the GA `2.0.0` out soon.
Until then, the [stable SDK offered by Sentry is at version 1.7.28](https://github.com/getsentry/sentry-java/releases/tag/v1.7.28)
Until then, the [stable SDK offered by Sentry is at version 1.7.28](https://github.com/getsentry/sentry-java/releases/tag/v1.7.28)
2 changes: 1 addition & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public final class io/sentry/android/core/ScreenshotEventProcessor : android/app
public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V
public fun onActivityStarted (Landroid/app/Activity;)V
public fun onActivityStopped (Landroid/app/Activity;)V
public fun process (Lio/sentry/SentryEvent;Ljava/util/Map;)Lio/sentry/SentryEvent;
public fun process (Lio/sentry/SentryEvent;Lio/sentry/hints/Hint;)Lio/sentry/SentryEvent;
}

public final class io/sentry/android/core/SentryAndroid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.SpanStatus;
import io.sentry.hints.Hint;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -126,10 +126,10 @@ private void addBreadcrumb(final @NotNull Activity activity, final @NotNull Stri
breadcrumb.setCategory("ui.lifecycle");
breadcrumb.setLevel(SentryLevel.INFO);

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(ANDROID_ACTIVITY, activity);
final Hint hint = new Hint();
hint.set(ANDROID_ACTIVITY, activity);

hub.addBreadcrumb(breadcrumb, hintMap);
hub.addBreadcrumb(breadcrumb, hint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.android.core.internal.util.DeviceOrientations;
import io.sentry.hints.Hint;
import io.sentry.protocol.Device;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -100,10 +99,10 @@ public void onConfigurationChanged(@NotNull Configuration newConfig) {
breadcrumb.setData("position", orientation);
breadcrumb.setLevel(SentryLevel.INFO);

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(ANDROID_CONFIGURATION, newConfig);
final Hint hint = new Hint();
hint.set(ANDROID_CONFIGURATION, newConfig);

hub.addBreadcrumb(breadcrumb, hintMap);
hub.addBreadcrumb(breadcrumb, hint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.hints.Hint;
import io.sentry.protocol.App;
import io.sentry.protocol.Device;
import io.sentry.protocol.OperatingSystem;
Expand Down Expand Up @@ -118,8 +119,7 @@ public DefaultAndroidEventProcessor(
}

@Override
public @NotNull SentryEvent process(
final @NotNull SentryEvent event, final @Nullable Map<String, Object> hint) {
public @NotNull SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) {
final boolean applyScopeData = shouldApplyScopeData(event, hint);
if (applyScopeData) {
// we only set memory data if it's not a hard crash, when it's a hard crash the event is
Expand All @@ -145,7 +145,7 @@ private void setCommons(
}

private boolean shouldApplyScopeData(
final @NotNull SentryBaseEvent event, final @Nullable Map<String, Object> hint) {
final @NotNull SentryBaseEvent event, final @NotNull Hint hint) {
if (HintUtils.shouldApplyScopeData(hint)) {
return true;
} else {
Expand Down Expand Up @@ -885,7 +885,7 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {

@Override
public @NotNull SentryTransaction process(
final @NotNull SentryTransaction transaction, final @Nullable Map<String, Object> hint) {
final @NotNull SentryTransaction transaction, final @NotNull Hint hint) {
final boolean applyScopeData = shouldApplyScopeData(transaction, hint);

if (applyScopeData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.sentry.android.core;

import static io.sentry.SentryLevel.ERROR;
import static io.sentry.TypeCheckHint.SENTRY_TYPE_CHECK_HINT;

import android.os.FileObserver;
import io.sentry.IEnvelopeSender;
Expand All @@ -10,13 +9,13 @@
import io.sentry.hints.ApplyScopeData;
import io.sentry.hints.Cached;
import io.sentry.hints.Flushable;
import io.sentry.hints.Hint;
import io.sentry.hints.Resettable;
import io.sentry.hints.Retryable;
import io.sentry.hints.SubmissionResult;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,12 +57,11 @@ public void onEvent(int eventType, @Nullable String relativePath) {

// TODO: Only some event types should be pass through?

final CachedEnvelopeHint hint = new CachedEnvelopeHint(flushTimeoutMillis, logger);
final CachedEnvelopeHint cachedHint = new CachedEnvelopeHint(flushTimeoutMillis, logger);

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(SENTRY_TYPE_CHECK_HINT, hint);
final Hint hint = HintUtils.createWithTypeCheckHint(cachedHint);

envelopeSender.processEnvelopeFile(this.rootPath + File.separator + relativePath, hintMap);
envelopeSender.processEnvelopeFile(this.rootPath + File.separator + relativePath, hint);
}

private static final class CachedEnvelopeHint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.sentry.EventProcessor;
import io.sentry.SentryEvent;
import io.sentry.SpanContext;
import io.sentry.hints.Hint;
import io.sentry.protocol.MeasurementValue;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.SentrySpan;
Expand Down Expand Up @@ -42,7 +43,7 @@ final class PerformanceAndroidEventProcessor implements EventProcessor {
*/
@Override
@Nullable
public SentryEvent process(@NotNull SentryEvent event, @Nullable Map<String, Object> hint) {
public SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) {
// that's only necessary because on newer versions of Unity, if not overriding this method, it's
// throwing 'java.lang.AbstractMethodError: abstract method' and the reason is probably
// compilation mismatch.
Expand All @@ -52,7 +53,7 @@ public SentryEvent process(@NotNull SentryEvent event, @Nullable Map<String, Obj
@SuppressWarnings("NullAway")
@Override
public synchronized @NotNull SentryTransaction process(
@NotNull SentryTransaction transaction, @Nullable Map<String, Object> hint) {
@NotNull SentryTransaction transaction, @NotNull Hint hint) {

if (!options.isTracingEnabled()) {
return transaction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.sentry.android.core;

import static io.sentry.TypeCheckHint.ANDROID_ACTIVITY;
import static io.sentry.TypeCheckHint.SENTRY_SCREENSHOT;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand All @@ -17,13 +16,12 @@
import io.sentry.EventProcessor;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.hints.Hint;
import io.sentry.util.Objects;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -68,8 +66,7 @@ public ScreenshotEventProcessor(

@SuppressWarnings("NullAway")
@Override
public @NotNull SentryEvent process(
final @NotNull SentryEvent event, @Nullable Map<String, Object> hint) {
public @NotNull SentryEvent process(final @NotNull SentryEvent event, @NotNull Hint hint) {
if (options.isAttachScreenshot() && event.isErrored() && currentActivity != null) {
final Activity activity = currentActivity.get();
if (isActivityValid(activity)
Expand All @@ -93,16 +90,10 @@ public ScreenshotEventProcessor(
// Some formats, like PNG which is lossless, will ignore the quality setting.
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);

if (hint == null) {
hint = new HashMap<>();
}

if (byteArrayOutputStream.size() > 0) {
// screenshot png is around ~100-150 kb
hint.put(
SENTRY_SCREENSHOT,
Attachment.fromScreenshot(byteArrayOutputStream.toByteArray()));
hint.put(ANDROID_ACTIVITY, activity);
hint.setScreenshot(Attachment.fromScreenshot(byteArrayOutputStream.toByteArray()));
hint.set(ANDROID_ACTIVITY, activity);
} else {
this.options
.getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.sentry.Integration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.hints.Hint;
import io.sentry.util.Objects;
import io.sentry.util.StringUtils;
import java.io.Closeable;
Expand Down Expand Up @@ -215,10 +216,10 @@ public void onReceive(Context context, Intent intent) {
}
breadcrumb.setLevel(SentryLevel.INFO);

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(ANDROID_INTENT, intent);
final Hint hint = new Hint();
hint.set(ANDROID_INTENT, intent);

hub.addBreadcrumb(breadcrumb, hintMap);
hub.addBreadcrumb(breadcrumb, hint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import io.sentry.Integration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.hints.Hint;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
Expand Down Expand Up @@ -107,10 +106,10 @@ public void onSensorChanged(final @NotNull SensorEvent event) {
breadcrumb.setLevel(SentryLevel.INFO);
breadcrumb.setData("degree", event.values[0]); // Celsius

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(ANDROID_SENSOR_EVENT, event);
final Hint hint = new Hint();
hint.set(ANDROID_SENSOR_EVENT, event);

hub.addBreadcrumb(breadcrumb, hintMap);
hub.addBreadcrumb(breadcrumb, hint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import io.sentry.SentryLevel;
import io.sentry.SpanStatus;
import io.sentry.android.core.SentryAndroidOptions;
import io.sentry.hints.Hint;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -186,14 +186,14 @@ private void addBreadcrumb(
className = target.getClass().getSimpleName();
}

final Map<String, Object> hintMap = new HashMap<>();
hintMap.put(ANDROID_MOTION_EVENT, motionEvent);
hintMap.put(ANDROID_VIEW, target);
final Hint hint = new Hint();
hint.set(ANDROID_MOTION_EVENT, motionEvent);
hint.set(ANDROID_VIEW, target);

hub.addBreadcrumb(
Breadcrumb.userInteraction(
eventType, ViewUtils.getResourceIdWithFallback(target), className, additionalData),
hintMap);
hint);
}

private void startTracing(final @NotNull View target, final @NotNull String eventType) {
Expand Down
Loading

0 comments on commit 134eb16

Please sign in to comment.