Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmalinowski committed Jun 7, 2024
1 parent 1bc3c4c commit 7828651
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 9 deletions.
7 changes: 6 additions & 1 deletion sample-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".JetpackComposeActivity"
android:theme="@style/Theme.SplunkRUMSampleApp.NoActionBar"
android:label="@string/app_name"
android:exported="true">
</activity>
<service
android:name=".SplunkBackgroundService"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static io.opentelemetry.api.common.AttributeKey.longKey;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -80,6 +81,10 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment));

binding.buttonScreenName.setOnClickListener(v -> {
SplunkRum.getInstance().setScreenName("custom-screen");
});

binding.crash.setOnClickListener(v -> multiThreadCrashing());

binding.httpMe.setOnClickListener(
Expand Down Expand Up @@ -184,6 +189,8 @@ public LiveData<String> getSessionId() {
public void onDestroyView() {
super.onDestroyView();
binding = null;

SplunkRum.getInstance().setScreenName(null);
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions sample-app/src/main/res/layout/fragment_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
android:layout_height="wrap_content"
android:text="@string/next" />

<Button
android:id="@+id/button_screen_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/screen_name" />

<Button
android:id="@+id/crash"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions sample-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="second_fragment_label">Second Fragment</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="screen_name">Custom screen name</string>

<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.splunk.rum;

import androidx.annotation.Nullable;

import java.util.concurrent.atomic.AtomicReference;

import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;

public class ExplicitVisibleScreenNameTracker extends VisibleScreenTracker {
private final AtomicReference<String> lastScreenName = new AtomicReference<>();
private final AtomicReference<String> previouslyLastScreenName = new AtomicReference<>();

public void setExplicitScreenName(String screenName) {
this.previouslyLastScreenName.set(this.lastScreenName.get());
this.lastScreenName.set(screenName);
}

@Nullable
@Override
public String getPreviouslyVisibleScreen() {
String screenName = previouslyLastScreenName.get();
if (screenName != null) {
return screenName;
}

return super.getPreviouslyVisibleScreen();
}

@Override
public String getCurrentlyVisibleScreen() {
String screenName = lastScreenName.get();
if (screenName != null) {
return screenName;
}

return super.getCurrentlyVisibleScreen();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NoOpSplunkRum extends SplunkRum {
// passing null values here is fine, they'll never get used anyway
@SuppressWarnings("NullAway")
private NoOpSplunkRum() {
super(OpenTelemetryRum.noop(), null);
super(OpenTelemetryRum.noop(), null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.OpenTelemetryRumBuilder;
import io.opentelemetry.android.RuntimeDetailsExtractor;
import io.opentelemetry.android.ScreenAttributesSpanProcessor;
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.anr.AnrDetector;
Expand All @@ -52,6 +53,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 @@ -86,7 +88,7 @@ class RumInitializer {
}

SplunkRum initialize(Looper mainLooper) {
VisibleScreenTracker visibleScreenTracker = new VisibleScreenTracker();
ExplicitVisibleScreenNameTracker visibleScreenTracker = new ExplicitVisibleScreenNameTracker();

initializationEvents.begin();

Expand All @@ -98,6 +100,7 @@ SplunkRum initialize(Looper mainLooper) {
config.disableNetworkChangeMonitoring();
}

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

otelRumBuilder.mergeResource(createSplunkResource());
Expand Down Expand Up @@ -193,6 +196,13 @@ SplunkRum initialize(Looper mainLooper) {
installCrashReporter(otelRumBuilder);
}

otelRumBuilder.addTracerProviderCustomizer(
(tracerProviderBuilder, app) -> {
SpanProcessor screenAttributesAppender =
new ScreenAttributesSpanProcessor(visibleScreenTracker);
return tracerProviderBuilder.addSpanProcessor(screenAttributesAppender);
});

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

Expand All @@ -204,7 +214,7 @@ SplunkRum initialize(Looper mainLooper) {
builder.getConfigFlags(),
openTelemetryRum.getOpenTelemetry().getTracer(RUM_TRACER_NAME));

return new SplunkRum(openTelemetryRum, globalAttributeSupplier);
return new SplunkRum(openTelemetryRum, globalAttributeSupplier, visibleScreenTracker);
}

@NonNull
Expand Down
17 changes: 16 additions & 1 deletion splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.Nullable;
import com.splunk.rum.internal.GlobalAttributesSupplier;
import io.opentelemetry.android.OpenTelemetryRum;
import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker;
import io.opentelemetry.android.instrumentation.startup.AppStartupTimer;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
Expand Down Expand Up @@ -72,15 +73,17 @@ public class SplunkRum {

private final OpenTelemetryRum openTelemetryRum;
private final GlobalAttributesSupplier globalAttributes;
private final ExplicitVisibleScreenNameTracker visibleScreenTracker;

static {
Handler handler = new Handler(Looper.getMainLooper());
startupTimer.detectBackgroundStart(handler);
}

SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSupplier globalAttributes) {
SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSupplier globalAttributes, ExplicitVisibleScreenNameTracker visibleScreenTracker) {
this.openTelemetryRum = openTelemetryRum;
this.globalAttributes = globalAttributes;
this.visibleScreenTracker = visibleScreenTracker;
}

/** Creates a new {@link SplunkRumBuilder}, used to set up a {@link SplunkRum} instance. */
Expand Down Expand Up @@ -113,6 +116,18 @@ static SplunkRum initialize(SplunkRumBuilder builder, Application application) {
return INSTANCE;
}

public void setScreenName(String screenName) {
if (visibleScreenTracker != null) {
visibleScreenTracker.setExplicitScreenName(screenName);
}

getTracer()
.spanBuilder("Created")
.setAttribute(COMPONENT_KEY, "ui")
.startSpan()
.end();
}

/** Returns {@code true} if the Splunk RUM library has been successfully initialized. */
public static boolean isInitialized() {
return INSTANCE != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void nonNullMethods() {
void addEvent() {
when(openTelemetryRum.getOpenTelemetry()).thenReturn(otelTesting.getOpenTelemetry());

SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes);
SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes, null);

Attributes attributes = Attributes.of(stringKey("one"), "1", longKey("two"), 2L);
splunkRum.addRumEvent("foo", attributes);
Expand All @@ -166,7 +166,7 @@ void addException() {

when(openTelemetryRum.getOpenTelemetry()).thenReturn(testSdk);

SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes);
SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes, null);

NullPointerException exception = new NullPointerException("oopsie");
Attributes attributes = Attributes.of(stringKey("one"), "1", longKey("two"), 2L);
Expand Down Expand Up @@ -197,7 +197,7 @@ private OpenTelemetrySdk buildTestSdk(InMemorySpanExporter testExporter) {
void createAndEnd() {
when(openTelemetryRum.getOpenTelemetry()).thenReturn(otelTesting.getOpenTelemetry());

SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes);
SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes, null);

Span span = splunkRum.startWorkflow("workflow");
Span inner = tracer.spanBuilder("foo").startSpan();
Expand Down Expand Up @@ -255,7 +255,7 @@ void updateLocation() {
.when(globalAttributes)
.update(isA(Consumer.class));

SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes);
SplunkRum splunkRum = new SplunkRum(openTelemetryRum, globalAttributes, null);

Location location = mock(Location.class);
when(location.getLatitude()).thenReturn(42d);
Expand Down

0 comments on commit 7828651

Please sign in to comment.