Skip to content

Commit 07534f5

Browse files
Allow opting out of device info collection that requires Inter-Process Communication (IPC) (#2100)
* Allow opting out of device info requiring IPC * Add changelog * Update sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java Co-authored-by: Manoel Aranda Neto <[email protected]> * Remove logger * Rename option and manifest entry * Allow opting out of getDeviceIo completely Co-authored-by: Manoel Aranda Neto <[email protected]>
1 parent a88509c commit 07534f5

File tree

8 files changed

+144
-55
lines changed

8 files changed

+144
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Replace `tracestate` header with `baggage` header ([#2078](https://github.com/getsentry/sentry-java/pull/2078))
8+
- Allow opting out of device info collection that requires Inter-Process Communication (IPC) ([#2100](https://github.com/getsentry/sentry-java/pull/2100))
89

910
## 6.1.0
1011

sentry-android-core/api/sentry-android-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
129129
public fun isAnrEnabled ()Z
130130
public fun isAnrReportInDebug ()Z
131131
public fun isAttachScreenshot ()Z
132+
public fun isCollectAdditionalContext ()Z
132133
public fun isEnableActivityLifecycleBreadcrumbs ()Z
133134
public fun isEnableActivityLifecycleTracingAutoFinish ()Z
134135
public fun isEnableAppComponentBreadcrumbs ()Z
@@ -141,6 +142,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
141142
public fun setAnrReportInDebug (Z)V
142143
public fun setAnrTimeoutIntervalMillis (J)V
143144
public fun setAttachScreenshot (Z)V
145+
public fun setCollectAdditionalContext (Z)V
144146
public fun setDebugImagesLoader (Lio/sentry/android/core/IDebugImagesLoader;)V
145147
public fun setEnableActivityLifecycleBreadcrumbs (Z)V
146148
public fun setEnableActivityLifecycleTracingAutoFinish (Z)V

sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static void init(
146146

147147
readDefaultOptionValues(options, context);
148148

149-
options.addEventProcessor(new DefaultAndroidEventProcessor(context, logger, buildInfoProvider));
149+
options.addEventProcessor(
150+
new DefaultAndroidEventProcessor(context, buildInfoProvider, options));
150151
options.addEventProcessor(new PerformanceAndroidEventProcessor(options, activityFramesTracker));
151152

152153
options.setTransportGate(new AndroidTransportGate(context, options.getLogger()));

sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.sentry.DateUtils;
2424
import io.sentry.EventProcessor;
2525
import io.sentry.Hint;
26-
import io.sentry.ILogger;
2726
import io.sentry.SentryBaseEvent;
2827
import io.sentry.SentryEvent;
2928
import io.sentry.SentryLevel;
@@ -69,26 +68,29 @@ final class DefaultAndroidEventProcessor implements EventProcessor {
6968

7069
private final @NotNull BuildInfoProvider buildInfoProvider;
7170
private final @NotNull RootChecker rootChecker;
72-
73-
private final @NotNull ILogger logger;
71+
private final @NotNull SentryAndroidOptions options;
7472

7573
public DefaultAndroidEventProcessor(
7674
final @NotNull Context context,
77-
final @NotNull ILogger logger,
78-
final @NotNull BuildInfoProvider buildInfoProvider) {
79-
this(context, logger, buildInfoProvider, new RootChecker(context, buildInfoProvider, logger));
75+
final @NotNull BuildInfoProvider buildInfoProvider,
76+
final @NotNull SentryAndroidOptions options) {
77+
this(
78+
context,
79+
buildInfoProvider,
80+
new RootChecker(context, buildInfoProvider, options.getLogger()),
81+
options);
8082
}
8183

8284
DefaultAndroidEventProcessor(
8385
final @NotNull Context context,
84-
final @NotNull ILogger logger,
8586
final @NotNull BuildInfoProvider buildInfoProvider,
86-
final @NotNull RootChecker rootChecker) {
87+
final @NotNull RootChecker rootChecker,
88+
final @NotNull SentryAndroidOptions options) {
8789
this.context = Objects.requireNonNull(context, "The application context is required.");
88-
this.logger = Objects.requireNonNull(logger, "The Logger is required.");
8990
this.buildInfoProvider =
9091
Objects.requireNonNull(buildInfoProvider, "The BuildInfoProvider is required.");
9192
this.rootChecker = Objects.requireNonNull(rootChecker, "The RootChecker is required.");
93+
this.options = Objects.requireNonNull(options, "The options object is required.");
9294

9395
ExecutorService executorService = Executors.newSingleThreadExecutor();
9496
// dont ref. to method reference, theres a bug on it
@@ -149,10 +151,12 @@ private boolean shouldApplyScopeData(
149151
if (HintUtils.shouldApplyScopeData(hint)) {
150152
return true;
151153
} else {
152-
logger.log(
153-
SentryLevel.DEBUG,
154-
"Event was cached so not applying data relevant to the current app execution/version: %s",
155-
event.getEventId());
154+
options
155+
.getLogger()
156+
.log(
157+
SentryLevel.DEBUG,
158+
"Event was cached so not applying data relevant to the current app execution/version: %s",
159+
event.getEventId());
156160
return false;
157161
}
158162
}
@@ -220,7 +224,7 @@ private void setThreads(final @NotNull SentryEvent event) {
220224

221225
private void setPackageInfo(final @NotNull SentryBaseEvent event, final @NotNull App app) {
222226
final PackageInfo packageInfo =
223-
ContextUtils.getPackageInfo(context, PackageManager.GET_PERMISSIONS, logger);
227+
ContextUtils.getPackageInfo(context, PackageManager.GET_PERMISSIONS, options.getLogger());
224228
if (packageInfo != null) {
225229
String versionCode = ContextUtils.getVersionCode(packageInfo);
226230

@@ -287,7 +291,9 @@ private void setArchitectures(final @NotNull Device device) {
287291

288292
// setting such values require IO hence we don't run for transactions
289293
if (errorEvent) {
290-
setDeviceIO(device, applyScopeData);
294+
if (options.isCollectAdditionalContext()) {
295+
setDeviceIO(device, applyScopeData);
296+
}
291297
}
292298

293299
device.setOrientation(getOrientation());
@@ -298,7 +304,7 @@ private void setArchitectures(final @NotNull Device device) {
298304
device.setSimulator((Boolean) emulator);
299305
}
300306
} catch (Throwable e) {
301-
logger.log(SentryLevel.ERROR, "Error getting emulator.", e);
307+
options.getLogger().log(SentryLevel.ERROR, "Error getting emulator.", e);
302308
}
303309

304310
DisplayMetrics displayMetrics = getDisplayMetrics();
@@ -336,7 +342,7 @@ private void setDeviceIO(final @NotNull Device device, final boolean applyScopeD
336342
}
337343

338344
Boolean connected;
339-
switch (ConnectivityChecker.getConnectionStatus(context, logger)) {
345+
switch (ConnectivityChecker.getConnectionStatus(context, options.getLogger())) {
340346
case NOT_CONNECTED:
341347
connected = false;
342348
break;
@@ -361,7 +367,8 @@ private void setDeviceIO(final @NotNull Device device, final boolean applyScopeD
361367
}
362368

363369
// this way of getting the size of storage might be problematic for storages bigger than 2GB
364-
// check the use of https://developer.android.com/reference/java/io/File.html#getFreeSpace%28%29
370+
// check the use of
371+
// https://developer.android.com/reference/java/io/File.html#getFreeSpace%28%29
365372
final File internalStorageFile = context.getExternalFilesDir(null);
366373
if (internalStorageFile != null) {
367374
StatFs internalStorageStat = new StatFs(internalStorageFile.getPath());
@@ -378,7 +385,7 @@ private void setDeviceIO(final @NotNull Device device, final boolean applyScopeD
378385
if (device.getConnectionType() == null) {
379386
// wifi, ethernet or cellular, null if none
380387
device.setConnectionType(
381-
ConnectivityChecker.getConnectionType(context, logger, buildInfoProvider));
388+
ConnectivityChecker.getConnectionType(context, options.getLogger(), buildInfoProvider));
382389
}
383390
}
384391

@@ -409,7 +416,7 @@ private TimeZone getTimeZone() {
409416
// currentTimeMillis returns UTC already
410417
return DateUtils.getDateTime(System.currentTimeMillis() - SystemClock.elapsedRealtime());
411418
} catch (IllegalArgumentException e) {
412-
logger.log(SentryLevel.ERROR, e, "Error getting the device's boot time.");
419+
options.getLogger().log(SentryLevel.ERROR, e, "Error getting the device's boot time.");
413420
}
414421
return null;
415422
}
@@ -427,10 +434,10 @@ private TimeZone getTimeZone() {
427434
actManager.getMemoryInfo(memInfo);
428435
return memInfo;
429436
}
430-
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
437+
options.getLogger().log(SentryLevel.INFO, "Error getting MemoryInfo.");
431438
return null;
432439
} catch (Throwable e) {
433-
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
440+
options.getLogger().log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
434441
return null;
435442
}
436443
}
@@ -449,7 +456,7 @@ private TimeZone getTimeZone() {
449456
try {
450457
return Build.MODEL.split(" ", -1)[0];
451458
} catch (Throwable e) {
452-
logger.log(SentryLevel.ERROR, "Error getting device family.", e);
459+
options.getLogger().log(SentryLevel.ERROR, "Error getting device family.", e);
453460
return null;
454461
}
455462
}
@@ -472,7 +479,7 @@ private TimeZone getTimeZone() {
472479

473480
return ((float) level / (float) scale) * percentMultiplier;
474481
} catch (Throwable e) {
475-
logger.log(SentryLevel.ERROR, "Error getting device battery level.", e);
482+
options.getLogger().log(SentryLevel.ERROR, "Error getting device battery level.", e);
476483
return null;
477484
}
478485
}
@@ -488,7 +495,7 @@ private TimeZone getTimeZone() {
488495
return plugged == BatteryManager.BATTERY_PLUGGED_AC
489496
|| plugged == BatteryManager.BATTERY_PLUGGED_USB;
490497
} catch (Throwable e) {
491-
logger.log(SentryLevel.ERROR, "Error getting device charging state.", e);
498+
options.getLogger().log(SentryLevel.ERROR, "Error getting device charging state.", e);
492499
return null;
493500
}
494501
}
@@ -500,7 +507,7 @@ private TimeZone getTimeZone() {
500507
return ((float) temperature) / 10; // celsius
501508
}
502509
} catch (Throwable e) {
503-
logger.log(SentryLevel.ERROR, "Error getting battery temperature.", e);
510+
options.getLogger().log(SentryLevel.ERROR, "Error getting battery temperature.", e);
504511
}
505512
return null;
506513
}
@@ -517,13 +524,15 @@ private TimeZone getTimeZone() {
517524
deviceOrientation =
518525
DeviceOrientations.getOrientation(context.getResources().getConfiguration().orientation);
519526
if (deviceOrientation == null) {
520-
logger.log(
521-
SentryLevel.INFO,
522-
"No device orientation available (ORIENTATION_SQUARE|ORIENTATION_UNDEFINED)");
527+
options
528+
.getLogger()
529+
.log(
530+
SentryLevel.INFO,
531+
"No device orientation available (ORIENTATION_SQUARE|ORIENTATION_UNDEFINED)");
523532
return null;
524533
}
525534
} catch (Throwable e) {
526-
logger.log(SentryLevel.ERROR, "Error getting device orientation.", e);
535+
options.getLogger().log(SentryLevel.ERROR, "Error getting device orientation.", e);
527536
}
528537
return deviceOrientation;
529538
}
@@ -539,7 +548,7 @@ private TimeZone getTimeZone() {
539548
long totalBlocks = getBlockCountLong(stat);
540549
return totalBlocks * blockSize;
541550
} catch (Throwable e) {
542-
logger.log(SentryLevel.ERROR, "Error getting total internal storage amount.", e);
551+
options.getLogger().log(SentryLevel.ERROR, "Error getting total internal storage amount.", e);
543552
return null;
544553
}
545554
}
@@ -594,7 +603,9 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
594603
long availableBlocks = getAvailableBlocksLong(stat);
595604
return availableBlocks * blockSize;
596605
} catch (Throwable e) {
597-
logger.log(SentryLevel.ERROR, "Error getting unused internal storage amount.", e);
606+
options
607+
.getLogger()
608+
.log(SentryLevel.ERROR, "Error getting unused internal storage amount.", e);
598609
return null;
599610
}
600611
}
@@ -605,10 +616,10 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
605616
if (path != null) { // && path.canRead()) { canRead() will read return false
606617
return new StatFs(path.getPath());
607618
}
608-
logger.log(SentryLevel.INFO, "Not possible to read external files directory");
619+
options.getLogger().log(SentryLevel.INFO, "Not possible to read external files directory");
609620
return null;
610621
}
611-
logger.log(SentryLevel.INFO, "External storage is not mounted or emulated.");
622+
options.getLogger().log(SentryLevel.INFO, "External storage is not mounted or emulated.");
612623
return null;
613624
}
614625

