-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NTP synchronization by using Kronos. #54
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have enough visibility on how
otel-swift
uses theClock.now
, but one thing we had to mitigate indd-sdk-ios
wasKronos.Clock.now
being not monotonic until.sync()
resolves all calls to the NTP pool.In other words, this:
might give negative
delta
for the first attempts when NTP sync is being performed and device time is ahead of the NTP time.When browsing this (start) and this (end) code in
otel-swift
, I'm not sure if this is not the case here. Is it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otel-swift
uses a monotonic clock for each span, which is initialized with the value of the active clock (Kronos clock in this case) that is only called once per span at the start, so durations should never be negative. But those spans can be started from any thread, so I cannot use theKronos.Clock.now
directly, I must evaluate if performance will suffer much from calling it in the main thread since I only call it once per spanThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will start with the simplest implementation, in my framework most of the spans will start at main thread (XCTest observer), and performance should not be so critical
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's definitely the easiest solution 👍, will work if you can make this threading assumption. Otherwise, we solved this (in DataDog/dd-sdk-ios#607) by using our
ValuePublisher
which usesDispatchQueue
. This comes with lock penalty, which we mitigate by using concurrent queue with.barrier
for making (rare) writes exclusive and allowing (frequent) simultaneous reads.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I find my solution performance is not good enough I will try to change to your solution, its way better than mine.