From 1cf1c255fd765c580b29b367858501d9f30e0637 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 25 Jul 2025 15:36:14 -0700 Subject: [PATCH 1/3] use correct annotation --- .../library/httpurlconnection/HttpUrlInstrumentation.java | 2 +- .../library/okhttp/v3_0/OkHttpInstrumentation.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java b/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java index 55a7e917d..f5ee7544b 100644 --- a/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java +++ b/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java @@ -137,7 +137,7 @@ public boolean emitExperimentalHttpClientMetrics() { } @Override - public void install(@NotNull InstallationContext ctx) { + public void install(@NonNull InstallationContext ctx) { HttpUrlConnectionSingletons.configure(this, ctx.getOpenTelemetry()); } diff --git a/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java b/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java index f416f6bdf..f55650606 100644 --- a/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java +++ b/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java @@ -139,7 +139,7 @@ public boolean emitExperimentalHttpClientTelemetry() { } @Override - public void install(@NotNull InstallationContext ctx) { + public void install(@NonNull InstallationContext ctx) { OkHttp3Singletons.configure(this, ctx.getOpenTelemetry()); } From 300ab9e6570130612fd0a78b3c52f1d86fdbbfd4 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 25 Jul 2025 15:36:50 -0700 Subject: [PATCH 2/3] ensure that a Supplier that returns no attributes at build time still sets up the GlobalAttributes appender(s). --- .../android/config/OtelRumConfig.java | 18 +++-- .../android/config/OtelRumConfigTest.kt | 74 +++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt diff --git a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index 37cade712..66c503e2e 100644 --- a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -5,6 +5,9 @@ package io.opentelemetry.android.config; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import io.opentelemetry.android.ScreenAttributesSpanProcessor; import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig; import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; @@ -19,7 +22,9 @@ * components. */ public class OtelRumConfig { - private Supplier globalAttributesSupplier = Attributes::empty; + + @Nullable + private Supplier globalAttributesSupplier = null; private boolean includeNetworkAttributes = true; private boolean generateSdkInitializationEvents = true; private boolean includeScreenAttributes = true; @@ -31,7 +36,10 @@ public class OtelRumConfig { * Configures the set of global attributes to emit with every span and event. Any existing * configured attributes will be dropped. Default = none. */ - public OtelRumConfig setGlobalAttributes(Attributes attributes) { + public OtelRumConfig setGlobalAttributes(@Nullable Attributes attributes) { + if(attributes == null || attributes.isEmpty()){ + return this; + } return setGlobalAttributes(() -> attributes); } @@ -41,12 +49,12 @@ public OtelRumConfig setGlobalAttributes(Supplier globalAttributesSu } public boolean hasGlobalAttributes() { - Attributes attributes = globalAttributesSupplier.get(); - return attributes != null && !attributes.isEmpty(); + return globalAttributesSupplier != null; } + @NonNull public Supplier getGlobalAttributesSupplier() { - return globalAttributesSupplier; + return globalAttributesSupplier == null ? Attributes::empty : globalAttributesSupplier; } /** diff --git a/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt b/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt new file mode 100644 index 000000000..49be9a2d2 --- /dev/null +++ b/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt @@ -0,0 +1,74 @@ +package io.opentelemetry.android.config + +import io.opentelemetry.api.common.AttributeKey.stringKey +import io.opentelemetry.api.common.Attributes +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import java.util.function.Supplier + +class OtelRumConfigTest { + + @Test + fun `no global attributes by default`(){ + val config = OtelRumConfig() + assertThat(config.hasGlobalAttributes()).isFalse() + assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + } + + @Test + fun `setting null Attributes does nothing`(){ + val config = OtelRumConfig() + config.setGlobalAttributes(null as Attributes?) + assertThat(config.hasGlobalAttributes()).isFalse() + assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + } + + @Test + fun `setting empty Attributes does nothing`(){ + val config = OtelRumConfig() + config.setGlobalAttributes(Attributes.empty()) + assertThat(config.hasGlobalAttributes()).isFalse() + assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + } + + @Test + fun `can set some Attributes directly`(){ + val config = OtelRumConfig() + config.setGlobalAttributes(Attributes.of(stringKey("foo"), "bar")) + assertThat(config.hasGlobalAttributes()).isTrue() + assertThat(config.globalAttributesSupplier.get().get(stringKey("foo"))).isEqualTo("bar") + } + + @Test + fun `setting a null attribute supplier does nothing`(){ + val config = OtelRumConfig() + config.setGlobalAttributes(null as Supplier?) + assertThat(config.hasGlobalAttributes()).isFalse() + assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + } + + @Test + fun `setting a Supplier that returns empty attributes is fine`(){ + val config = OtelRumConfig() + config.setGlobalAttributes { Attributes.empty() } + assertThat(config.hasGlobalAttributes()).isTrue() // It might return some Attributes later + assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + } + + @Test + fun `setting a Supplier that returns null attributes is fine`(){ + val config = OtelRumConfig() + config.setGlobalAttributes { null } + assertThat(config.hasGlobalAttributes()).isTrue() // It might return some Attributes later + assertThat(config.globalAttributesSupplier.get()).isNull() + } + + @Test + fun `can supply global attributes with a supplier`(){ + val config = OtelRumConfig() + config.setGlobalAttributes { Attributes.of(stringKey("foo"), "bar") } + assertThat(config.hasGlobalAttributes()).isTrue() + assertThat(config.globalAttributesSupplier.get().get(stringKey("foo"))).isEqualTo("bar") + } + +} \ No newline at end of file From 449a94d5a59445ca8e60f15a50a7db0bcb8b6515 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Fri, 25 Jul 2025 15:40:12 -0700 Subject: [PATCH 3/3] spotless --- .../android/config/OtelRumConfig.java | 6 ++--- .../android/config/OtelRumConfigTest.kt | 25 +++++++++++-------- .../HttpUrlInstrumentation.java | 1 - .../okhttp/v3_0/OkHttpInstrumentation.java | 1 - 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java index 66c503e2e..f679409a1 100644 --- a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java @@ -7,7 +7,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; - import io.opentelemetry.android.ScreenAttributesSpanProcessor; import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig; import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider; @@ -23,8 +22,7 @@ */ public class OtelRumConfig { - @Nullable - private Supplier globalAttributesSupplier = null; + @Nullable private Supplier globalAttributesSupplier = null; private boolean includeNetworkAttributes = true; private boolean generateSdkInitializationEvents = true; private boolean includeScreenAttributes = true; @@ -37,7 +35,7 @@ public class OtelRumConfig { * configured attributes will be dropped. Default = none. */ public OtelRumConfig setGlobalAttributes(@Nullable Attributes attributes) { - if(attributes == null || attributes.isEmpty()){ + if (attributes == null || attributes.isEmpty()) { return this; } return setGlobalAttributes(() -> attributes); diff --git a/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt b/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt index 49be9a2d2..aaffe82f4 100644 --- a/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt +++ b/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.android.config import io.opentelemetry.api.common.AttributeKey.stringKey @@ -7,16 +12,15 @@ import org.junit.Test import java.util.function.Supplier class OtelRumConfigTest { - @Test - fun `no global attributes by default`(){ + fun `no global attributes by default`() { val config = OtelRumConfig() assertThat(config.hasGlobalAttributes()).isFalse() assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() } @Test - fun `setting null Attributes does nothing`(){ + fun `setting null Attributes does nothing`() { val config = OtelRumConfig() config.setGlobalAttributes(null as Attributes?) assertThat(config.hasGlobalAttributes()).isFalse() @@ -24,7 +28,7 @@ class OtelRumConfigTest { } @Test - fun `setting empty Attributes does nothing`(){ + fun `setting empty Attributes does nothing`() { val config = OtelRumConfig() config.setGlobalAttributes(Attributes.empty()) assertThat(config.hasGlobalAttributes()).isFalse() @@ -32,7 +36,7 @@ class OtelRumConfigTest { } @Test - fun `can set some Attributes directly`(){ + fun `can set some Attributes directly`() { val config = OtelRumConfig() config.setGlobalAttributes(Attributes.of(stringKey("foo"), "bar")) assertThat(config.hasGlobalAttributes()).isTrue() @@ -40,7 +44,7 @@ class OtelRumConfigTest { } @Test - fun `setting a null attribute supplier does nothing`(){ + fun `setting a null attribute supplier does nothing`() { val config = OtelRumConfig() config.setGlobalAttributes(null as Supplier?) assertThat(config.hasGlobalAttributes()).isFalse() @@ -48,7 +52,7 @@ class OtelRumConfigTest { } @Test - fun `setting a Supplier that returns empty attributes is fine`(){ + fun `setting a Supplier that returns empty attributes is fine`() { val config = OtelRumConfig() config.setGlobalAttributes { Attributes.empty() } assertThat(config.hasGlobalAttributes()).isTrue() // It might return some Attributes later @@ -56,7 +60,7 @@ class OtelRumConfigTest { } @Test - fun `setting a Supplier that returns null attributes is fine`(){ + fun `setting a Supplier that returns null attributes is fine`() { val config = OtelRumConfig() config.setGlobalAttributes { null } assertThat(config.hasGlobalAttributes()).isTrue() // It might return some Attributes later @@ -64,11 +68,10 @@ class OtelRumConfigTest { } @Test - fun `can supply global attributes with a supplier`(){ + fun `can supply global attributes with a supplier`() { val config = OtelRumConfig() config.setGlobalAttributes { Attributes.of(stringKey("foo"), "bar") } assertThat(config.hasGlobalAttributes()).isTrue() assertThat(config.globalAttributesSupplier.get().get(stringKey("foo"))).isEqualTo("bar") } - -} \ No newline at end of file +} diff --git a/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java b/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java index f5ee7544b..d25206455 100644 --- a/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java +++ b/instrumentation/httpurlconnection/library/src/main/java/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlInstrumentation.java @@ -20,7 +20,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.jetbrains.annotations.NotNull; /** Instrumentation for HttpURLConnection requests. */ @AutoService(AndroidInstrumentation.class) diff --git a/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java b/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java index f55650606..6d1faa8ea 100644 --- a/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java +++ b/instrumentation/okhttp3/library/src/main/java/io/opentelemetry/instrumentation/library/okhttp/v3_0/OkHttpInstrumentation.java @@ -21,7 +21,6 @@ import java.util.Set; import okhttp3.Interceptor; import okhttp3.Response; -import org.jetbrains.annotations.NotNull; /** Instrumentation for okhttp requests. */ @AutoService(AndroidInstrumentation.class)