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
56 57 58 59 60 |
# File 'lib/datadog/core/utils/time.rb', line 56 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.
45 46 47 |
# File 'lib/datadog/core/utils/time.rb', line 45 def get_time_provider=(block) define_singleton_method(:get_time, &block) end |
.measure(unit = :float_second) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/datadog/core/utils/time.rb', line 49 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 |
# File 'lib/datadog/core/utils/time.rb', line 33 def now_provider=(block) define_singleton_method(:now, &block) end |