-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC Global Attributes implementation #1181
base: feature/next-gen
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.splunk.rum.integration.agent.api.attributes | ||
|
||
import com.cisco.android.common.utils.extensions.forEachFast | ||
import com.splunk.rum.integration.agent.api.attributes.GlobalAttributes | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan | ||
import io.opentelemetry.sdk.trace.ReadableSpan | ||
import io.opentelemetry.sdk.trace.SpanProcessor | ||
|
||
class GlobalAttributeSpanProcessor2 : SpanProcessor { | ||
|
||
override fun onStart(parentContext: Context, span: ReadWriteSpan) { | ||
// Fetch the current attributes from GlobalAttributes and apply them to the span | ||
GlobalAttributes.instance.attributes.forEach { attribute -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
when (attribute) { | ||
is GlobalAttributes.Attribute.Boolean -> span.setAttribute(attribute.name, attribute.value) | ||
is GlobalAttributes.Attribute.Double -> span.setAttribute(attribute.name, attribute.value) | ||
is GlobalAttributes.Attribute.Long -> span.setAttribute(attribute.name, attribute.value) | ||
is GlobalAttributes.Attribute.String -> span.setAttribute(attribute.name, attribute.value) | ||
} | ||
} | ||
} | ||
|
||
override fun isStartRequired(): Boolean { | ||
return true | ||
} | ||
|
||
override fun onEnd(span: ReadableSpan) { | ||
} | ||
|
||
override fun isEndRequired(): Boolean { | ||
return true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.splunk.rum.integration.agent.api.attributes | ||
|
||
import io.opentelemetry.api.common.AttributeKey | ||
|
||
class GlobalAttributes private constructor() { | ||
|
||
companion object { | ||
val instance: GlobalAttributes by lazy { GlobalAttributes() } | ||
} | ||
|
||
private val _attributes: MutableList<Attribute> = ArrayList() | ||
val attributes: List<Attribute> get() = _attributes // Expose as immutable list | ||
|
||
// this method definitely can be streamlined | ||
operator fun set(key: Any, value: Any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, it would be better to write a function for each type. These "Any functions" are often leads into issues.
|
||
if (key is String) { | ||
val existingAttribute = _attributes.find { it.name == key } | ||
if (existingAttribute != null) { | ||
_attributes.remove(existingAttribute) | ||
} | ||
|
||
_attributes += when (value) { | ||
is String -> Attribute.String(key, value) | ||
is Boolean -> Attribute.Boolean(key, value) | ||
is Long -> Attribute.Long(key, value) | ||
is Double -> Attribute.Double(key, value) | ||
else -> throw IllegalArgumentException("Unsupported attribute type") | ||
} | ||
} else if (key is AttributeKey<*>) { | ||
val existingAttribute = _attributes.find { it.name == key.key } | ||
if (existingAttribute != null) { | ||
_attributes.remove(existingAttribute) | ||
} | ||
|
||
_attributes += when (value) { | ||
is String -> Attribute.String(key.key, value) | ||
is Boolean -> Attribute.Boolean(key.key, value) | ||
is Long -> Attribute.Long(key.key, value) | ||
is Double -> Attribute.Double(key.key, value) | ||
else -> throw IllegalArgumentException("Unsupported attribute type") | ||
} | ||
} else { | ||
throw IllegalArgumentException("Key must be either String or AttributeKey") | ||
} | ||
} | ||
|
||
fun remove(key: String) { | ||
_attributes.removeAll { it.name == key } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There will be always at most one element with the
|
||
} | ||
|
||
sealed interface Attribute { | ||
|
||
val name: kotlin.String | ||
|
||
data class Boolean(override val name: kotlin.String, val value: kotlin.Boolean) : Attribute | ||
data class Double(override val name: kotlin.String, val value: kotlin.Double) : Attribute | ||
data class String(override val name: kotlin.String, val value: kotlin.String) : Attribute | ||
data class Long(override val name: kotlin.String, val value: kotlin.Long) : Attribute | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.splunk.rum.integration.agent.api.attributes.extension | ||
|
||
import com.splunk.rum.integration.agent.api.SplunkRUMAgent | ||
import com.splunk.rum.integration.agent.api.attributes.GlobalAttributes | ||
|
||
/** | ||
* Extension property to access the [GlobalAttributes] instance via [SplunkRUMAgent]. | ||
*/ | ||
@Suppress("UnusedReceiverParameter") | ||
val SplunkRUMAgent.globalAttributes: GlobalAttributes | ||
get() = GlobalAttributes.instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove
GlobalAttributeSpanProcessor
if possible