Module: Datadog::Core::Utils::Time
- Defined in:
- lib/datadog/core/utils/time.rb
Overview
Common database-related utility functions.
Class Method Summary collapse
- .as_utc_epoch_ns(time) ⇒ Object
-
.get_time(unit = :float_second) ⇒ Numeric
Current monotonic time.
-
.get_time_provider=(block) ⇒ Object
Overrides the implementation of ‘#get_time with the provided callable.
- .measure(unit = :float_second) ⇒ Object
-
.now ⇒ Time
Current wall time.
-
.now_provider=(block) ⇒ Object
Overrides the implementation of ‘#now with the provided callable.
Class Method Details
.as_utc_epoch_ns(time) ⇒ Object
68 69 70 71 72 |
# File 'lib/datadog/core/utils/time.rb', line 68 def as_utc_epoch_ns(time) # we use #to_r instead of #to_f because Float doesn't have enough precision to represent exact nanoseconds, see # https://rubyapi.org/3.0/o/time#method-i-to_f (time.to_r * 1_000_000_000).to_i end |
.get_time(unit = :float_second) ⇒ Numeric
Current monotonic time
14 15 16 |
# File 'lib/datadog/core/utils/time.rb', line 14 def get_time(unit = :float_second) Process.clock_gettime(Process::CLOCK_MONOTONIC, unit) end |
.get_time_provider=(block) ⇒ Object
Overrides the implementation of ‘#get_time with the provided callable.
Overriding the method ‘#get_time` instead of indirectly calling `block` removes one level of method call overhead.
51 52 53 54 55 56 57 58 59 |
# File 'lib/datadog/core/utils/time.rb', line 51 def get_time_provider=(block) class << self # Avoid method redefinition warning # `rescue nil` is added in case customers remove the method # themselves to squelch the warning. remove_method(:get_time) rescue nil end define_singleton_method(:get_time, &block) end |
.measure(unit = :float_second) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/datadog/core/utils/time.rb', line 61 def measure(unit = :float_second) before = get_time(unit) yield after = get_time(unit) after - before end |
.now ⇒ Time
Current wall time.
21 22 23 |
# File 'lib/datadog/core/utils/time.rb', line 21 def now ::Time.now end |
.now_provider=(block) ⇒ Object
Overrides the implementation of ‘#now with the provided callable.
Overriding the method ‘#now` instead of indirectly calling `block` removes one level of method call overhead.
33 34 35 36 37 38 39 40 41 |
# File 'lib/datadog/core/utils/time.rb', line 33 def now_provider=(block) class << self # Avoid method redefinition warning. # `rescue nil` is added in case customers remove the method # themselves to squelch the warning. remove_method(:now) rescue nil end define_singleton_method(:now, &block) end |