Skip to content

Commit a4a2645

Browse files
committed
RUM-9908 handle View attributes updates
1 parent 0d54656 commit a4a2645

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,16 @@ internal class RumApplicationScope(
122122

123123
// endregion
124124

125+
// region RumViewChangedListener
126+
125127
override fun onViewChanged(viewInfo: RumViewInfo) {
126128
if (viewInfo.isActive) {
127129
lastActiveViewInfo = viewInfo
128130
}
129131
}
130132

133+
// endregion
134+
131135
// region Internal
132136

133137
@WorkerThread

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ internal open class RumViewScope(
216216

217217
is RumRawEvent.UpdatePerformanceMetric -> onUpdatePerformanceMetric(event)
218218
is RumRawEvent.AddViewLoadingTime -> onAddViewLoadingTime(event, writer)
219+
is RumRawEvent.AddViewAttributes -> onAddViewAttributes(event)
220+
is RumRawEvent.RemoveViewAttributes -> onRemoveViewAttributes(event)
219221

220222
else -> delegateEventToChildren(event, writer)
221223
}
@@ -322,6 +324,18 @@ internal open class RumViewScope(
322324
sendViewUpdate(event, writer)
323325
}
324326

327+
@WorkerThread
328+
private fun onAddViewAttributes(event: RumRawEvent.AddViewAttributes) {
329+
viewAttributes.putAll(event.attributes)
330+
}
331+
332+
@WorkerThread
333+
private fun onRemoveViewAttributes(event: RumRawEvent.RemoveViewAttributes) {
334+
event.attributes.forEach {
335+
viewAttributes.remove(it)
336+
}
337+
}
338+
325339
@WorkerThread
326340
private fun onStartView(
327341
event: RumRawEvent.StartView,

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ import com.datadog.tools.unit.extensions.TestConfigurationExtension
4545
import com.datadog.tools.unit.extensions.config.TestConfiguration
4646
import com.datadog.tools.unit.forge.exhaustiveAttributes
4747
import fr.xgouchet.elmyr.Forge
48+
import fr.xgouchet.elmyr.annotation.AdvancedForgery
4849
import fr.xgouchet.elmyr.annotation.BoolForgery
4950
import fr.xgouchet.elmyr.annotation.Forgery
5051
import fr.xgouchet.elmyr.annotation.LongForgery
52+
import fr.xgouchet.elmyr.annotation.MapForgery
5153
import fr.xgouchet.elmyr.annotation.StringForgery
54+
import fr.xgouchet.elmyr.annotation.StringForgeryType
5255
import fr.xgouchet.elmyr.junit5.ForgeConfiguration
5356
import fr.xgouchet.elmyr.junit5.ForgeExtension
5457
import org.assertj.core.api.Assertions.assertThat
@@ -314,6 +317,52 @@ internal class RumViewScopeAttributePropagationTest {
314317
}
315318
}
316319

320+
@Test
321+
fun `M send event with added view attributes W handleEvent(AddViewAttributes+KeepAlive) on active view`() {
322+
// Given
323+
val expectedAttributes = mutableMapOf<String, Any?>()
324+
expectedAttributes.putAll(fakeParentAttributes)
325+
expectedAttributes.putAll(fakeViewAttributes)
326+
testedScope = newRumViewScope(initialAttributes = emptyMap())
327+
328+
// When
329+
testedScope.handleEvent(RumRawEvent.AddViewAttributes(fakeViewAttributes), mockWriter)
330+
val result = testedScope.handleEvent(RumRawEvent.KeepAlive(), mockWriter)
331+
332+
// Then
333+
argumentCaptor<ViewEvent> {
334+
verify(mockWriter).write(eq(mockEventBatchWriter), capture(), eq(EventType.DEFAULT))
335+
assertThat(lastValue)
336+
.containsExactlyContextAttributes(expectedAttributes)
337+
}
338+
assertThat(result).isNotNull()
339+
}
340+
341+
@Test
342+
fun `M send event without removed view attributes W handleEvent(AddViewAttributes+KeepAlive) on active view`(
343+
@MapForgery(
344+
key = AdvancedForgery(string = [StringForgery(StringForgeryType.ALPHABETICAL)]),
345+
value = AdvancedForgery(string = [StringForgery()])
346+
) fakeViewAttributes: Map<String, String>
347+
) {
348+
// Given
349+
val expectedAttributes = mutableMapOf<String, Any?>()
350+
expectedAttributes.putAll(fakeParentAttributes)
351+
testedScope = newRumViewScope(initialAttributes = fakeViewAttributes)
352+
353+
// When
354+
testedScope.handleEvent(RumRawEvent.RemoveViewAttributes(fakeViewAttributes.keys), mockWriter)
355+
val result = testedScope.handleEvent(RumRawEvent.KeepAlive(), mockWriter)
356+
357+
// Then
358+
argumentCaptor<ViewEvent> {
359+
verify(mockWriter).write(eq(mockEventBatchWriter), capture(), eq(EventType.DEFAULT))
360+
assertThat(lastValue)
361+
.containsExactlyContextAttributes(expectedAttributes)
362+
}
363+
assertThat(result).isNotNull()
364+
}
365+
317366
// endregion
318367

319368
// region Propagate parent attributes in Error Event

0 commit comments

Comments
 (0)