Skip to content

Commit

Permalink
RUM-2816 Apply rum view time offset
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Mar 21, 2024
1 parent 807627d commit 594b1e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
20 changes: 2 additions & 18 deletions DatadogSessionReplay/Sources/Feature/WebViewRecordReceiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ internal final class WebViewRecordReceiver: FeatureMessageReceiver {

var event = event

if let timestamp = event["timestamp"] as? Int {
event["timestamp"] = Int64(timestamp) + self.offset(forView: view.id, context: context)
if let timestamp = event["timestamp"] as? Int, let offset = rumContext.viewServerTimeOffset {
event["timestamp"] = Int64(timestamp) + offset.toInt64Milliseconds
}

let record = WebRecord(
Expand All @@ -54,20 +54,4 @@ internal final class WebViewRecordReceiver: FeatureMessageReceiver {

return true
}

// MARK: - Time offsets

private var offsets: [(id: String, value: Int64)] = []

private func offset(forView id: String, context: DatadogContext) -> Int64 {
if let found = offsets.first(where: { $0.id == id }) {
return found.value
}

let offset = context.serverTimeOffset.toInt64Milliseconds
offsets.insert((id, offset), at: 0)
// only retain 3 offsets
offsets = Array(offsets.prefix(3))
return offset
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import TestUtilities

class WebViewRecordReceiverTests: XCTestCase {
func testGivenRUMContextAvailable_whenReceivingWebRecord_itCreatesSegment() throws {
let rumContext: RUMContext = .mockRandom()
let serverTimeOffset: TimeInterval = .mockRandom(min: -10, max: 10).rounded()

let rumContext: RUMContext = .mockWith(
serverTimeOffset: serverTimeOffset
)

let core = PassthroughCoreMock(
context: .mockWith(
source: "react-native",
serverTimeOffset: .mockRandom(min: -10, max: 10).rounded(),
baggages: ["rum": FeatureBaggage(rumContext)]
)
)
Expand Down Expand Up @@ -44,7 +48,7 @@ class WebViewRecordReceiverTests: XCTestCase {
"viewID": browserViewID,
"records": [
[
"timestamp": 100_000 + core.context.serverTimeOffset.toInt64Milliseconds,
"timestamp": 100_000 + serverTimeOffset.toInt64Milliseconds,
"type": 2
].merging(random, uniquingKeysWith: { old, _ in old })
]
Expand Down

0 comments on commit 594b1e9

Please sign in to comment.