Skip to content

perf(integrations): Do not register for SystemEvents and NetworkCallbacks when launched with background importance #4579

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 37 commits into from
Aug 6, 2025

Conversation

romtsn
Copy link
Member

@romtsn romtsn commented Jul 30, 2025

📜 Description

  • Do not registerDefaultNetworkCallback when importance is not foreground
    • Will be registered on next foreground
    • Also unregister the network callback on background to avoid being spammed by Android when the app is being unfreezed. This will also invalidate the cached capabilities, but everything else should still work because it would query the network status directly from the OS. It's a small compromise between doing a bit more IPC calls while in background (which should be a small number of things to do anyway) and ensuring the correct connectivity state.
  • Do not register BroadcastReceiver in SystemEvents integration when importance is not foreground
    • Will be registered on next foreground
  • Do not cache isForegroundImportance anymore, because it can actually change during a single process lifetime
  • Added a sdkperf/ folder with explanations and small scripts used to reproduce all kinds of non-standard behavior that the changes from above have been tested against.

Before

This is a network connectivity showcase. App is frozen, disable/enable network 10 times and unfreeze again. You can see that our network status observer is fired many times in a quick succession. This was resulting in submitting many tasks to our background queue in SendCachedEnvelopeIntegration:

Android Studio 2025-07-30 00 49 04

After

Same scenario, but now no network observer is notified, because we're still in background:

Android Studio 2025-07-30 00 49 49

💡 Motivation and Context

💚 How did you test it?

Manually + automated

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

github-actions bot commented Jul 30, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 436.51 ms 483.82 ms 47.31 ms
Size 1.58 MiB 2.09 MiB 521.73 KiB

Previous results on branch: rz/perf/no-network-on-background

Startup times

Revision Plain With Sentry Diff
a6e42e2 387.98 ms 427.92 ms 39.94 ms
28091ec 398.49 ms 425.67 ms 27.18 ms
cb15848 391.38 ms 404.63 ms 13.25 ms
260dc49 381.94 ms 424.55 ms 42.61 ms

App size

Revision Plain With Sentry Diff
a6e42e2 1.58 MiB 2.09 MiB 521.73 KiB
28091ec 1.58 MiB 2.09 MiB 521.78 KiB
cb15848 1.58 MiB 2.09 MiB 520.93 KiB
260dc49 1.58 MiB 2.09 MiB 521.73 KiB

cursor[bot]

This comment was marked as outdated.

Copy link
Member

@markushi markushi left a comment

Choose a reason for hiding this comment

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

Left a few comments for clarification, but already looks really good - nice work!

Copy link
Member

Choose a reason for hiding this comment

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

Awesome!

cursor[bot]

This comment was marked as outdated.

Base automatically changed from rz/perf/single-lifecycle-observer to main August 5, 2025 09:34
cursor[bot]

This comment was marked as outdated.

@romtsn romtsn enabled auto-merge (squash) August 6, 2025 09:46
@romtsn romtsn merged commit bc1e990 into main Aug 6, 2025
35 checks passed
@romtsn romtsn deleted the rz/perf/no-network-on-background branch August 6, 2025 09:53
adinauer pushed a commit that referenced this pull request Aug 8, 2025
adinauer added a commit that referenced this pull request Aug 8, 2025
* Do not report cached events as lost

