-
Notifications
You must be signed in to change notification settings - Fork 6
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 #54 from DataDog/add-ntp-clock
* Add NTP synchronization by using Kronos. It just wraps Kronos Clock in a new Opentelemetry Clock and uses it. * Make sure Kronos.Clock is only called in main thread
- Loading branch information
Showing
5 changed files
with
81 additions
and
8 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,29 @@ | ||
/* | ||
* 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 2020-2021 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
@_implementationOnly import Kronos | ||
@_implementationOnly import OpenTelemetrySdk | ||
|
||
class NTPClock: OpenTelemetrySdk.Clock { | ||
init() { | ||
Kronos.Clock.sync() | ||
} | ||
|
||
var now: Date { | ||
if Thread.isMainThread { | ||
return Kronos.Clock.now ?? Date() | ||
} else { | ||
var date = Date() | ||
DispatchQueue.main.sync { | ||
if let now = Kronos.Clock.now { | ||
date = now | ||
} | ||
} | ||
return date | ||
} | ||
} | ||
} |