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

Merged
merged 12 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixes

- 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 @@ -28,6 +28,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.AndroidCurrentDateProvider;
import io.sentry.android.core.internal.util.AndroidThreadChecker;
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.android.core.performance.AppStartMetrics;
Expand Down Expand Up @@ -157,7 +158,8 @@ static void initializeIntegrationsAndProcessors(

if (options.getConnectionStatusProvider() instanceof NoOpConnectionStatusProvider) {
options.setConnectionStatusProvider(
new AndroidConnectionStatusProvider(context, options.getLogger(), buildInfoProvider));
new AndroidConnectionStatusProvider(
context, options, buildInfoProvider, AndroidCurrentDateProvider.getInstance()));
}

if (options.getCacheDirPath() != null) {
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) {
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
Loading
Loading