Skip to content

Commit 2182f07

Browse files
pass end view to app launch vital
1 parent 31933fc commit 2182f07

File tree

8 files changed

+126
-32
lines changed

8 files changed

+126
-32
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ internal data class Time(
1313
val nanoTime: Long = System.nanoTime()
1414
)
1515

16+
/**
17+
* Convert a value obtained from [System.currentTimeMillis] to [Time].
18+
*/
1619
internal fun Long.asTime(): Time {
1720
// Because nanoTime only measures the nanoseconds since the beginning
1821
// of the current JVM lifetime, we need to approximate the nanotime we want.
@@ -22,3 +25,12 @@ internal fun Long.asTime(): Time {
2225
val offset = this - now.timestamp
2326
return Time(this, TimeUnit.MILLISECONDS.toNanos(offset) + now.nanoTime)
2427
}
28+
29+
/**
30+
* Convert a value obtained from [System.nanoTime] to [Time].
31+
*/
32+
internal fun Long.asTimeNs(): Time {
33+
val now = Time()
34+
val offset = this - now.nanoTime
35+
return Time(TimeUnit.NANOSECONDS.toMillis(offset) + now.timestamp, this)
36+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ internal class RumSessionScope(
162162
writeScope = writeScope,
163163
writer = actualWriter,
164164
rumContext = getRumContext(),
165-
customAttributes = getCustomAttributes()
165+
customAttributes = getCustomAttributes(),
166+
activeView = activeView
166167
)
167168
}
168169
}
@@ -179,7 +180,8 @@ internal class RumSessionScope(
179180
writeScope = writeScope,
180181
writer = actualWriter,
181182
rumContext = getRumContext(),
182-
customAttributes = getCustomAttributes()
183+
customAttributes = getCustomAttributes(),
184+
activeView = activeView
183185
)
184186
}
185187
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class RumVitalAppLaunchEventHelper(
3333
datadogContext: DatadogContext,
3434
eventAttributes: Map<String, Any?>,
3535
customAttributes: Map<String, Any?>,
36-
view: RumVitalAppLaunchEvent.RumVitalAppLaunchEventView?,
36+
view: RumViewScope?,
3737
hasReplay: Boolean?,
3838
rumContext: RumContext,
3939
durationNs: Long,
@@ -85,7 +85,14 @@ internal class RumVitalAppLaunchEventHelper(
8585
type = sessionType,
8686
hasReplay = hasReplay
8787
),
88-
view = view,
88+
view = view?.let {
89+
RumVitalAppLaunchEvent.RumVitalAppLaunchEventView(
90+
id = it.viewId,
91+
referrer = null,
92+
url = it.url,
93+
name = it.key.name
94+
)
95+
},
8996
source = RumVitalAppLaunchEvent.RumVitalAppLaunchEventSource.tryFromSource(
9097
source = datadogContext.source,
9198
internalLogger = internalLogger

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/internal/startup/RumAppStartupDetector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.datadog.android.core.InternalSdkCore
1111
import com.datadog.android.core.internal.system.BuildSdkVersionProvider
1212
import com.datadog.android.rum.DdRumContentProvider
1313
import com.datadog.android.rum.internal.domain.Time
14-
import com.datadog.android.rum.internal.domain.asTime
14+
import com.datadog.android.rum.internal.domain.asTimeNs
1515

1616
internal interface RumAppStartupDetector {
1717
interface Listener {
@@ -29,7 +29,7 @@ internal interface RumAppStartupDetector {
2929
return RumAppStartupDetectorImpl(
3030
application = application,
3131
buildSdkVersionProvider = BuildSdkVersionProvider.DEFAULT,
32-
appStartupTimeProvider = { sdkCore.appStartTimeNs.asTime() },
32+
appStartupTimeProvider = { sdkCore.appStartTimeNs.asTimeNs() },
3333
processImportanceProvider = { DdRumContentProvider.processImportance },
3434
timeProvider = { Time() },
3535
listener

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/internal/startup/RumSessionScopeStartupManager.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.datadog.android.api.storage.DataWriter
1313
import com.datadog.android.core.InternalSdkCore
1414
import com.datadog.android.rum.internal.domain.RumContext
1515
import com.datadog.android.rum.internal.domain.scope.RumRawEvent
16+
import com.datadog.android.rum.internal.domain.scope.RumViewScope
1617
import com.datadog.android.rum.internal.domain.scope.RumVitalAppLaunchEventHelper
1718
import com.datadog.android.rum.internal.utils.newRumEventWriteOperation
1819
import com.datadog.android.rum.model.RumVitalAppLaunchEvent
@@ -26,7 +27,8 @@ internal interface RumSessionScopeStartupManager {
2627
writeScope: EventWriteScope,
2728
writer: DataWriter<Any>,
2829
rumContext: RumContext,
29-
customAttributes: Map<String, Any?>
30+
customAttributes: Map<String, Any?>,
31+
activeView: RumViewScope?
3032
)
3133

3234
fun onTTFDEvent(
@@ -35,7 +37,8 @@ internal interface RumSessionScopeStartupManager {
3537
writeScope: EventWriteScope,
3638
writer: DataWriter<Any>,
3739
rumContext: RumContext,
38-
customAttributes: Map<String, Any?>
40+
customAttributes: Map<String, Any?>,
41+
activeView: RumViewScope?
3942
)
4043

4144
companion object {
@@ -84,7 +87,8 @@ internal class RumSessionScopeStartupManagerImpl(
8487
writeScope: EventWriteScope,
8588
writer: DataWriter<Any>,
8689
rumContext: RumContext,
87-
customAttributes: Map<String, Any?>
90+
customAttributes: Map<String, Any?>,
91+
activeView: RumViewScope?
8892
) {
8993
ttidReportedForScenario = true
9094

@@ -105,7 +109,7 @@ internal class RumSessionScopeStartupManagerImpl(
105109
datadogContext = datadogContext,
106110
eventAttributes = emptyMap(),
107111
customAttributes = customAttributes,
108-
view = null,
112+
view = activeView,
109113
hasReplay = null,
110114
rumContext = rumContext,
111115
durationNs = event.info.durationNs,
@@ -126,7 +130,8 @@ internal class RumSessionScopeStartupManagerImpl(
126130
rumContext = rumContext,
127131
customAttributes = customAttributes,
128132
durationNs = event.info.durationNs,
129-
scenario = event.info.scenario
133+
scenario = event.info.scenario,
134+
activeView = activeView
130135
)
131136
}
132137
}
@@ -138,7 +143,8 @@ internal class RumSessionScopeStartupManagerImpl(
138143
writeScope: EventWriteScope,
139144
writer: DataWriter<Any>,
140145
rumContext: RumContext,
141-
customAttributes: Map<String, Any?>
146+
customAttributes: Map<String, Any?>,
147+
activeView: RumViewScope?
142148
) {
143149
if (ttfdReportedForSession) {
144150
return
@@ -185,7 +191,8 @@ internal class RumSessionScopeStartupManagerImpl(
185191
rumContext = rumContext,
186192
customAttributes = customAttributes,
187193
durationNs = event.eventTime.nanoTime - scenario.initialTime.nanoTime,
188-
scenario = scenario
194+
scenario = scenario,
195+
activeView = activeView
189196
)
190197
}
191198

@@ -196,15 +203,16 @@ internal class RumSessionScopeStartupManagerImpl(
196203
rumContext: RumContext,
197204
customAttributes: Map<String, Any?>,
198205
durationNs: Long,
199-
scenario: RumStartupScenario
206+
scenario: RumStartupScenario,
207+
activeView: RumViewScope?
200208
) {
201209
sdkCore.newRumEventWriteOperation(datadogContext, writeScope, writer) {
202210
rumVitalAppLaunchEventHelper.newVitalAppLaunchEvent(
203211
timestampMs = scenario.initialTime.timestamp + sdkCore.time.serverTimeOffsetMs,
204212
datadogContext = datadogContext,
205213
eventAttributes = emptyMap(),
206214
customAttributes = customAttributes,
207-
view = null,
215+
view = activeView,
208216
hasReplay = null,
209217
rumContext = rumContext,
210218
durationNs = durationNs,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,23 @@ internal class TimeTest {
5252
offset(2L)
5353
)
5454
}
55+
56+
@Test
57+
fun `M convert nanoTime to Time W asTimeNs()`(
58+
@LongForgery(1000000000000, 2000000000000) nanoTime: Long
59+
) {
60+
val startMs = System.currentTimeMillis()
61+
val startNs = System.nanoTime()
62+
val time = nanoTime.asTimeNs()
63+
val endNs = System.nanoTime()
64+
val endMs = System.currentTimeMillis()
65+
66+
assertThat(time.nanoTime).isEqualTo(nanoTime)
67+
val nanoOffset = time.nanoTime - ((startNs + endNs) / 2)
68+
val milliOffset = time.timestamp - ((startMs + endMs) / 2)
69+
assertThat(TimeUnit.NANOSECONDS.toMillis(nanoOffset)).isCloseTo(
70+
milliOffset,
71+
offset(2L)
72+
)
73+
}
5574
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,8 @@ internal class RumSessionScopeTest {
15431543
writeScope = mockEventWriteScope,
15441544
writer = mockWriter,
15451545
rumContext = rumContext,
1546-
customAttributes = fakeParentAttributes
1546+
customAttributes = fakeParentAttributes,
1547+
activeView = testedScope.activeView
15471548
)
15481549

15491550
verifyNoMoreInteractions(mockRumSessionScopeStartupManager)
@@ -1578,7 +1579,8 @@ internal class RumSessionScopeTest {
15781579
writeScope = mockEventWriteScope,
15791580
writer = mockWriter,
15801581
rumContext = rumContext,
1581-
customAttributes = fakeParentAttributes
1582+
customAttributes = fakeParentAttributes,
1583+
activeView = testedScope.activeView
15821584
)
15831585

15841586
verifyNoMoreInteractions(mockRumSessionScopeStartupManager)

0 commit comments

Comments
 (0)