diff --git a/core/api/core.api b/core/api/core.api index e9a818073..04804bfc9 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -62,25 +62,25 @@ public final class io/opentelemetry/android/SessionIdRatioBasedSampler : io/open public fun shouldSample (Lio/opentelemetry/context/Context;Ljava/lang/String;Ljava/lang/String;Lio/opentelemetry/api/trace/SpanKind;Lio/opentelemetry/api/common/Attributes;Ljava/util/List;)Lio/opentelemetry/sdk/trace/samplers/SamplingResult; } -public class io/opentelemetry/android/config/OtelRumConfig { +public final class io/opentelemetry/android/config/OtelRumConfig { public fun ()V - public fun allowInstrumentation (Ljava/lang/String;)Lio/opentelemetry/android/config/OtelRumConfig; - public fun disableInstrumentationDiscovery ()Lio/opentelemetry/android/config/OtelRumConfig; - public fun disableNetworkAttributes ()Lio/opentelemetry/android/config/OtelRumConfig; - public fun disableScreenAttributes ()Lio/opentelemetry/android/config/OtelRumConfig; - public fun disableSdkInitializationEvents ()Lio/opentelemetry/android/config/OtelRumConfig; - public fun getDiskBufferingConfig ()Lio/opentelemetry/android/features/diskbuffering/DiskBufferingConfig; - public fun getGlobalAttributesSupplier ()Ljava/util/function/Supplier; - public fun hasGlobalAttributes ()Z - public fun isSuppressed (Ljava/lang/String;)Z - public fun setDiskBufferingConfig (Lio/opentelemetry/android/features/diskbuffering/DiskBufferingConfig;)Lio/opentelemetry/android/config/OtelRumConfig; - public fun setGlobalAttributes (Lio/opentelemetry/api/common/Attributes;)Lio/opentelemetry/android/config/OtelRumConfig; - public fun setGlobalAttributes (Ljava/util/function/Supplier;)Lio/opentelemetry/android/config/OtelRumConfig; - public fun shouldDiscoverInstrumentations ()Z - public fun shouldGenerateSdkInitializationEvents ()Z - public fun shouldIncludeNetworkAttributes ()Z - public fun shouldIncludeScreenAttributes ()Z - public fun suppressInstrumentation (Ljava/lang/String;)Lio/opentelemetry/android/config/OtelRumConfig; + public final fun allowInstrumentation (Ljava/lang/String;)Lio/opentelemetry/android/config/OtelRumConfig; + public final fun disableInstrumentationDiscovery ()Lio/opentelemetry/android/config/OtelRumConfig; + public final fun disableNetworkAttributes ()Lio/opentelemetry/android/config/OtelRumConfig; + public final fun disableScreenAttributes ()Lio/opentelemetry/android/config/OtelRumConfig; + public final fun disableSdkInitializationEvents ()Lio/opentelemetry/android/config/OtelRumConfig; + public final fun getDiskBufferingConfig ()Lio/opentelemetry/android/features/diskbuffering/DiskBufferingConfig; + public final fun getGlobalAttributesSupplier ()Ljava/util/function/Supplier; + public final fun hasGlobalAttributes ()Z + public final fun isSuppressed (Ljava/lang/String;)Z + public final fun setDiskBufferingConfig (Lio/opentelemetry/android/features/diskbuffering/DiskBufferingConfig;)Lio/opentelemetry/android/config/OtelRumConfig; + public final fun setGlobalAttributes (Lio/opentelemetry/api/common/Attributes;)Lio/opentelemetry/android/config/OtelRumConfig; + public final fun setGlobalAttributes (Ljava/util/function/Supplier;)Lio/opentelemetry/android/config/OtelRumConfig; + public final fun shouldDiscoverInstrumentations ()Z + public final fun shouldGenerateSdkInitializationEvents ()Z + public final fun shouldIncludeNetworkAttributes ()Z + public final fun shouldIncludeScreenAttributes ()Z + public final fun suppressInstrumentation (Ljava/lang/String;)Lio/opentelemetry/android/config/OtelRumConfig; } public final class io/opentelemetry/android/export/AttributeModifyingSpanExporter : io/opentelemetry/sdk/trace/export/SpanExporter { diff --git a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java deleted file mode 100644 index 97afaf4f9..000000000 --- a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -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; -import io.opentelemetry.api.common.Attributes; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Supplier; - -/** - * Configuration object for OpenTelemetry Android. The configuration items in this class will be - * used in the OpenTelemetryRumBuilder to wire up and enable/disable various mobile instrumentation - * components. - */ -public class OtelRumConfig { - - @Nullable private Supplier globalAttributesSupplier = null; - private boolean includeNetworkAttributes = true; - private boolean generateSdkInitializationEvents = true; - private boolean includeScreenAttributes = true; - private boolean discoverInstrumentations = true; - private DiskBufferingConfig diskBufferingConfig = DiskBufferingConfig.create(); - private final List suppressedInstrumentations = new ArrayList<>(); - - /** - * 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(@Nullable Attributes attributes) { - if (attributes == null || attributes.isEmpty()) { - return this; - } - return setGlobalAttributes(() -> attributes); - } - - public OtelRumConfig setGlobalAttributes(Supplier globalAttributesSupplier) { - this.globalAttributesSupplier = globalAttributesSupplier; - return this; - } - - public boolean hasGlobalAttributes() { - return globalAttributesSupplier != null; - } - - @NonNull - public Supplier getGlobalAttributesSupplier() { - return globalAttributesSupplier == null ? Attributes::empty : globalAttributesSupplier; - } - - /** - * Disables the collection of runtime network attributes. See {@link CurrentNetworkProvider} for - * more information. Default = true. - * - * @return this - */ - public OtelRumConfig disableNetworkAttributes() { - includeNetworkAttributes = false; - return this; - } - - /** Returns true if runtime network attributes are enabled, false otherwise. */ - public boolean shouldIncludeNetworkAttributes() { - return includeNetworkAttributes; - } - - /** - * Disables the collection of events related to the initialization of the OTel Android SDK - * itself. Default = true. - * - * @return this - */ - public OtelRumConfig disableSdkInitializationEvents() { - generateSdkInitializationEvents = false; - return this; - } - - /** Returns true if the SDK is configured to generate initialization events, false otherwise. */ - public boolean shouldGenerateSdkInitializationEvents() { - return generateSdkInitializationEvents; - } - - /** - * Call this to disable the collection of screen attributes. See {@link - * ScreenAttributesSpanProcessor} for more information. Default = true. - * - * @return this - */ - public OtelRumConfig disableScreenAttributes() { - includeScreenAttributes = false; - return this; - } - - /** Return true if the SDK should be configured to report screen attributes. */ - public boolean shouldIncludeScreenAttributes() { - return includeScreenAttributes; - } - - public DiskBufferingConfig getDiskBufferingConfig() { - return diskBufferingConfig; - } - - /** - * Return {@link Boolean#TRUE} if the RUM initialization should look for instrumentations in the - * classpath and apply them automatically. - */ - public boolean shouldDiscoverInstrumentations() { - return discoverInstrumentations; - } - - /** - * Call this to disable the automatic search for instrumentations in the classpath. - * - * @return this - */ - public OtelRumConfig disableInstrumentationDiscovery() { - discoverInstrumentations = false; - return this; - } - - /** - * Sets the parameters for caching signals in disk in order to export them later. - * - * @return this - */ - public OtelRumConfig setDiskBufferingConfig(DiskBufferingConfig diskBufferingConfig) { - this.diskBufferingConfig = diskBufferingConfig; - return this; - } - - /** - * Adds an instrumentation name to the list of suppressed instrumentations. Instrumentations - * that have been suppressed will not be installed at startup. - */ - public OtelRumConfig suppressInstrumentation(String instrumentationName) { - suppressedInstrumentations.add(instrumentationName); - return this; - } - - /** - * Removes an instrumentation name from the list of suppressed instrumentations. - * Instrumentations that have been suppressed will not be installed at startup. - */ - public OtelRumConfig allowInstrumentation(String instrumentationName) { - suppressedInstrumentations.remove(instrumentationName); - return this; - } - - /** Returns false when the given instrumentation has been suppressed. True otherwise. */ - public boolean isSuppressed(String instrumentationName) { - return suppressedInstrumentations.contains(instrumentationName); - } -} diff --git a/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.kt b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.kt new file mode 100644 index 000000000..a9fff2cf1 --- /dev/null +++ b/core/src/main/java/io/opentelemetry/android/config/OtelRumConfig.kt @@ -0,0 +1,136 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.android.config + +import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig +import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig.Companion.create +import io.opentelemetry.api.common.Attributes +import java.util.function.Supplier + +/** + * Configuration object for OpenTelemetry Android. The configuration items in this class will be + * used in the OpenTelemetryRumBuilder to wire up and enable/disable various mobile instrumentation + * components. + */ +class OtelRumConfig { + private var globalAttributesSupplierImpl: Supplier? = null + private var includeNetworkAttributes = true + private var generateSdkInitializationEvents = true + private var includeScreenAttributes = true + private var discoverInstrumentations = true + private val suppressedInstrumentations: MutableList = mutableListOf() + private var diskBufferingConfigImpl: DiskBufferingConfig = create() + + /** + * Configures the set of global attributes to emit with every span and event. Any existing + * configured attributes will be dropped. Default = none. + */ + fun setGlobalAttributes(attributes: Attributes): OtelRumConfig { + if (attributes.isEmpty) { + return this + } + return setGlobalAttributes { attributes } + } + + fun setGlobalAttributes(globalAttributesSupplier: Supplier?): OtelRumConfig { + this.globalAttributesSupplierImpl = globalAttributesSupplier + return this + } + + fun hasGlobalAttributes(): Boolean = globalAttributesSupplierImpl != null + + fun getGlobalAttributesSupplier(): Supplier = globalAttributesSupplierImpl ?: Supplier { Attributes.empty() } + + /** + * Disables the collection of runtime network attributes. See [CurrentNetworkProvider] for + * more information. Default = true. + * + * @return this + */ + fun disableNetworkAttributes(): OtelRumConfig { + includeNetworkAttributes = false + return this + } + + /** Returns true if runtime network attributes are enabled, false otherwise. */ + fun shouldIncludeNetworkAttributes(): Boolean = includeNetworkAttributes + + /** + * Disables the collection of events related to the initialization of the OTel Android SDK + * itself. Default = true. + * + * @return this + */ + fun disableSdkInitializationEvents(): OtelRumConfig { + generateSdkInitializationEvents = false + return this + } + + /** Returns true if the SDK is configured to generate initialization events, false otherwise. */ + fun shouldGenerateSdkInitializationEvents(): Boolean = generateSdkInitializationEvents + + /** + * Call this to disable the collection of screen attributes. See [ ] for more information. Default = true. + * + * @return this + */ + fun disableScreenAttributes(): OtelRumConfig { + includeScreenAttributes = false + return this + } + + /** Return true if the SDK should be configured to report screen attributes. */ + fun shouldIncludeScreenAttributes(): Boolean = includeScreenAttributes + + /** + * Return true if the RUM initialization should look for instrumentations in the + * classpath and apply them automatically. + */ + fun shouldDiscoverInstrumentations(): Boolean = discoverInstrumentations + + /** + * Call this to disable the automatic search for instrumentations in the classpath. + * + * @return this + */ + fun disableInstrumentationDiscovery(): OtelRumConfig { + discoverInstrumentations = false + return this + } + + /** + * Adds an instrumentation name to the list of suppressed instrumentations. Instrumentations + * that have been suppressed will not be installed at startup. + */ + fun suppressInstrumentation(instrumentationName: String): OtelRumConfig { + suppressedInstrumentations.add(instrumentationName) + return this + } + + /** + * Removes an instrumentation name from the list of suppressed instrumentations. + * Instrumentations that have been suppressed will not be installed at startup. + */ + fun allowInstrumentation(instrumentationName: String): OtelRumConfig { + suppressedInstrumentations.remove(instrumentationName) + return this + } + + /** Returns false when the given instrumentation has been suppressed. True otherwise. */ + fun isSuppressed(instrumentationName: String): Boolean = suppressedInstrumentations.contains(instrumentationName) + + fun getDiskBufferingConfig(): DiskBufferingConfig = diskBufferingConfigImpl + + /** + * Sets the parameters for caching signals in disk in order to export them later. + * + * @return this + */ + fun setDiskBufferingConfig(diskBufferingConfig: DiskBufferingConfig): OtelRumConfig { + this.diskBufferingConfigImpl = diskBufferingConfig + return this + } +} 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 aaffe82f4..f2c2a859b 100644 --- a/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt +++ b/core/src/test/java/io/opentelemetry/android/config/OtelRumConfigTest.kt @@ -16,15 +16,15 @@ class OtelRumConfigTest { fun `no global attributes by default`() { val config = OtelRumConfig() assertThat(config.hasGlobalAttributes()).isFalse() - assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + assertThat(config.getGlobalAttributesSupplier().get().isEmpty).isTrue() } @Test fun `setting null Attributes does nothing`() { val config = OtelRumConfig() - config.setGlobalAttributes(null as Attributes?) + config.setGlobalAttributes(null) assertThat(config.hasGlobalAttributes()).isFalse() - assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + assertThat(config.getGlobalAttributesSupplier().get().isEmpty).isTrue() } @Test @@ -32,7 +32,7 @@ class OtelRumConfigTest { val config = OtelRumConfig() config.setGlobalAttributes(Attributes.empty()) assertThat(config.hasGlobalAttributes()).isFalse() - assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + assertThat(config.getGlobalAttributesSupplier().get().isEmpty).isTrue() } @Test @@ -40,7 +40,9 @@ class OtelRumConfigTest { val config = OtelRumConfig() config.setGlobalAttributes(Attributes.of(stringKey("foo"), "bar")) assertThat(config.hasGlobalAttributes()).isTrue() - assertThat(config.globalAttributesSupplier.get().get(stringKey("foo"))).isEqualTo("bar") + assertThat( + config.getGlobalAttributesSupplier().get().get(stringKey("foo")), + ).isEqualTo("bar") } @Test @@ -48,7 +50,7 @@ class OtelRumConfigTest { val config = OtelRumConfig() config.setGlobalAttributes(null as Supplier?) assertThat(config.hasGlobalAttributes()).isFalse() - assertThat(config.globalAttributesSupplier.get().isEmpty).isTrue() + assertThat(config.getGlobalAttributesSupplier().get().isEmpty).isTrue() } @Test @@ -56,15 +58,7 @@ class OtelRumConfigTest { 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() + assertThat(config.getGlobalAttributesSupplier().get().isEmpty).isTrue() } @Test @@ -72,6 +66,8 @@ class OtelRumConfigTest { val config = OtelRumConfig() config.setGlobalAttributes { Attributes.of(stringKey("foo"), "bar") } assertThat(config.hasGlobalAttributes()).isTrue() - assertThat(config.globalAttributesSupplier.get().get(stringKey("foo"))).isEqualTo("bar") + assertThat( + config.getGlobalAttributesSupplier().get().get(stringKey("foo")), + ).isEqualTo("bar") } }