From da903aa71c003c095bd1d33d9cd1fe0aae295d3f Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Fri, 8 Dec 2023 09:45:50 -0800 Subject: [PATCH] Leverage upstream improvements (#710) * use GlobalAttributeSpanAppender via upstream mechanism * let upstream handle global attributes * let upstream handle screen attributes * spotless * spotless --- .../java/com/splunk/rum/RumInitializer.java | 30 ++++------- .../main/java/com/splunk/rum/SplunkRum.java | 6 +-- .../internal/GlobalAttributesSupplier.java | 53 +++++++++++++++++++ .../java/com/splunk/rum/SplunkRumTest.java | 6 +-- .../GlobalAttributesSupplierTest.java | 41 ++++++++++++++ 5 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 splunk-otel-android/src/main/java/com/splunk/rum/internal/GlobalAttributesSupplier.java create mode 100644 splunk-otel-android/src/test/java/com/splunk/rum/internal/GlobalAttributesSupplierTest.java diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/RumInitializer.java b/splunk-otel-android/src/main/java/com/splunk/rum/RumInitializer.java index 3da85465..b8e05336 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/RumInitializer.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/RumInitializer.java @@ -31,11 +31,12 @@ import android.os.Looper; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.splunk.rum.internal.GlobalAttributesSupplier; import com.splunk.rum.internal.UInt32QuadXorTraceIdRatioSampler; -import io.opentelemetry.android.GlobalAttributesSpanAppender; import io.opentelemetry.android.OpenTelemetryRum; import io.opentelemetry.android.OpenTelemetryRumBuilder; import io.opentelemetry.android.RuntimeDetailsExtractor; +import io.opentelemetry.android.config.OtelRumConfig; import io.opentelemetry.android.instrumentation.activity.VisibleScreenTracker; import io.opentelemetry.android.instrumentation.anr.AnrDetector; import io.opentelemetry.android.instrumentation.crash.CrashReporter; @@ -92,7 +93,13 @@ SplunkRum initialize( VisibleScreenTracker visibleScreenTracker = new VisibleScreenTracker(); initializationEvents.begin(); - OpenTelemetryRumBuilder otelRumBuilder = OpenTelemetryRum.builder(application); + + OtelRumConfig config = new OtelRumConfig(); + GlobalAttributesSupplier globalAttributeSupplier = + new GlobalAttributesSupplier(builder.globalAttributes); + config.setGlobalAttributes(globalAttributeSupplier); + + OpenTelemetryRumBuilder otelRumBuilder = OpenTelemetryRum.builder(application, config); otelRumBuilder.mergeResource(createSplunkResource()); initializationEvents.emit("resourceInitialized"); @@ -104,14 +111,6 @@ SplunkRum initialize( // TODO: How truly important is the order of these span processors? The location of event // generation should probably not be altered... - GlobalAttributesSpanAppender globalAttributesSpanAppender = - GlobalAttributesSpanAppender.create(builder.globalAttributes); - - // Add span processor that appends global attributes. - otelRumBuilder.addTracerProviderCustomizer( - (tracerProviderBuilder, app) -> - tracerProviderBuilder.addSpanProcessor(globalAttributesSpanAppender)); - // Add span processor that appends network attributes. otelRumBuilder.addTracerProviderCustomizer( (tracerProviderBuilder, app) -> { @@ -120,15 +119,6 @@ SplunkRum initialize( return tracerProviderBuilder.addSpanProcessor(networkAttributesSpanAppender); }); - // Add span processor that appends screen attributes and generate init event. - otelRumBuilder.addTracerProviderCustomizer( - (tracerProviderBuilder, app) -> { - ScreenAttributesAppender screenAttributesAppender = - new ScreenAttributesAppender(visibleScreenTracker); - initializationEvents.emit("attributeAppenderInitialized"); - return tracerProviderBuilder.addSpanProcessor(screenAttributesAppender); - }); - // Add batch span processor otelRumBuilder.addTracerProviderCustomizer( (tracerProviderBuilder, app) -> { @@ -225,7 +215,7 @@ SplunkRum initialize( builder.getConfigFlags(), openTelemetryRum.getOpenTelemetry().getTracer(RUM_TRACER_NAME)); - return new SplunkRum(openTelemetryRum, globalAttributesSpanAppender); + return new SplunkRum(openTelemetryRum, globalAttributeSupplier); } @NonNull diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java b/splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java index d6861718..5a9922f7 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/SplunkRum.java @@ -26,7 +26,7 @@ import android.util.Log; import android.webkit.WebView; import androidx.annotation.Nullable; -import io.opentelemetry.android.GlobalAttributesSpanAppender; +import com.splunk.rum.internal.GlobalAttributesSupplier; import io.opentelemetry.android.OpenTelemetryRum; import io.opentelemetry.android.instrumentation.network.CurrentNetworkProvider; import io.opentelemetry.android.instrumentation.startup.AppStartupTimer; @@ -73,14 +73,14 @@ public class SplunkRum { @Nullable private static SplunkRum INSTANCE; private final OpenTelemetryRum openTelemetryRum; - private final GlobalAttributesSpanAppender globalAttributes; + private final GlobalAttributesSupplier globalAttributes; static { Handler handler = new Handler(Looper.getMainLooper()); startupTimer.detectBackgroundStart(handler); } - SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSpanAppender globalAttributes) { + SplunkRum(OpenTelemetryRum openTelemetryRum, GlobalAttributesSupplier globalAttributes) { this.openTelemetryRum = openTelemetryRum; this.globalAttributes = globalAttributes; } diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/internal/GlobalAttributesSupplier.java b/splunk-otel-android/src/main/java/com/splunk/rum/internal/GlobalAttributesSupplier.java new file mode 100644 index 00000000..4a4db6e4 --- /dev/null +++ b/splunk-otel-android/src/main/java/com/splunk/rum/internal/GlobalAttributesSupplier.java @@ -0,0 +1,53 @@ +/* + * 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.internal; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * Class to hold and update the set of global attributes that are appended to each span. This is + * used as the supplier to the GlobalAttributesSpanAppender. We need this because SplunkRum exposes + * a topmost update() method, and we can't break that contract, and there's no way to get a + * reference to the GlobalAttributesSpanAppender created by OtelRum. + * + *
When global attributes are more fleshed out in upstream, this will hopefully improve or go + * away. + * + *
This class is internal and is hence not for public use. Its APIs are unstable and can change
+ * at any time.
+ */
+public class GlobalAttributesSupplier implements Supplier