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

This file was deleted.

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

package io.opentelemetry.android

import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.common.AttributesBuilder
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.trace.ReadWriteSpan
import io.opentelemetry.sdk.trace.ReadableSpan
import io.opentelemetry.sdk.trace.SpanProcessor
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Consumer
import java.util.function.Supplier

/**
* A [SpanProcessor] implementation that appends a set of [attributes][Attributes]
* to every span. The attributes are supplied via Supplier. This Supplier may alter
* its results and return different attributes over time. collection is mutable, and can be updated
* by calling [.update].
*/
internal class GlobalAttributesSpanAppender(
initialState: Supplier<Attributes>,
) : SpanProcessor {
private val attributesSupplier = AtomicReference(initialState)

override fun onStart(
parentContext: Context,
span: ReadWriteSpan,
) {
span.setAllAttributes(attributes)
}

private val attributes: Attributes
get() = attributesSupplier.get().get()

override fun isStartRequired(): Boolean = true

override fun onEnd(span: ReadableSpan) {}

override fun isEndRequired(): Boolean = false

/**
* Update the global set of attributes to be appended to every span.
*
* Note: Calling this method invalidates the Supplier originally passed to this [ ] and any other previously updated Supplier.
*
* @param attributesUpdater A function which will update the current set of attributes, by
* operating on a [AttributesBuilder] from the current set.
*/
fun update(attributesUpdater: Consumer<AttributesBuilder>) {
synchronized(attributesSupplier) {
val oldAttributes = attributes
val builder = oldAttributes.toBuilder()
attributesUpdater.accept(builder)
val newAttributes = builder.build()
attributesSupplier.set(Supplier { newAttributes })
}
}

/**
* Replaces the currently configured attributes Supplier with a new one.
*
* @param attributesSupplier Supplier to call to obtain Attributes for every span.
*/
fun update(attributesSupplier: Supplier<Attributes>) {
this.attributesSupplier.set(attributesSupplier)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private void applyConfiguration(Services services, InitializationEvents initiali
if (config.hasGlobalAttributes()) {
// Add span processor that appends global attributes.
GlobalAttributesSpanAppender appender =
GlobalAttributesSpanAppender.create(config.getGlobalAttributesSupplier());
new GlobalAttributesSpanAppender(config.getGlobalAttributesSupplier());
addTracerProviderCustomizer(
(tracerProviderBuilder, app) ->
tracerProviderBuilder.addSpanProcessor(appender));
Expand Down

This file was deleted.

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

package io.opentelemetry.android

import io.opentelemetry.android.common.RumConstants
import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenTracker
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.trace.ReadWriteSpan
import io.opentelemetry.sdk.trace.ReadableSpan
import io.opentelemetry.sdk.trace.SpanProcessor

/**
* This class appends the screen name to all spans.
*/
internal class ScreenAttributesSpanProcessor(
private val visibleScreenTracker: VisibleScreenTracker,
) : SpanProcessor {
override fun onStart(
parentContext: Context,
span: ReadWriteSpan,
) {
val currentScreen = visibleScreenTracker.currentlyVisibleScreen
span.setAttribute(RumConstants.SCREEN_NAME_KEY, currentScreen)
}

override fun isStartRequired(): Boolean = true

override fun onEnd(span: ReadableSpan) {
// nop
}

override fun isEndRequired(): Boolean = false
}

This file was deleted.

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

package io.opentelemetry.android.internal.features.networkattrs

import io.opentelemetry.android.common.internal.features.networkattributes.CurrentNetworkAttributesExtractor
import io.opentelemetry.android.common.internal.features.networkattributes.data.CurrentNetwork
import io.opentelemetry.android.internal.services.network.CurrentNetworkProvider
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.trace.ReadWriteSpan
import io.opentelemetry.sdk.trace.ReadableSpan
import io.opentelemetry.sdk.trace.SpanProcessor

/**
* A [SpanProcessor] implementation that appends a set of [attributes][Attributes]
* describing the [current network][CurrentNetwork] to every span that is exported.
*/
internal class NetworkAttributesSpanAppender(
private val currentNetworkProvider: CurrentNetworkProvider,
) : SpanProcessor {
private val networkAttributesExtractor = CurrentNetworkAttributesExtractor()

override fun onStart(
parentContext: Context,
span: ReadWriteSpan,
) {
val currentNetwork = currentNetworkProvider.currentNetwork
span.setAllAttributes(networkAttributesExtractor.extract(currentNetwork))
}

override fun isStartRequired(): Boolean = true

override fun onEnd(span: ReadableSpan) {}

override fun isEndRequired(): Boolean = false

companion object {
@JvmStatic
fun create(currentNetworkProvider: CurrentNetworkProvider): SpanProcessor = NetworkAttributesSpanAppender(currentNetworkProvider)
}
}
Loading