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

Add new experimental api to customize the currently visible screen name. #857

Closed
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 @@ -80,6 +80,7 @@ public void onCreate() {
return chain.proceed(requestBuilder.build());
});
})
.setExperimentalCurrentScreenCustomizer(vs -> () -> "customized: " + vs.get())
.build(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.splunk.rum.incubating.CurrentlyVisibleScreen;
import com.splunk.rum.incubating.internal.CurrentlyVisibleScreenAttributeSpanProcessor;
import com.splunk.rum.internal.GlobalAttributesSupplier;
import com.splunk.rum.internal.UInt32QuadXorTraceIdRatioSampler;
import io.opentelemetry.android.OpenTelemetryRum;
Expand All @@ -52,6 +54,7 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;
import io.opentelemetry.sdk.trace.SpanLimits;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
Expand Down Expand Up @@ -97,6 +100,9 @@ SplunkRum initialize(Looper mainLooper) {
if (!builder.isNetworkMonitorEnabled()) {
config.disableNetworkChangeMonitoring();
}
if (builder.hasExperimentalVisibleScreenCustomization()) {
config.disableScreenAttributes();
}

OpenTelemetryRumBuilder otelRumBuilder = OpenTelemetryRum.builder(application, config);

Expand Down Expand Up @@ -193,6 +199,10 @@ SplunkRum initialize(Looper mainLooper) {
installCrashReporter(otelRumBuilder);
}

if (builder.hasExperimentalVisibleScreenCustomization()) {
customizeCurrentlyVisibleScreen(otelRumBuilder, visibleScreenTracker);
}

// Lifecycle events instrumentation are always installed.
installLifecycleInstrumentations(otelRumBuilder, visibleScreenTracker);

Expand All @@ -207,6 +217,24 @@ SplunkRum initialize(Looper mainLooper) {
return new SplunkRum(openTelemetryRum, globalAttributeSupplier);
}

private void customizeCurrentlyVisibleScreen(
OpenTelemetryRumBuilder otelRumBuilder, VisibleScreenTracker visibleScreenTracker) {
if (builder.experimentalCurrentScreenCustomizer == null) {
return;
}
Function<CurrentlyVisibleScreen, CurrentlyVisibleScreen> customizer =
builder.experimentalCurrentScreenCustomizer;
otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
CurrentlyVisibleScreen visibleScreen =
visibleScreenTracker::getCurrentlyVisibleScreen;
CurrentlyVisibleScreen customized = customizer.apply(visibleScreen);
SpanProcessor screenAttributesAppender =
new CurrentlyVisibleScreenAttributeSpanProcessor(customized);
return tracerProviderBuilder.addSpanProcessor(screenAttributesAppender);
});
}