@@ -649,7 +660,7 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
649660
return file;
650661
}
651662
} else {
652-
logger.log(SentryLevel.INFO, "Not possible to read getExternalFilesDirs");
663+
options.getLogger().log(SentryLevel.INFO, "Not possible to read getExternalFilesDirs");
653664
}
654665
return null;
655666
}
@@ -666,7 +677,7 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
666677
long totalBlocks = getBlockCountLong(stat);
667678
return totalBlocks * blockSize;
668679
} catch (Throwable e) {
669-
logger.log(SentryLevel.ERROR, "Error getting total external storage amount.", e);
680+
options.getLogger().log(SentryLevel.ERROR, "Error getting total external storage amount.", e);
670681
return null;
671682
}
672683
}
@@ -690,7 +701,9 @@ private boolean isExternalStorageMounted() {
690701
long availableBlocks = getAvailableBlocksLong(stat);
691702
return availableBlocks * blockSize;
692703
} catch (Throwable e) {
693-
logger.log(SentryLevel.ERROR, "Error getting unused external storage amount.", e);
704+
options
705+
.getLogger()
706+
.log(SentryLevel.ERROR, "Error getting unused external storage amount.", e);
694707
return null;
695708
}
696709
}
@@ -704,7 +717,7 @@ private boolean isExternalStorageMounted() {
704717
try {
705718
return context.getResources().getDisplayMetrics();
706719
} catch (Throwable e) {
707-
logger.log(SentryLevel.ERROR, "Error getting DisplayMetrics.", e);
720+
options.getLogger().log(SentryLevel.ERROR, "Error getting DisplayMetrics.", e);
708721
return null;
709722
}
710723
}
@@ -726,7 +739,7 @@ private boolean isExternalStorageMounted() {
726739
os.setRooted((Boolean) rooted);
727740
}
728741
} catch (Throwable e) {
729-
logger.log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
742+
options.getLogger().log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
730743
}
731744

