Skip to content

Commit 953bce8

Browse files
committed
RUM-9908 Propagate parent attributes to the ResourceScope
1 parent 4f1bef2 commit 953bce8

File tree

4 files changed

+385
-113
lines changed

4 files changed

+385
-113
lines changed

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/RumResourceScope.kt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.datadog.android.api.storage.DataWriter
1212
import com.datadog.android.core.InternalSdkCore
1313
import com.datadog.android.core.internal.net.FirstPartyHostHeaderTypeResolver
1414
import com.datadog.android.internal.utils.loggableStackTrace
15-
import com.datadog.android.rum.GlobalRumMonitor
1615
import com.datadog.android.rum.RumAttributes
1716
import com.datadog.android.rum.RumErrorSource
1817
import com.datadog.android.rum.RumResourceKind
@@ -50,9 +49,8 @@ internal class RumResourceScope(
5049
) : RumScope {
5150

5251
internal val resourceId: String = UUID.randomUUID().toString()
53-
internal val attributes: MutableMap<String, Any?> = initialAttributes.toMutableMap().apply {
54-
putAll(GlobalRumMonitor.get(sdkCore).getAttributes())
55-
}
52+
internal val resourceAttributes: MutableMap<String, Any?> = initialAttributes.toMutableMap()
53+
5654
private var timing: ResourceTiming? = null
5755
private val initialContext = parentScope.getRumContext()
5856

@@ -95,6 +93,10 @@ internal class RumResourceScope(
9593
return initialContext
9694
}
9795

96+
override fun getCustomAttributes(): Map<String, Any?> {
97+
return parentScope.getCustomAttributes() + resourceAttributes
98+
}
99+
98100
override fun isActive(): Boolean {
99101
return !stopped
100102
}
@@ -110,7 +112,7 @@ internal class RumResourceScope(
110112
) {
111113
if (key != event.key) return
112114
stopped = true
113-
attributes.putAll(event.attributes)
115+
resourceAttributes.putAll(event.attributes)
114116
kind = event.kind
115117
statusCode = event.statusCode
116118
size = event.size
@@ -138,7 +140,7 @@ internal class RumResourceScope(
138140
writer: DataWriter<Any>
139141
) {
140142
if (key != event.key) return
141-
attributes.putAll(event.attributes)
143+
resourceAttributes.putAll(event.attributes)
142144
sendError(
143145
event.message,
144146
event.source,
@@ -157,7 +159,7 @@ internal class RumResourceScope(
157159
writer: DataWriter<Any>
158160
) {
159161
if (key != event.key) return
160-
attributes.putAll(event.attributes)
162+
resourceAttributes.putAll(event.attributes)
161163

162164
val errorCategory =
163165
if (event.stackTrace.isNotEmpty()) ErrorEvent.Category.EXCEPTION else null
@@ -182,10 +184,9 @@ internal class RumResourceScope(
182184
eventTime: Time,
183185
writer: DataWriter<Any>
184186
) {
185-
attributes.putAll(GlobalRumMonitor.get(sdkCore).getAttributes())
186-
val traceId = attributes.remove(RumAttributes.TRACE_ID)?.toString()
187-
val spanId = attributes.remove(RumAttributes.SPAN_ID)?.toString()
188-
val rulePsr = attributes.remove(RumAttributes.RULE_PSR) as? Number
187+
val traceId = resourceAttributes.remove(RumAttributes.TRACE_ID)?.toString()
188+
val spanId = resourceAttributes.remove(RumAttributes.SPAN_ID)?.toString()
189+
val rulePsr = resourceAttributes.remove(RumAttributes.RULE_PSR) as? Number
189190

190191
val rumContext = getRumContext()
191192
val syntheticsAttribute = if (
@@ -207,15 +208,14 @@ internal class RumResourceScope(
207208

208209
@Suppress("UNCHECKED_CAST")
209210
val finalTiming = timing ?: extractResourceTiming(
210-
attributes.remove(RumAttributes.RESOURCE_TIMINGS) as? Map<String, Any?>
211+
resourceAttributes.remove(RumAttributes.RESOURCE_TIMINGS) as? Map<String, Any?>
211212
)
212213
val graphql = resolveGraphQLAttributes(
213-
attributes.remove(RumAttributes.GRAPHQL_OPERATION_TYPE) as? String?,
214-
attributes.remove(RumAttributes.GRAPHQL_OPERATION_NAME) as? String?,
215-
attributes.remove(RumAttributes.GRAPHQL_PAYLOAD) as? String?,
216-
attributes.remove(RumAttributes.GRAPHQL_VARIABLES) as? String?
214+
resourceAttributes.remove(RumAttributes.GRAPHQL_OPERATION_TYPE) as? String?,
215+
resourceAttributes.remove(RumAttributes.GRAPHQL_OPERATION_NAME) as? String?,
216+
resourceAttributes.remove(RumAttributes.GRAPHQL_PAYLOAD) as? String?,
217+
resourceAttributes.remove(RumAttributes.GRAPHQL_VARIABLES) as? String?
217218
)
218-
val eventAttributes = attributes.toMutableMap()
219219
sdkCore.newRumEventWriteOperation(writer) { datadogContext ->
220220
val user = datadogContext.userInfo
221221
val hasReplay = featuresContextResolver.resolveViewHasReplay(
@@ -282,7 +282,7 @@ internal class RumResourceScope(
282282
brand = datadogContext.deviceInfo.deviceBrand,
283283
architecture = datadogContext.deviceInfo.architecture
284284
),
285-
context = ResourceEvent.Context(additionalProperties = eventAttributes),
285+
context = ResourceEvent.Context(additionalProperties = getCustomAttributes().toMutableMap()),
286286
dd = ResourceEvent.Dd(
287287
traceId = traceId,
288288
spanId = spanId,
@@ -343,12 +343,9 @@ internal class RumResourceScope(
343343
writer: DataWriter<Any>,
344344
resourceStopTimestampInNanos: Long
345345
) {
346-
attributes.putAll(GlobalRumMonitor.get(sdkCore).getAttributes())
347-
val errorFingerprint = attributes.remove(RumAttributes.ERROR_FINGERPRINT) as? String
348-
346+
val errorFingerprint = resourceAttributes.remove(RumAttributes.ERROR_FINGERPRINT) as? String
349347
val rumContext = getRumContext()
350348

351-
val eventAttributes = attributes.toMutableMap()
352349
val syntheticsAttribute = if (
353350
rumContext.syntheticsTestId.isNullOrBlank() ||
354351
rumContext.syntheticsResultId.isNullOrBlank()
@@ -431,7 +428,7 @@ internal class RumResourceScope(
431428
brand = datadogContext.deviceInfo.deviceBrand,
432429
architecture = datadogContext.deviceInfo.architecture
433430
),
434-
context = ErrorEvent.Context(additionalProperties = eventAttributes),
431+
context = ErrorEvent.Context(additionalProperties = getCustomAttributes().toMutableMap()),
435432
dd = ErrorEvent.Dd(
436433
session = ErrorEvent.DdSession(
437434
sessionPrecondition = rumContext.sessionStartReason.toErrorSessionPrecondition()

0 commit comments

Comments
 (0)