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

Introduce a SplunkRumBuilder class and deprecate Config #342

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -19,7 +19,6 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import android.app.Application;
import com.splunk.rum.Config;
import com.splunk.rum.SplunkRum;
import com.splunk.rum.StandardAttributes;
import io.opentelemetry.api.common.Attributes;
Expand All @@ -35,42 +34,35 @@ public class SampleApplication extends Application {
public void onCreate() {
super.onCreate();

Config config =
SplunkRum.newConfigBuilder()
// note: for these values to be resolved, put them in your local.properties
// file as
// rum.beacon.url and rum.access.token
.realm(getResources().getString(R.string.rum_realm))
.slowRenderingDetectionPollInterval(Duration.ofMillis(1000))
.rumAccessToken(getResources().getString(R.string.rum_access_token))
.applicationName("Android Demo App")
.debugEnabled(true)
.diskBufferingEnabled(true)
.deploymentEnvironment("demo")
.limitDiskUsageMegabytes(1)
.globalAttributes(
Attributes.builder()
.put("vendor", "Splunk")
.put(
StandardAttributes.APP_VERSION,
BuildConfig.VERSION_NAME)
.build())
.filterSpans(
spanFilter ->
spanFilter
.removeSpanAttribute(stringKey("http.user_agent"))
.rejectSpansByName(
spanName -> spanName.contains("ignored"))
// sensitive data in the login http.url attribute
// will be redacted before it hits the exporter
.replaceSpanAttribute(
StandardAttributes.HTTP_URL,
value ->
HTTP_URL_SENSITIVE_DATA_PATTERN
.matcher(value)
.replaceAll(
"$1=<redacted>")))
.build();
SplunkRum.initialize(config, this);
SplunkRum.builder()
// note: for these values to be resolved, put them in your local.properties
// file as rum.beacon.url and rum.access.token
.setRealm(getResources().getString(R.string.rum_realm))
.setApplicationName("Android Demo App")
.setRumAccessToken(getResources().getString(R.string.rum_access_token))
.enableDebug(true)
.enableDiskBuffering(true)
.setSlowRenderingDetectionPollInterval(Duration.ofMillis(1000))
.setDeploymentEnvironment("demo")
.limitDiskUsageMegabytes(1)
.setGlobalAttributes(
Attributes.builder()
.put("vendor", "Splunk")
.put(StandardAttributes.APP_VERSION, BuildConfig.VERSION_NAME)
.build())
.filterSpans(
spanFilter ->
spanFilter
.removeSpanAttribute(stringKey("http.user_agent"))
.rejectSpansByName(spanName -> spanName.contains("ignored"))
// sensitive data in the login http.url attribute
// will be redacted before it hits the exporter
.replaceSpanAttribute(
StandardAttributes.HTTP_URL,
value ->
HTTP_URL_SENSITIVE_DATA_PATTERN
.matcher(value)
.replaceAll("$1=<redacted>")))
.build(this);
}
}
39 changes: 38 additions & 1 deletion splunk-otel-android/src/main/java/com/splunk/rum/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
*
* <p>Both the beaconUrl and the rumAuthToken are mandatory configuration settings. Trying to build
* a Config instance without both of these items specified will result in an exception being thrown.
*
* @deprecated Use {@link #builder()} and the {@link SplunkRumBuilder} to configure a {@link
* SplunkRum} instance.
*/
@Deprecated
public class Config {

private final String beaconEndpoint;
Expand All @@ -43,6 +47,7 @@ public class Config {
private final boolean anrDetectionEnabled;
private final Attributes globalAttributes;
private final Function<SpanExporter, SpanExporter> spanFilterExporterDecorator;
private final Consumer<SpanFilterBuilder> spanFilterBuilderConfigurer;
private final boolean slowRenderingDetectionEnabled;
private final Duration slowRenderingDetectionPollInterval;
private final boolean diskBufferingEnabled;
Expand All @@ -62,6 +67,7 @@ private Config(Builder builder) {
this.slowRenderingDetectionPollInterval = builder.slowRenderingDetectionPollInterval;
this.slowRenderingDetectionEnabled = builder.slowRenderingDetectionEnabled;
this.spanFilterExporterDecorator = builder.spanFilterBuilder.build();
this.spanFilterBuilderConfigurer = builder.spanFilterBuilderConfigurer;
this.diskBufferingEnabled = builder.diskBufferingEnabled;
this.maxUsageMegabytes = builder.maxUsageMegabytes;
this.sessionBasedSamplerEnabled = builder.sessionBasedSamplerEnabled;
Expand Down Expand Up @@ -174,7 +180,35 @@ SpanExporter decorateWithSpanFilter(SpanExporter exporter) {
return spanFilterExporterDecorator.apply(exporter);
}

/** Builder class for the Splunk RUM {@link Config} class. */
SplunkRumBuilder toSplunkRumBuilder() {
SplunkRumBuilder splunkRumBuilder =
new SplunkRumBuilder()
.setApplicationName(applicationName)
.setBeaconEndpoint(beaconEndpoint)
.setRumAccessToken(rumAccessToken)
.enableDebug(debugEnabled)
.enableDiskBuffering(diskBufferingEnabled)
.disableCrashReporting(!crashReportingEnabled)
.disableNetworkMonitorEnabled(!networkMonitorEnabled)
.disableAnrDetection(!anrDetectionEnabled)
.disableSlowRenderingDetection(!slowRenderingDetectionEnabled)
.setSlowRenderingDetectionPollInterval(slowRenderingDetectionPollInterval)
.setGlobalAttributes(globalAttributes)
.filterSpans(spanFilterBuilderConfigurer)
.limitDiskUsageMegabytes(maxUsageMegabytes);
if (sessionBasedSamplerEnabled) {
splunkRumBuilder.enableSessionBasedSampling(sessionBasedSamplerRatio);
}
return splunkRumBuilder;
}

/**
* Builder class for the Splunk RUM {@link Config} class.
*
* @deprecated Use {@link #builder()} and the {@link SplunkRumBuilder} to configure a {@link
* SplunkRum} instance.
*/
@Deprecated
public static class Builder {

private static final Duration DEFAULT_SLOW_RENDERING_DETECTION_POLL_INTERVAL =
Expand All @@ -192,6 +226,7 @@ public static class Builder {
private Attributes globalAttributes = Attributes.empty();
private String deploymentEnvironment;
private final SpanFilterBuilder spanFilterBuilder = new SpanFilterBuilder();
private Consumer<SpanFilterBuilder> spanFilterBuilderConfigurer = f -> {};
private String realm;
private Duration slowRenderingDetectionPollInterval =
DEFAULT_SLOW_RENDERING_DETECTION_POLL_INTERVAL;
Expand Down Expand Up @@ -379,6 +414,8 @@ public Builder deploymentEnvironment(String environment) {
* @return {@code this}.
*/
public Builder filterSpans(Consumer<SpanFilterBuilder> configurer) {
Consumer<SpanFilterBuilder> previous = this.spanFilterBuilderConfigurer;
this.spanFilterBuilderConfigurer = previous.andThen(configurer);
configurer.accept(spanFilterBuilder);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.splunk.rum;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
Expand Down Expand Up @@ -137,4 +139,16 @@ public void onLost(@NonNull Network network) {
}
}
}

static class Factory {

ConnectionUtil create(Application application) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a method comment to explain to the caller that the returned util has already been started monitoring? Or could rename to createAndStart() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or could rename to createAndStart()

👍

Context context = application.getApplicationContext();
ConnectionUtil connectionUtil = new ConnectionUtil(NetworkDetector.create(context));
connectionUtil.startMonitoring(
ConnectionUtil::createNetworkMonitoringRequest,
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectionUtil;
}
}
}
Loading