* E2E tests for OpenTelemetry based console sample (#4563)

* e2e tests for console app

* fix test failures by waiting for 10s after first try to find envelopes

* add system-test-runner.py script to replace bash scripts for running e2e / system tests

* use py script for ci, cleanup, makefile

* Format code

* remove bash scripts

* install requests module

* api

* fix gh script

* Implement E2E tests for OTel based console sample

* fixes after merge

* Format code

* e2e tests for console app

* Implement E2E tests for OTel based console sample

* fixes after merge

* Format code

* api

* Reduce scope forking when using OpenTelemetry (#4565)

* Reduce scope forking in OpenTelemetry

* Format code

* api

* changelog

---------

Co-authored-by: Sentry Github Bot <[email protected]>

* SDKs send queue is no longer shutdown immediately on re-init (#4564)

* Let queue drain on a restart

* Format code

* Format code

* Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

* Let queue drain on a restart

* Format code

* Format code

* Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

* adapt tests

* changelog

---------

Co-authored-by: Sentry Github Bot <[email protected]>

---------

Co-authored-by: Sentry Github Bot <[email protected]>

* release: 8.18.0

* ref(replay): Use main thread to schedule capture (#4542)

* perf(connectivity): Cache network capabilities and status to reduce IPC calls (#4560)

* fix(breadcrumbs): Deduplicate battery breadcrumbs (#4561)

* fix(ci): remove obsolete NDK debug symbols (#4581)

As they don't exist anymore and this is done within sentry-native directly: https://github.com/getsentry/sentry-native/pull/1327/files

* fix(android): Remove unused method (#4585)

* fix(android): Remove unused method

* Update Changelog

* Add rules file for documenting SDK offline behaviour (#4572)

#skip-changelog

## 📜 Description
<!--- Describe your changes in detail -->
Add rules file for documenting SDK offline behaviour

## 💡 Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
Should help speed up AI reasoning about the SDK offline/retry behaviour.

## 💚 How did you test it?


## 📝 Checklist
<!--- Put an `x` in the boxes that apply -->

- [ ] I added tests to verify the changes.
- [ ] No new PII added or SDK only sends newly added PII if `sendDefaultPII` is enabled.
- [ ] I updated the docs if needed.
- [ ] I updated the wizard if needed.
- [ ] Review from the native team if needed.
- [ ] No breaking change or entry added to the changelog.
- [ ] No breaking change for hybrid SDKs or communicated to hybrid SDKs.


## 🔮 Next steps

* perf(connectivity): Have only one NetworkCallback active at a time (#4562)

* fix(scripts): update-gradle script set-version (#4591)

* fix: sentry-android-ndk proguard rule keeps all native class (#4427)

* fix: sentry-androi-ndk proguard rule keeps all native class

* docs: update CHANGELOG

* fix: update CHANGELOG

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Markus Hintersteiner <[email protected]>
Co-authored-by: Markus Hintersteiner <[email protected]>

* refactor(lifecycle): Use single lifecycle observer (#4567)

* perf(connectivity): Cache network capabilities and status to reduce IPC calls

* changelog

* Changelog

* revert

* fix(breadcrumbs): Deduplicate battery breadcrumbs

* ref

* Changelog

* Fix test

* perf(connectivity): Have only one NetworkCallback active at a time

* Changelog

* perf(integrations): Use single lifecycle observer

* Add tests

* Changelog

* Fix tests

* Improve callback handling and test visibility (#4593)

* Null-check lifecycleObserver

---------

Co-authored-by: Markus Hintersteiner <[email protected]>

* fix(sqlite): Fix abstract method error (#4597)

* fix(sqlite): Fix abstract method error

* Update CHANGELOG.md

* Suppress metadata version checks

* perf(integrations): Do not register for SystemEvents and NetworkCallbacks when launched with background importance (#4579)

* fix(android): Ensure frame metrics listeners are registered/unregistered on the main thread (#4582)

* fix(android): Ensure frame metrics listeners are registered/unregistered on the main thread

* Fix race conditions

* Update Changelog

* Update CHANGELOG.md

* Address PR feedback

* perf(executor): Prewarm SentryExecutorService (#4606)

* review feedback

* changelog

* pass through whether cache stored in AndroidEnvelopeCache + test

* Format code

---------

Co-authored-by: Sentry Github Bot <[email protected]>
Co-authored-by: getsentry-bot <[email protected]>
Co-authored-by: Roman Zavarnitsyn <[email protected]>
Co-authored-by: Markus Hintersteiner <[email protected]>
Co-authored-by: Ghasem Shirdel <[email protected]>
Co-authored-by: Markus Hintersteiner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants