-
Notifications
You must be signed in to change notification settings - Fork 368
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
Improve duration counting using monotonic clock #424
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0b69113
Improve duration counting using monotonic clock
soulcutter 259da18
Placate Rubocop and add some method comments
soulcutter c44e391
Use concurrent-ruby's monotonic clock for compatibility with older ru…
soulcutter 1d60503
Extract Datadog::Span::Duration class
soulcutter File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,69 @@ | ||
require 'concurrent/utility/monotonic_time' | ||
module Datadog | ||
class Span | ||
# Stateful class used to track and calculate the duration of a Span. | ||
class Duration | ||
attr_reader :start_time, :end_time | ||
|
||
def initialize | ||
@start_time = nil | ||
@duration_start = nil | ||
@end_time = nil | ||
@duration_end = nil | ||
@wall_clock_duration = false | ||
end | ||
|
||
# Return whether the duration is started or not | ||
def started? | ||
!@start_time.nil? | ||
end | ||
|
||
# Return whether the duration is finished or not. | ||
def finished? | ||
!@end_time.nil? | ||
end | ||
|
||
def complete? | ||
started? && finished? | ||
end | ||
|
||
def start(start_time) | ||
if start_time | ||
@start_time = start_time | ||
@duration_start = start_time | ||
@wall_clock_duration = true | ||
else | ||
@start_time = Time.now.utc | ||
@duration_start = duration_marker | ||
end | ||
end | ||
|
||
def finish(finish_time) | ||
now = Time.now.utc | ||
|
||
# Provide a default start_time if unset. | ||
# Using `now` here causes duration to be 0; this is expected | ||
# behavior when start_time is unknown. | ||
start(finish_time || now) unless started? | ||
|
||
if finish_time | ||
@end_time = finish_time | ||
@duration_start = @start_time | ||
@duration_end = finish_time | ||
@wall_clock_duration = true | ||
else | ||
@end_time = now | ||
@duration_end = @wall_clock_duration ? now : duration_marker | ||
end | ||
end | ||
|
||
def to_f | ||
(@duration_end - @duration_start).to_f rescue 0.0 | ||
end | ||
|
||
def duration_marker | ||
Concurrent.monotonic_time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the existing |
||
end | ||
end | ||
end | ||
end |
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
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.
Forwardable has some performance that we'd like to avoid. It allocates a new object on every method call, which is not desirable. It is also slower, but I'm more concerned with the memory allocation for each call to the delegated methods: