sentry-cocoa icon indicating copy to clipboard operation
sentry-cocoa copied to clipboard

Detect low power mode

Open noahsmartin opened this issue 6 months ago • 3 comments

Problem Statement

Particularly for performance related issues it would be helpful to see in the issue context if the device was in low power mode. Additionally low power mode state changes could be a useful breadcrumb

Solution Brainstorm

Seems pretty straightforward to get the data: https://developer.apple.com/documentation/foundation/processinfo/islowpowermodeenabled

just not sure how it would be piped through

Are you willing to submit a PR?

No response

noahsmartin avatar May 29 '25 16:05 noahsmartin

I guess I never created an issue for this before when we built profiling metrics (cpu, memory, energy usage) but i had a list of similar ones in a private notion doc: thermal/power-mode/memory-pressure state changes.

This would be cool to add to those profiling metrics we already gather, record the timestamps of state changes as breadcrumbs, or some other kind of event/marker.

armcknight avatar May 30 '25 08:05 armcknight

The app context would be the place to go

https://github.com/getsentry/sentry-cocoa/blob/f96ab191e62125a645c846318e73eb96dc0a5794/Sources/Sentry/SentryClient.m#L736-L747

But the logic in the client only works for non-crash events. For crash events, we would need to store that information somewhere. We can't access that information when we're crashing, so we would need to subscribe to NSProcessInfoPowerStateDidChangeNotification and store that info on the context of the scope, which then serializes that info to SentryCrash via SentryCrashScopeObserver could do the trick.

The SentryCrashWrapper creates the initial app context and puts it to the scope, but that's only for scope that doesn't change: https://github.com/getsentry/sentry-cocoa/blob/f96ab191e62125a645c846318e73eb96dc0a5794/Sources/Sentry/SentryCrashWrapper.m#L201-L214

philipphofmann avatar May 30 '25 08:05 philipphofmann

When adding this to the app context we should consider if we want to have this indexed (in Sentry) so that it is possible to find issues caused by low power mode (not happening by default).

philprime avatar Jun 18 '25 12:06 philprime