|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.android.config |
| 7 | + |
| 8 | +import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig |
| 9 | +import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig.Companion.create |
| 10 | +import io.opentelemetry.api.common.Attributes |
| 11 | +import java.util.function.Supplier |
| 12 | + |
| 13 | +/** |
| 14 | + * Configuration object for OpenTelemetry Android. The configuration items in this class will be |
| 15 | + * used in the OpenTelemetryRumBuilder to wire up and enable/disable various mobile instrumentation |
| 16 | + * components. |
| 17 | + */ |
| 18 | +class OtelRumConfig { |
| 19 | + private var globalAttributesSupplierImpl: Supplier<Attributes>? = null |
| 20 | + private var includeNetworkAttributes = true |
| 21 | + private var generateSdkInitializationEvents = true |
| 22 | + private var includeScreenAttributes = true |
| 23 | + private var discoverInstrumentations = true |
| 24 | + private val suppressedInstrumentations: MutableList<String> = mutableListOf() |
| 25 | + private var diskBufferingConfigImpl: DiskBufferingConfig = create() |
| 26 | + |
| 27 | + /** |
| 28 | + * Configures the set of global attributes to emit with every span and event. Any existing |
| 29 | + * configured attributes will be dropped. Default = none. |
| 30 | + */ |
| 31 | + fun setGlobalAttributes(attributes: Attributes): OtelRumConfig { |
| 32 | + if (attributes.isEmpty) { |
| 33 | + return this |
| 34 | + } |
| 35 | + return setGlobalAttributes { attributes } |
| 36 | + } |
| 37 | + |
| 38 | + fun setGlobalAttributes(globalAttributesSupplier: Supplier<Attributes>?): OtelRumConfig { |
| 39 | + this.globalAttributesSupplierImpl = globalAttributesSupplier |
| 40 | + return this |
| 41 | + } |
| 42 | + |
| 43 | + fun hasGlobalAttributes(): Boolean = globalAttributesSupplierImpl != null |
| 44 | + |
| 45 | + fun getGlobalAttributesSupplier(): Supplier<Attributes> = globalAttributesSupplierImpl ?: Supplier { Attributes.empty() } |
| 46 | + |
| 47 | + /** |
| 48 | + * Disables the collection of runtime network attributes. See [CurrentNetworkProvider] for |
| 49 | + * more information. Default = true. |
| 50 | + * |
| 51 | + * @return this |
| 52 | + */ |
| 53 | + fun disableNetworkAttributes(): OtelRumConfig { |
| 54 | + includeNetworkAttributes = false |
| 55 | + return this |
| 56 | + } |
| 57 | + |
| 58 | + /** Returns true if runtime network attributes are enabled, false otherwise. */ |
| 59 | + fun shouldIncludeNetworkAttributes(): Boolean = includeNetworkAttributes |
| 60 | + |
| 61 | + /** |
| 62 | + * Disables the collection of events related to the initialization of the OTel Android SDK |
| 63 | + * itself. Default = true. |
| 64 | + * |
| 65 | + * @return this |
| 66 | + */ |
| 67 | + fun disableSdkInitializationEvents(): OtelRumConfig { |
| 68 | + generateSdkInitializationEvents = false |
| 69 | + return this |
| 70 | + } |
| 71 | + |
| 72 | + /** Returns true if the SDK is configured to generate initialization events, false otherwise. */ |
| 73 | + fun shouldGenerateSdkInitializationEvents(): Boolean = generateSdkInitializationEvents |
| 74 | + |
| 75 | + /** |
| 76 | + * Call this to disable the collection of screen attributes. See [ ] for more information. Default = true. |
| 77 | + * |
| 78 | + * @return this |
| 79 | + */ |
| 80 | + fun disableScreenAttributes(): OtelRumConfig { |
| 81 | + includeScreenAttributes = false |
| 82 | + return this |
| 83 | + } |
| 84 | + |
| 85 | + /** Return true if the SDK should be configured to report screen attributes. */ |
| 86 | + fun shouldIncludeScreenAttributes(): Boolean = includeScreenAttributes |
| 87 | + |
| 88 | + /** |
| 89 | + * Return true if the RUM initialization should look for instrumentations in the |
| 90 | + * classpath and apply them automatically. |
| 91 | + */ |
| 92 | + fun shouldDiscoverInstrumentations(): Boolean = discoverInstrumentations |
| 93 | + |
| 94 | + /** |
| 95 | + * Call this to disable the automatic search for instrumentations in the classpath. |
| 96 | + * |
| 97 | + * @return this |
| 98 | + */ |
| 99 | + fun disableInstrumentationDiscovery(): OtelRumConfig { |
| 100 | + discoverInstrumentations = false |
| 101 | + return this |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Adds an instrumentation name to the list of suppressed instrumentations. Instrumentations |
| 106 | + * that have been suppressed will not be installed at startup. |
| 107 | + */ |
| 108 | + fun suppressInstrumentation(instrumentationName: String): OtelRumConfig { |
| 109 | + suppressedInstrumentations.add(instrumentationName) |
| 110 | + return this |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Removes an instrumentation name from the list of suppressed instrumentations. |
| 115 | + * Instrumentations that have been suppressed will not be installed at startup. |
| 116 | + */ |
| 117 | + fun allowInstrumentation(instrumentationName: String): OtelRumConfig { |
| 118 | + suppressedInstrumentations.remove(instrumentationName) |
| 119 | + return this |
| 120 | + } |
| 121 | + |
| 122 | + /** Returns false when the given instrumentation has been suppressed. True otherwise. */ |
| 123 | + fun isSuppressed(instrumentationName: String): Boolean = suppressedInstrumentations.contains(instrumentationName) |
| 124 | + |
| 125 | + fun getDiskBufferingConfig(): DiskBufferingConfig = diskBufferingConfigImpl |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the parameters for caching signals in disk in order to export them later. |
| 129 | + * |
| 130 | + * @return this |
| 131 | + */ |
| 132 | + fun setDiskBufferingConfig(diskBufferingConfig: DiskBufferingConfig): OtelRumConfig { |
| 133 | + this.diskBufferingConfigImpl = diskBufferingConfig |
| 134 | + return this |
| 135 | + } |
| 136 | +} |
0 commit comments