Skip to content

fix(breadcrumbs): Deduplicate battery breadcrumbs #4561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: rz/perf/less-ipc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Allow multiple UncaughtExceptionHandlerIntegrations to be active at the same time ([#4462](https://github.com/getsentry/sentry-java/pull/4462))
- Cache network capabilities and status to reduce IPC calls ([#4560](https://github.com/getsentry/sentry-java/pull/4560))
- Deduplicate battery breadcrumbs ([#4561](https://github.com/getsentry/sentry-java/pull/4561))

## 8.17.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,43 @@ static final class SystemEventsBroadcastReceiver extends BroadcastReceiver {
private final @NotNull Debouncer batteryChangedDebouncer =
new Debouncer(AndroidCurrentDateProvider.getInstance(), DEBOUNCE_WAIT_TIME_MS, 0);

// Track previous battery state to avoid duplicate breadcrumbs when values haven't changed
private @Nullable BatteryState previousBatteryState;

static final class BatteryState {
private final @Nullable Float level;
private final @Nullable Boolean charging;

BatteryState(final @Nullable Float level, final @Nullable Boolean charging) {
this.level = level;
this.charging = charging;
}

@Override
public boolean equals(final @Nullable Object other) {
if (!(other instanceof BatteryState)) return false;
BatteryState that = (BatteryState) other;
return isSimilarLevel(level, that.level) && Objects.equals(charging, that.charging);
}

@Override
public int hashCode() {
// Use rounded level for hash consistency
Float roundedLevel = level != null ? Math.round(level * 100f) / 100f : null;
return Objects.hash(roundedLevel, charging);
}

private boolean isSimilarLevel(final @Nullable Float level1, final @Nullable Float level2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO I'd rather store the battery level as an integer value directly, saves us some extra logic. I guess it's fine to have the breadcrumb report an integer value too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds good, I also was hesitating with this logic tbh. Will adjust 👍

if (level1 == null && level2 == null) return true;
if (level1 == null || level2 == null) return false;

// Round both levels to 2 decimal places and compare
float rounded1 = Math.round(level1 * 100f) / 100f;
float rounded2 = Math.round(level2 * 100f) / 100f;
return rounded1 == rounded2;
}
}

SystemEventsBroadcastReceiver(
final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) {
this.scopes = scopes;
Expand All @@ -350,19 +387,34 @@ public void onReceive(final Context context, final @NotNull Intent intent) {
final @Nullable String action = intent.getAction();
final boolean isBatteryChanged = ACTION_BATTERY_CHANGED.equals(action);

// aligning with iOS which only captures battery status changes every minute at maximum
if (isBatteryChanged && batteryChangedDebouncer.checkForDebounce()) {
return;
@Nullable BatteryState batteryState = null;
if (isBatteryChanged) {
if (batteryChangedDebouncer.checkForDebounce()) {
// aligning with iOS which only captures battery status changes every minute at maximum
return;
}

// For battery changes, check if the actual values have changed
final @Nullable Float currentBatteryLevel = DeviceInfoUtil.getBatteryLevel(intent, options);
final @Nullable Boolean currentChargingState = DeviceInfoUtil.isCharging(intent, options);
batteryState = new BatteryState(currentBatteryLevel, currentChargingState);

// Only create breadcrumb if battery state has actually changed
if (batteryState.equals(previousBatteryState)) {
return;
}

previousBatteryState = batteryState;
}

final BatteryState state = batteryState;
final long now = System.currentTimeMillis();
try {
options
.getExecutorService()
.submit(
() -> {
final Breadcrumb breadcrumb =
createBreadcrumb(now, intent, action, isBatteryChanged);
final Breadcrumb breadcrumb = createBreadcrumb(now, intent, action, state);
final Hint hint = new Hint();
hint.set(ANDROID_INTENT, intent);
scopes.addBreadcrumb(breadcrumb, hint);
Expand Down Expand Up @@ -411,7 +463,7 @@ String getStringAfterDotFast(final @Nullable String str) {
final long timeMs,
final @NotNull Intent intent,
final @Nullable String action,
boolean isBatteryChanged) {
final @Nullable BatteryState batteryState) {
final Breadcrumb breadcrumb = new Breadcrumb(timeMs);
breadcrumb.setType("system");
breadcrumb.setCategory("device.event");
Expand All @@ -420,14 +472,12 @@ String getStringAfterDotFast(final @Nullable String str) {
breadcrumb.setData("action", shortAction);
}

if (isBatteryChanged) {
final Float batteryLevel = DeviceInfoUtil.getBatteryLevel(intent, options);
if (batteryLevel != null) {
breadcrumb.setData("level", batteryLevel);
if (batteryState != null) {
if (batteryState.level != null) {
breadcrumb.setData("level", batteryState.level);
}
final Boolean isCharging = DeviceInfoUtil.isCharging(intent, options);
if (isCharging != null) {
breadcrumb.setData("charging", isCharging);
if (batteryState.charging != null) {
breadcrumb.setData("charging", batteryState.charging);
}
} else {
final Bundle extras = intent.getExtras();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,83 @@ class SystemEventsBreadcrumbsIntegrationTest {
verifyNoMoreInteractions(fixture.scopes)
}

@Test
fun `battery changes with identical values do not generate breadcrumbs`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)

val intent1 =
Intent().apply {
action = Intent.ACTION_BATTERY_CHANGED
putExtra(BatteryManager.EXTRA_LEVEL, 80)
putExtra(BatteryManager.EXTRA_SCALE, 100)
putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB)
}
val intent2 =
Intent().apply {
action = Intent.ACTION_BATTERY_CHANGED
putExtra(BatteryManager.EXTRA_LEVEL, 80)
putExtra(BatteryManager.EXTRA_SCALE, 100)
putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB)
}

// Receive first battery change
sut.receiver!!.onReceive(fixture.context, intent1)

// Receive second battery change with identical values
sut.receiver!!.onReceive(fixture.context, intent2)

// should only add the first crumb since values are identical
verify(fixture.scopes)
.addBreadcrumb(
check<Breadcrumb> {
assertEquals(it.data["level"], 80f)
assertEquals(it.data["charging"], true)
},
anyOrNull(),
)
verifyNoMoreInteractions(fixture.scopes)
}

@Test
fun `battery changes with minor level differences do not generate breadcrumbs`() {
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)

val intent1 =
Intent().apply {
action = Intent.ACTION_BATTERY_CHANGED
putExtra(BatteryManager.EXTRA_LEVEL, 80001) // 80.001%
putExtra(BatteryManager.EXTRA_SCALE, 100000)
putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB)
}
val intent2 =
Intent().apply {
action = Intent.ACTION_BATTERY_CHANGED
putExtra(BatteryManager.EXTRA_LEVEL, 80002) // 80.002%
putExtra(BatteryManager.EXTRA_SCALE, 100000)
putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB)
}

// Receive first battery change
sut.receiver!!.onReceive(fixture.context, intent1)

// Receive second battery change with very minor level difference (rounds to same 3 decimal
// places)
sut.receiver!!.onReceive(fixture.context, intent2)

// should only add the first crumb since both round to 80.000%
verify(fixture.scopes)
.addBreadcrumb(
check<Breadcrumb> {
assertEquals(it.data["level"], 80.001f)
assertEquals(it.data["charging"], true)
},
anyOrNull(),
)
verifyNoMoreInteractions(fixture.scopes)
}

@Test
fun `Do not crash if registerReceiver throws exception`() {
val sut = fixture.getSut()
Expand Down
Loading