732745
return os;
@@ -781,7 +794,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
781794
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
782795
return br.readLine();
783796
} catch (IOException e) {
784-
logger.log(SentryLevel.ERROR, errorMsg, e);
797+
options.getLogger().log(SentryLevel.ERROR, errorMsg, e);
785798
}
786799

787800
return defaultVersion;
@@ -805,7 +818,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
805818
return context.getString(stringId);
806819
}
807820
} catch (Throwable e) {
808-
logger.log(SentryLevel.ERROR, "Error getting application name.", e);
821+
options.getLogger().log(SentryLevel.ERROR, "Error getting application name.", e);
809822
}
810823

811824
return null;
@@ -827,7 +840,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
827840
try {
828841
return Installation.id(context);
829842
} catch (Throwable e) {
830-
logger.log(SentryLevel.ERROR, "Error getting installationId.", e);
843+
options.getLogger().log(SentryLevel.ERROR, "Error getting installationId.", e);
831844
}
832845
return null;
833846
}
@@ -836,7 +849,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
836849
private @Nullable Map<String, String> getSideLoadedInfo() {
837850
String packageName = null;
838851
try {
839-
final PackageInfo packageInfo = ContextUtils.getPackageInfo(context, logger);
852+
final PackageInfo packageInfo = ContextUtils.getPackageInfo(context, options.getLogger());
840853
final PackageManager packageManager = context.getPackageManager();
841854

842855
if (packageInfo != null && packageManager != null) {
@@ -861,7 +874,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
861874
}
862875
} catch (IllegalArgumentException e) {
863876
// it'll never be thrown as we are querying its own App's package.
864-
logger.log(SentryLevel.DEBUG, "%s package isn't installed.", packageName);
877+
options.getLogger().log(SentryLevel.DEBUG, "%s package isn't installed.", packageName);
865878
}
866879

867880
return null;
@@ -879,7 +892,7 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
879892
}
880893
}
881894
} catch (Throwable e) {
882-
logger.log(SentryLevel.ERROR, "Error getting side loaded info.", e);
895+
options.getLogger().log(SentryLevel.ERROR, "Error getting side loaded info.", e);
883896
}
884897
}
885898

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ final class ManifestMetadataReader {
7171

7272
static final String ATTACH_SCREENSHOT = "io.sentry.attach-screenshot";
7373
static final String CLIENT_REPORTS_ENABLE = "io.sentry.send-client-reports";
74+
static final String COLLECT_ADDITIONAL_CONTEXT = "io.sentry.additional-context";
7475

7576
/** ManifestMetadataReader ctor */
7677
private ManifestMetadataReader() {}
@@ -210,6 +211,13 @@ static void applyMetadata(
210211
options.setSendClientReports(
211212
readBool(metadata, logger, CLIENT_REPORTS_ENABLE, options.isSendClientReports()));
212213

214+
options.setCollectAdditionalContext(
215+
readBool(
216+
metadata,
217+
logger,
218+
COLLECT_ADDITIONAL_CONTEXT,
219+
options.isCollectAdditionalContext()));
220+
213221
if (options.getTracesSampleRate() == null) {
214222
final Double tracesSampleRate = readDouble(metadata, logger, TRACES_SAMPLE_RATE);
215223
if (tracesSampleRate != -1) {

0 commit comments

Comments
 (0)