Skip to content

Fix timing of ApplicationLaunch view and application_start events#1305

Merged
fuzzybinary merged 5 commits into
developfrom
jward/fix-app-start-time
Mar 3, 2023
Merged

Fix timing of ApplicationLaunch view and application_start events#1305
fuzzybinary merged 5 commits into
developfrom
jward/fix-app-start-time

Conversation

@fuzzybinary

Copy link
Copy Markdown
Member

What does this PR do?

Fix the timing of ApplicaitonLaunch vie and application_start events. The events were incorrectly using the nano time for their timestamp instead of using an ms timestamp. We use the difference between the triggering event's timestamp and nano time to determine how far apart they are, and add that to the application start time.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@fuzzybinary
fuzzybinary requested a review from a team as a code owner February 27, 2023 14:47
The events were incorrectly using the nano time for their timestamp.
@fuzzybinary
fuzzybinary force-pushed the jward/fix-app-start-time branch from fe5c4f1 to 062424a Compare February 27, 2023 14:48
@WorkerThread
private fun startApplicationLaunchView(event: RumRawEvent, writer: DataWriter<Any>) {
val applicationLaunchViewTimeNs = appStartTimeProvider.appStartTimeNs
// appStartTimeNs is based on elapsed system time. Timestamp needs to be currentMs.

@0xnm 0xnm Feb 27, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this description confusing. timestamp doesn't need to be current time.

Basically Time#timestamp and Time#nanoTime are pointing to the same instant (moment of the time), while having different base - timestamp is based on the Unix epoch start, nanoTime is based on the JVM start time (for Android I think it is effectively the process start/fork time).

So I guess the comment wanted to say that timestamp should have the same base as System.currentTimeMillis (just the shortened abbreviation is used)?

So what we are doing below is first calculating the different between 2 instant representations, which is event.eventTime.timestamp - TimeUnit.NANOSECONDS.toMillis(event.eventTime.nanoTime) to find out when actually process has started in terms of epoch time, and then apply the time since this process start (which is applicationLaunchViewTimeNs) to find when event happened in the epoch time format.

So I would suggest for the clarity to introduce the variable processStartNs with the description what is this (also maybe we need to keep calculation in the ns and convert to the ms at the very end, better probably precision-wise (although 10^6 different between ns and ms will erase this precision gain anyway)), because I think without it the expression below is not quite clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll take another stab at it.

@fuzzybinary
fuzzybinary requested a review from 0xnm February 27, 2023 17:35
@fuzzybinary

Copy link
Copy Markdown
Member Author

@0xnm took another stab at it. Let me know if that's clearer.

// to convert it to milliseconds since epoch provided by System.currentTimeMillis.
// To do so, we take the offset of those times in the event time, which should be consistent,
// then add that to our processStartTime to get the correct value.
val timestampNs = (TimeUnit.MILLISECONDS.toNanos(event.eventTime.timestamp - event.eventTime.nanoTime)) +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be like (TimeUnit.MILLISECONDS.toNanos(event.eventTime.timestamp) - event.eventTime.nanoTime) here? given that nanoTime is actually in nanoseconds.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ Yes. Fixed.

@fuzzybinary
fuzzybinary force-pushed the jward/fix-app-start-time branch from 79b13e3 to 5483718 Compare February 28, 2023 00:16
@fuzzybinary
fuzzybinary force-pushed the jward/fix-app-start-time branch from dea1292 to 99e578a Compare February 28, 2023 15:46
Also fix updating RUM context on session start.
@fuzzybinary
fuzzybinary force-pushed the jward/fix-app-start-time branch from 3c60f53 to 442ec60 Compare March 1, 2023 18:23
sessionState = if (keepSession) State.TRACKED else State.NOT_TRACKED
sessionId = UUID.randomUUID().toString()
sessionStartNs.set(nanoTime)
sdkCore.updateFeatureContext(RumFeature.RUM_FEATURE_NAME) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. While it probably makes sense to update session ID when session is renewed, in the end the only real top-level entity is RUM view, and once RUM view is created, it will be associated with the proper session anyway. So I guess the only impact without this change lies in the Logs/Traces domain, they can have old RUM session ID attached.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #1305 (8eb374c) into develop (c618644) will decrease coverage by 0.03%.
The diff coverage is 91.67%.

@@             Coverage Diff             @@
##           develop    #1305      +/-   ##
===========================================
- Coverage    81.97%   81.93%   -0.03%     
===========================================
  Files          362      362              
  Lines        12855    12859       +4     
  Branches      2155     2155              
===========================================
- Hits         10537    10536       -1     
- Misses        1662     1665       +3     
- Partials       656      658       +2     
Impacted Files Coverage Δ
...droid/rum/internal/domain/scope/RumSessionScope.kt 89.74% <50.00%> (-1.05%) ⬇️
...d/rum/internal/domain/scope/RumViewManagerScope.kt 92.04% <100.00%> (+0.37%) ⬆️
.../android/rum/internal/domain/scope/RumViewScope.kt 95.40% <100.00%> (-0.02%) ⬇️
.../datadog/android/sessionreplay/utils/ThemeUtils.kt 83.33% <0.00%> (-16.67%) ⬇️
...n/com/datadog/android/v2/core/SdkInternalLogger.kt 91.67% <0.00%> (-2.78%) ⬇️
...d/v2/core/internal/storage/FileEventBatchWriter.kt 94.74% <0.00%> (-2.63%) ⬇️
...id/rum/internal/ndk/DatadogNdkCrashEventHandler.kt 89.00% <0.00%> (-2.00%) ⬇️
...rsistence/file/batch/PlainBatchFileReaderWriter.kt 81.54% <0.00%> (-1.54%) ⬇️
...ndroid/telemetry/internal/TelemetryEventHandler.kt 72.66% <0.00%> (-0.78%) ⬇️
...in/com/datadog/android/log/internal/LogsFeature.kt 86.92% <0.00%> (+0.77%) ⬆️
... and 2 more

@fuzzybinary
fuzzybinary merged commit f6cfb7a into develop Mar 3, 2023
@fuzzybinary
fuzzybinary deleted the jward/fix-app-start-time branch March 3, 2023 19:10
@xgouchet xgouchet added this to the 1.18.0 milestone Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants