-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from DataDog/ncreated/RUMM-655-use-ntp-time-w…
…hen-writing-events RUMM-655 / RUMM-689 Use NTP time for Logs, Spans and RUM events
- Loading branch information
Showing
41 changed files
with
519 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// Adjusts device time to server time using the time difference calculated with NTP. | ||
internal protocol DateCorrectionType { | ||
/// Corrects given device time to server time using the last known time difference between the two. | ||
func toServerDate(deviceDate: Date) -> Date | ||
} | ||
|
||
internal class DateCorrection: DateCorrectionType { | ||
static let datadogNTPServers = [ | ||
"0.datadog.pool.ntp.org", | ||
"1.datadog.pool.ntp.org", | ||
"2.datadog.pool.ntp.org", | ||
"3.datadog.pool.ntp.org" | ||
] | ||
private let deviceDateProvider: DateProvider | ||
private let serverDateProvider: ServerDateProvider | ||
|
||
init(deviceDateProvider: DateProvider, serverDateProvider: ServerDateProvider) { | ||
self.deviceDateProvider = deviceDateProvider | ||
self.serverDateProvider = serverDateProvider | ||
// swiftlint:disable trailing_closure | ||
serverDateProvider.synchronize( | ||
with: DateCorrection.datadogNTPServers.randomElement()!, // swiftlint:disable:this force_unwrapping | ||
completion: { serverTime in | ||
let deviceTime = deviceDateProvider.currentDate() | ||
if let serverTime = serverTime { | ||
let difference = (serverTime.timeIntervalSince(deviceTime) * 1_000).rounded() / 1_000 | ||
userLogger.info( | ||
""" | ||
NTP time synchronization completed. | ||
Server time will be used for signing events (current server time is \(serverTime); \(difference)s difference with device time). | ||
""" | ||
) | ||
} else { | ||
userLogger.warn( | ||
""" | ||
NTP time synchronization failed. | ||
Device time will be used for signing events (current device time is \(deviceTime)). | ||
""" | ||
) | ||
} | ||
} | ||
) | ||
// swiftlint:enable trailing_closure | ||
} | ||
|
||
func toServerDate(deviceDate: Date) -> Date { | ||
if let serverTime = serverDateProvider.currentDate() { | ||
let deviceTime = deviceDateProvider.currentDate() | ||
let timeDifference = serverTime.timeIntervalSince(deviceTime) | ||
return deviceDate.addingTimeInterval(timeDifference) | ||
} else { | ||
return deviceDate | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import Kronos | ||
|
||
/// Abstract the monotonic clock synchronized with the server using NTP. | ||
internal protocol ServerDateProvider { | ||
/// Start the clock synchronisation with NTP server. | ||
/// Calls the `completion` by passing it the server time when the synchronization succeeds or`nil` if it fails. | ||
func synchronize(with ntpPool: String, completion: @escaping (Date?) -> Void) | ||
/// Returns the server time or `nil` if not yet determined. | ||
/// This time gets more precise while synchronization is pending. | ||
func currentDate() -> Date? | ||
} | ||
|
||
internal class NTPServerDateProvider: ServerDateProvider { | ||
func synchronize(with ntpPool: String, completion: @escaping (Date?) -> Void) { | ||
// swiftlint:disable trailing_closure multiline_arguments_brackets | ||
Clock.sync(from: ntpPool, completion: { serverTime, _ in | ||
completion(serverTime) | ||
}) | ||
// swiftlint:enable trailing_closure multiline_arguments_brackets | ||
} | ||
|
||
func currentDate() -> Date? { | ||
return Clock.now | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.