Skip to content

Commit

Permalink
Introduce a SplunkRumBuilder class and deprecate Config (#342)
Browse files Browse the repository at this point in the history
* Introduce a SplunkRumBuilder class and deprecate Config

* code review comments
  • Loading branch information
Mateusz Rzeszutek authored Sep 13, 2022
1 parent b7be663 commit 7eb5047
Show file tree
Hide file tree
Showing 11 changed files with 641 additions and 167 deletions.
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()
.enableDiskBuffering()
.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);
}
}
52 changes: 49 additions & 3 deletions 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 @@ -170,11 +176,48 @@ public static Builder builder() {
return new Builder();
}

SpanExporter decorateWithSpanFilter(SpanExporter exporter) {
return spanFilterExporterDecorator.apply(exporter);
SplunkRumBuilder toSplunkRumBuilder() {
SplunkRumBuilder splunkRumBuilder =
new SplunkRumBuilder()
.setApplicationName(applicationName)
.setBeaconEndpoint(beaconEndpoint)
.setRumAccessToken(rumAccessToken);
if (debugEnabled) {
splunkRumBuilder.enableDebug();
}
if (diskBufferingEnabled) {
splunkRumBuilder.enableDiskBuffering();
}
if (!crashReportingEnabled) {
splunkRumBuilder.disableCrashReporting();
}
if (!networkMonitorEnabled) {
splunkRumBuilder.disableNetworkMonitorEnabled();
}
if (!anrDetectionEnabled) {
splunkRumBuilder.disableAnrDetection();
}
if (!slowRenderingDetectionEnabled) {
splunkRumBuilder.disableSlowRenderingDetection();
}
splunkRumBuilder
.setSlowRenderingDetectionPollInterval(slowRenderingDetectionPollInterval)
.setGlobalAttributes(globalAttributes)
.filterSpans(spanFilterBuilderConfigurer)
.limitDiskUsageMegabytes(maxUsageMegabytes);
if (sessionBasedSamplerEnabled) {
splunkRumBuilder.enableSessionBasedSampling(sessionBasedSamplerRatio);
}
return splunkRumBuilder;
}

/** Builder class for the Splunk RUM {@link Config} class. */
/**
* 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 +235,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 +423,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 createAndStart(Application application) {
Context context = application.getApplicationContext();
ConnectionUtil connectionUtil = new ConnectionUtil(NetworkDetector.create(context));
connectionUtil.startMonitoring(
ConnectionUtil::createNetworkMonitoringRequest,
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectionUtil;
}
}
}
Loading

0 comments on commit 7eb5047

Please sign in to comment.