Skip to content
Merged
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 @@ -5,6 +5,8 @@

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;
Expand All @@ -19,7 +21,8 @@
* components.
*/
public class OtelRumConfig {
private Supplier<Attributes> globalAttributesSupplier = Attributes::empty;

@Nullable private Supplier<Attributes> globalAttributesSupplier = null;
private boolean includeNetworkAttributes = true;
private boolean generateSdkInitializationEvents = true;
private boolean includeScreenAttributes = true;
Expand All @@ -31,7 +34,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);
}

Expand All @@ -41,12 +47,12 @@ public OtelRumConfig setGlobalAttributes(Supplier<Attributes> globalAttributesSu
}

public boolean hasGlobalAttributes() {
Attributes attributes = globalAttributesSupplier.get();
return attributes != null && !attributes.isEmpty();
return globalAttributesSupplier != null;
}

@NonNull
public Supplier<Attributes> getGlobalAttributesSupplier() {
return globalAttributesSupplier;
return globalAttributesSupplier == null ? Attributes::empty : globalAttributesSupplier;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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<Attributes>?)
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -137,7 +136,7 @@ public boolean emitExperimentalHttpClientMetrics() {
}

@Override
public void install(@NotNull InstallationContext ctx) {
public void install(@NonNull InstallationContext ctx) {
HttpUrlConnectionSingletons.configure(this, ctx.getOpenTelemetry());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -139,7 +138,7 @@ public boolean emitExperimentalHttpClientTelemetry() {
}

@Override
public void install(@NotNull InstallationContext ctx) {
public void install(@NonNull InstallationContext ctx) {
OkHttp3Singletons.configure(this, ctx.getOpenTelemetry());
}

Expand Down