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

Disable Background Task Instrumentation #614

Merged
merged 8 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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,50 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.splunk.rum;

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

public final class BackgroundProcessDetector {

private BackgroundProcessDetector() {}

public static Boolean isBackgroundProcess(String applicationId) {
String applicationProcessName = getApplicationProcessName();
return Objects.equals(applicationProcessName, applicationId);
}

private static String getApplicationProcessName() {
if (Build.VERSION.SDK_INT >= 28) {
return Application.getProcessName();
} else {
try {
@SuppressLint("PrivateApi")
Class<?> activityThread = Class.forName("android.app.ActivityThread");
String methodName = "currentProcessName";
@SuppressLint("PrivateApi")
Method getProcessName = activityThread.getDeclaredMethod(methodName);
return (String) getProcessName.invoke(null);
} catch (Exception e) {
return null;
}
}
}
}
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
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.isBackgroundTaskReportingDisabled() && builder.isBackgroundProcess) {
INSTANCE = SplunkRum.noop();
} 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 @@ -47,6 +47,7 @@ public final class SplunkRumBuilder {
int maxUsageMegabytes = DEFAULT_MAX_STORAGE_USE_MB;
boolean sessionBasedSamplerEnabled = false;
double sessionBasedSamplerRatio = 1.0;
boolean isBackgroundProcess = false;

/**
* Sets the application name that will be used to identify your application in the Splunk RUM
Expand Down Expand Up @@ -314,6 +315,20 @@ public SplunkRum build(Application application) {
return SplunkRum.initialize(this, application, CurrentNetworkProvider::createAndStart);
}

/**
* Disables the instrumentation of background process feature. If enabled, the background
* processes will be instrumented.
*
* <p>This feature is enabled by default. You can disable it by calling this method.
*
* @return {@code this}
*/
public SplunkRumBuilder disableBackgroundTaskReporting(String applicationId) {
isBackgroundProcess = BackgroundProcessDetector.isBackgroundProcess(applicationId);
configFlags.disableBackgroundTaskDetection();
return this;
}

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

boolean isBackgroundTaskReportingDisabled() {
return !configFlags.isBackgroundTaskInstrumentationEnabled();
}
}