Skip to content
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

Fix: Run event processors and enrich transactions with contexts #1430

Merged
merged 20 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.sentry.DateUtils;
import io.sentry.EventProcessor;
import io.sentry.ILogger;
import io.sentry.SentryBaseEvent;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.android.core.util.ConnectivityChecker;
Expand All @@ -34,6 +35,7 @@
import io.sentry.protocol.Device;
import io.sentry.protocol.OperatingSystem;
import io.sentry.protocol.SentryThread;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.util.ApplyScopeUtils;
import io.sentry.util.Objects;
Expand Down Expand Up @@ -139,35 +141,54 @@ public DefaultAndroidEventProcessor(
@Override
public @NotNull SentryEvent process(
final @NotNull SentryEvent event, final @Nullable Object hint) {
if (ApplyScopeUtils.shouldApplyScopeData(hint)) {
if (shouldApplyScopeData(event, hint)) {
processNonCachedEvent(event);
mergeDebugImages(event);
setThreads(event);
}

setCommons(event, true);

return event;
}

private void setCommons(final @NotNull SentryBaseEvent event, final boolean doIO) {
mergeUser(event);
setDevice(event, doIO);
mergeOS(event);
setSideLoadedInfo(event);
}

private boolean shouldApplyScopeData(
final @NotNull SentryBaseEvent event, final @Nullable Object hint) {
if (ApplyScopeUtils.shouldApplyScopeData(hint)) {
return true;
} else {
logger.log(
SentryLevel.DEBUG,
"Event was cached so not applying data relevant to the current app execution/version: %s",
event.getEventId());
return false;
}
}

private void mergeUser(final @NotNull SentryBaseEvent event) {
// userId should be set even if event is Cached as the userId is static and won't change anyway.
final User user = event.getUser();
if (user == null) {
event.setUser(getDefaultUser());
} else if (user.getId() == null) {
user.setId(getDeviceId());
}
}

private void setDevice(final @NotNull SentryBaseEvent event, final boolean doIO) {
if (event.getContexts().getDevice() == null) {
event.getContexts().setDevice(getDevice());
event.getContexts().setDevice(getDevice(doIO));
}

mergeOS(event);

setSideLoadedInfo(event);

return event;
}

private void mergeOS(final @NotNull SentryEvent event) {
private void mergeOS(final @NotNull SentryBaseEvent event) {
final OperatingSystem currentOS = event.getContexts().getOperatingSystem();
final OperatingSystem androidOS = getOperatingSystem();

Expand All @@ -187,34 +208,42 @@ private void mergeOS(final @NotNull SentryEvent event) {
}

// Data to be applied to events that was created in the running process
private void processNonCachedEvent(final @NotNull SentryEvent event) {
private void processNonCachedEvent(final @NotNull SentryBaseEvent event) {
App app = event.getContexts().getApp();
if (app == null) {
app = new App();
}
setAppExtras(app);

mergeDebugImages(event);

PackageInfo packageInfo = ContextUtils.getPackageInfo(context, logger);
if (packageInfo != null) {
String versionCode = ContextUtils.getVersionCode(packageInfo);

if (event.getDist() == null) {
event.setDist(versionCode);
}
setAppPackageInfo(app, packageInfo);
}
setPackageInfo(event, app);

event.getContexts().setApp(app);
}

private void setThreads(final @NotNull SentryEvent event) {
if (event.getThreads() != null) {
for (SentryThread thread : event.getThreads()) {
thread.setCurrent(MainThreadChecker.isMainThread(thread));
}
}
}

private void setPackageInfo(final @NotNull SentryBaseEvent event, final @NotNull App app) {
final PackageInfo packageInfo = ContextUtils.getPackageInfo(context, logger);
if (packageInfo != null) {
String versionCode = ContextUtils.getVersionCode(packageInfo);

setDist(event, versionCode);
setAppPackageInfo(app, packageInfo);
}
}

private void setDist(final @NotNull SentryBaseEvent event, final @NotNull String versionCode) {
if (event.getDist() == null) {
event.setDist(versionCode);
}
}

private void mergeDebugImages(final @NotNull SentryEvent event) {
final List<DebugImage> debugImages = getDebugImages();
if (debugImages == null) {
Expand Down Expand Up @@ -302,7 +331,7 @@ private void setArchitectures(final @NotNull Device device) {

// we can get some inspiration here
// https://github.com/flutter/plugins/blob/master/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java
private @NotNull Device getDevice() {
private @NotNull Device getDevice(final boolean doIO) {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
// TODO: missing usable memory

Device device = new Device();
Expand All @@ -314,12 +343,51 @@ private void setArchitectures(final @NotNull Device device) {
device.setModelId(Build.ID);
setArchitectures(device);

Intent batteryIntent = getBatteryIntent();
// setting such values require IO hence we don't run for transactions
if (doIO) {
setDeviceIO(device);
}

device.setOrientation(getOrientation());

try {
Object emulator = contextData.get().get(EMULATOR);
if (emulator != null) {
device.setSimulator((Boolean) emulator);
}
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error getting emulator.", e);
}

DisplayMetrics displayMetrics = getDisplayMetrics();
if (displayMetrics != null) {
device.setScreenWidthPixels(displayMetrics.widthPixels);
device.setScreenHeightPixels(displayMetrics.heightPixels);
device.setScreenDensity(displayMetrics.density);
device.setScreenDpi(displayMetrics.densityDpi);
}

device.setBootTime(getBootTime());
device.setTimezone(getTimeZone());

if (device.getId() == null) {
device.setId(getDeviceId());
}
if (device.getLanguage() == null) {
device.setLanguage(Locale.getDefault().toString()); // eg en_US
}

return device;
}

private void setDeviceIO(final @NotNull Device device) {
final Intent batteryIntent = getBatteryIntent();
if (batteryIntent != null) {
device.setBatteryLevel(getBatteryLevel(batteryIntent));
device.setCharging(isCharging(batteryIntent));
device.setBatteryTemperature(getBatteryTemperature(batteryIntent));
}

Boolean connected;
switch (ConnectivityChecker.getConnectionStatus(context, logger)) {
case NOT_CONNECTED:
Expand All @@ -332,18 +400,8 @@ private void setArchitectures(final @NotNull Device device) {
connected = null;
}
device.setOnline(connected);
device.setOrientation(getOrientation());

try {
Object emulator = contextData.get().get(EMULATOR);
if (emulator != null) {
device.setSimulator((Boolean) emulator);
}
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error getting emulator.", e);
}

ActivityManager.MemoryInfo memInfo = getMemInfo();
final ActivityManager.MemoryInfo memInfo = getMemInfo();
if (memInfo != null) {
// in bytes
device.setMemorySize(getMemorySize(memInfo));
Expand All @@ -355,43 +413,24 @@ private void setArchitectures(final @NotNull Device device) {

// this way of getting the size of storage might be problematic for storages bigger than 2GB
// check the use of https://developer.android.com/reference/java/io/File.html#getFreeSpace%28%29
File internalStorageFile = context.getExternalFilesDir(null);
final File internalStorageFile = context.getExternalFilesDir(null);
if (internalStorageFile != null) {
StatFs internalStorageStat = new StatFs(internalStorageFile.getPath());
device.setStorageSize(getTotalInternalStorage(internalStorageStat));
device.setFreeStorage(getUnusedInternalStorage(internalStorageStat));
}

StatFs externalStorageStat = getExternalStorageStat(internalStorageFile);
final StatFs externalStorageStat = getExternalStorageStat(internalStorageFile);
if (externalStorageStat != null) {
device.setExternalStorageSize(getTotalExternalStorage(externalStorageStat));
device.setExternalFreeStorage(getUnusedExternalStorage(externalStorageStat));
}

DisplayMetrics displayMetrics = getDisplayMetrics();
if (displayMetrics != null) {
device.setScreenWidthPixels(displayMetrics.widthPixels);
device.setScreenHeightPixels(displayMetrics.heightPixels);
device.setScreenDensity(displayMetrics.density);
device.setScreenDpi(displayMetrics.densityDpi);
}

device.setBootTime(getBootTime());
device.setTimezone(getTimeZone());

if (device.getId() == null) {
device.setId(getDeviceId());
}
if (device.getLanguage() == null) {
device.setLanguage(Locale.getDefault().toString()); // eg en_US
}
if (device.getConnectionType() == null) {
// wifi, ethernet or cellular, null if none
device.setConnectionType(
ConnectivityChecker.getConnectionType(context, logger, buildInfoProvider));
}

return device;
}

@SuppressWarnings("ObsoleteSdkInt")
Expand Down Expand Up @@ -949,7 +988,7 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
}

@SuppressWarnings("unchecked")
private void setSideLoadedInfo(final @NotNull SentryEvent event) {
private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
try {
final Object sideLoadedInfo = contextData.get().get(SIDE_LOADED);

Expand All @@ -963,4 +1002,16 @@ private void setSideLoadedInfo(final @NotNull SentryEvent event) {
logger.log(SentryLevel.ERROR, "Error getting side loaded info.", e);
}
}

@Override
public @Nullable SentryTransaction process(
final @NotNull SentryTransaction transaction, final @Nullable Object hint) {
if (shouldApplyScopeData(transaction, hint)) {
processNonCachedEvent(transaction);
}

setCommons(transaction, false);

return transaction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.sentry.EventProcessor;
import io.sentry.SentryEvent;
import io.sentry.protocol.SentryRuntime;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;

Expand All @@ -25,7 +26,7 @@ public CustomEventProcessor() {
}

@Override
public SentryEvent process(SentryEvent event, @Nullable Object hint) {
public SentryEvent process(@NotNull SentryEvent event, @Nullable Object hint) {
final SentryRuntime runtime = new SentryRuntime();
runtime.setVersion(javaVersion);
runtime.setName(javaVendor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

// TODO: check if I need to support process(SentryTransaction) here
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
public final class SentryUserProviderEventProcessor implements EventProcessor {
private final @NotNull SentryOptions options;
private final @NotNull SentryUserProvider sentryUserProvider;
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/EventProcessor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.sentry;

import io.sentry.protocol.SentryTransaction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface EventProcessor {
@Nullable
SentryEvent process(@NotNull SentryEvent event, @Nullable Object hint);

@Nullable
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
default SentryTransaction process(@NotNull SentryTransaction transaction, @Nullable Object hint) {
return transaction;
}
}
Loading