@NonNull
private MemorySpanBuffer constructBacklogProvider(VisibleScreenTracker visibleScreenTracker) {
if (builder.isBackgroundInstrumentationDeferredUntilForeground()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import android.app.Application;
import android.util.Log;
import androidx.annotation.Nullable;
import com.splunk.rum.incubating.CurrentlyVisibleScreen;
import com.splunk.rum.incubating.HttpSenderCustomizer;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.time.Duration;
import java.util.function.Consumer;
import java.util.function.Function;

/** A builder of {@link SplunkRum}. */
public final class SplunkRumBuilder {
Expand All @@ -50,6 +52,9 @@ public final class SplunkRumBuilder {
double sessionBasedSamplerRatio = 1.0;
boolean isSubprocess = false;

@Nullable
Function<CurrentlyVisibleScreen, CurrentlyVisibleScreen> experimentalCurrentScreenCustomizer;

/**
* Sets the application name that will be used to identify your application in the Splunk RUM
* UI.
Expand Down Expand Up @@ -394,6 +399,22 @@ public SplunkRumBuilder enableExperimentalOtlpExporter() {
return this;
}

/**
* This method can be used to provide a customer that is able of altering the value of the
* "current screen". Typically, SplunkRum will track Fragment and Activity state changes to
* automatically determine the name of the current "screen". By passing a customizer Function
* here, you can override the default behavior by customizing (at initialization time) the
* instance of CurrentlyVisibleScreen that will be used when setting the screen.name attribute
* on telemetry. Note that this does not alter the screen.name assigned to spans created by the
* ActivityTracer and FragmentTracer. Those will continue to mirror the name of the Activity or
* Fragment. Status: Experimental. API is subject to potential change or removal in the future.
*/
public SplunkRumBuilder setExperimentalCurrentScreenCustomizer(
Function<CurrentlyVisibleScreen, CurrentlyVisibleScreen> customizer) {
this.experimentalCurrentScreenCustomizer = customizer;
return this;
}

// one day maybe these can use kotlin delegation
ConfigFlags getConfigFlags() {
return configFlags;
Expand Down Expand Up @@ -444,4 +465,8 @@ boolean isSubprocessInstrumentationDisabled() {
boolean isBackgroundInstrumentationDeferredUntilForeground() {
return configFlags.isBackgroundInstrumentationDeferredUntilForeground();
}

public boolean hasExperimentalVisibleScreenCustomization() {
return experimentalCurrentScreenCustomizer != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.incubating;

import java.util.function.Supplier;

/** Experimental interface, used to return the name of the currently visible screen. */
public interface CurrentlyVisibleScreen extends Supplier<String> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.incubating.internal;

import static io.opentelemetry.android.RumConstants.SCREEN_NAME_KEY;

import com.splunk.rum.incubating.CurrentlyVisibleScreen;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

/**
* Experimental SpanProcessor that sets the screen.name attribute on all Spans. It obtains the
* screen name from an instance of CurrentlyVisibleScreen, and exists primarily as a copy of
* ScreenAttributesSpanProcessor from upstream, without the problems of coupling to the
* VisibleScreenTracker.
*/
public final class CurrentlyVisibleScreenAttributeSpanProcessor implements SpanProcessor {

private final CurrentlyVisibleScreen visibleScreen;

public CurrentlyVisibleScreenAttributeSpanProcessor(CurrentlyVisibleScreen visibleScreen) {
this.visibleScreen = visibleScreen;
}

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
String currentScreen = visibleScreen.get();
span.setAttribute(SCREEN_NAME_KEY, currentScreen);
}

@Override
public boolean isStartRequired() {
return true;
}

@Override
public void onEnd(ReadableSpan span) {
// nop
}

@Override
public boolean isEndRequired() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Looper;
import com.splunk.rum.incubating.CurrentlyVisibleScreen;
import com.splunk.rum.incubating.HttpSenderCustomizer;
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.network.CurrentNetwork;
Expand All @@ -52,6 +53,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -258,6 +260,40 @@ void canCustomizeHttpSender() {
assertNotNull(seenBuilder.get());
}

@Test
void canCustomizeCurrentScreenName() {
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
Function<CurrentlyVisibleScreen, CurrentlyVisibleScreen> customizer =
cvs -> () -> "custom:" + cvs.get();
SplunkRumBuilder splunkRumBuilder =
new SplunkRumBuilder()
.setRealm("us0")
.setRumAccessToken("secret!")
.setApplicationName("test")
.disableAnrDetection()
.setExperimentalCurrentScreenCustomizer(customizer);

when(application.getApplicationContext()).thenReturn(context);
when(application.getMainLooper()).thenReturn(mainLooper);

RumInitializer testInitializer =
new RumInitializer(splunkRumBuilder, application, new AppStartupTimer()) {
@Override
SpanExporter getCoreSpanExporter() {
return testExporter;
}
};
SplunkRum rum = testInitializer.initialize(mainLooper);
rum.addRumEvent("foo", Attributes.empty()); // need to trigger export
rum.flushSpans();

List<SpanData> spans = testExporter.getFinishedSpanItems();
assertThat(spans.size()).isEqualTo(1);
assertThat(spans.get(0).getName()).isEqualTo("foo");
assertThat(spans.get(0).getAttributes().get(stringKey("screen.name")))
.isEqualTo("custom:unknown");
}

@Test
void shouldTranslateExceptionEventsToSpanAttributes() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
Expand Down
Loading