Skip to content

Commit

Permalink
Added logic to disable instrumentation for background task
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhimaheshwari committed Aug 21, 2023
1 parent f1bb12e commit bb8a5ef
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void onCreate() {
.setRumAccessToken(getResources().getString(R.string.rum_access_token))
.enableDebug()
.enableDiskBuffering()
.disableBackgroundTaskReporting(BuildConfig.APPLICATION_ID)
.setSlowRenderingDetectionPollInterval(Duration.ofMillis(1000))
.setDeploymentEnvironment("demo")
.limitDiskUsageMegabytes(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.splunk.rum;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Build;
import java.lang.reflect.Method;

public class BackgroundProcessDetector {
public static Boolean isBackgroundProcess(String applicationId) {
String applicationProcessName = "";
if (Build.VERSION.SDK_INT >= 28)
applicationProcessName = Application.getProcessName();
else {
try {
@SuppressLint("PrivateApi")
Class<?> activityThread = Class.forName("android.app.ActivityThread");
String methodName = "currentProcessName";
@SuppressLint("PrivateApi") Method getProcessName = activityThread.getDeclaredMethod(methodName);
applicationProcessName = (String) getProcessName.invoke(null);
} catch (Exception e) {
}
}

return applicationProcessName != null && applicationProcessName.equals(applicationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ConfigFlags {
private boolean networkMonitorEnabled = true;
private boolean anrDetectionEnabled = true;
private boolean slowRenderingDetectionEnabled = true;
private boolean backgroundTaskInstrumentationEnabled = true;

void enableDebug() {
debugEnabled = true;
Expand Down Expand Up @@ -55,10 +56,18 @@ void disableSlowRenderingDetection() {
slowRenderingDetectionEnabled = false;
}

public void disableBackgroundTaskDetection() {
backgroundTaskInstrumentationEnabled = false;
}

boolean isDebugEnabled() {
return debugEnabled;
}

boolean isBackgroundTaskInstrumentationEnabled() {
return backgroundTaskInstrumentationEnabled;
}

boolean isAnrDetectionEnabled() {
return anrDetectionEnabled;
}
Expand Down
10 changes: 7 additions & 3 deletions splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ static SplunkRum initialize(
return INSTANCE;
}

INSTANCE =
new RumInitializer(builder, application, startupTimer)
.initialize(currentNetworkProviderFactory, Looper.getMainLooper());
if(builder.isBackgroundTaskReportingEnabled()) {
INSTANCE = NoOpSplunkRum.INSTANCE;
} else {
INSTANCE =
new RumInitializer(builder, application, startupTimer)
.initialize(currentNetworkProviderFactory, Looper.getMainLooper());
}

if (builder.isDebugEnabled()) {
Log.i(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ public SplunkRum build(Application application) {
return SplunkRum.initialize(this, application, CurrentNetworkProvider::createAndStart);
}

public SplunkRumBuilder disableBackgroundTaskReporting(String applicationId) {
Boolean isBackgroundProcess = BackgroundProcessDetector.isBackgroundProcess(applicationId);
if(isBackgroundProcess) {
configFlags.disableBackgroundTaskDetection();
}
return this;
}

// one day maybe these can use kotlin delegation
ConfigFlags getConfigFlags() {
return configFlags;
Expand Down Expand Up @@ -350,4 +358,8 @@ boolean isDiskBufferingEnabled() {
boolean isReactNativeSupportEnabled() {
return configFlags.isReactNativeSupportEnabled();
}

boolean isBackgroundTaskReportingEnabled() {
return configFlags.isBackgroundTaskInstrumentationEnabled();
}
}

0 comments on commit bb8a5ef

Please sign in to comment.