Module: Datadog::Core::Utils::Time

Defined in:
lib/datadog/core/utils/time.rb

Overview

Common database-related utility functions.

Class Method Summary collapse

Class Method Details

.as_utc_epoch_ns(time) ⇒ Object



44
45
46
47
48
# File 'lib/datadog/core/utils/time.rb', line 44

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

Parameters:

  • unit (Symbol) (defaults to: :float_second)

    unit for the resulting value, same as ::Process#clock_gettime, defaults to :float_second

Returns:

  • (Numeric)

    timestamp in the requested unit, since some unspecified starting point



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

.measure(unit = :float_second) ⇒ Object



37
38
39
40
41
42
# File 'lib/datadog/core/utils/time.rb', line 37

def measure(unit = :float_second)
  before = get_time(unit)
  yield
  after = get_time(unit)
  after - before
end

.nowTime

Current wall time.

Returns:

  • (Time)

    current time object



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.

Parameters:

  • block (Proc)

    block that returns a ‘Time` object representing the current wall time



33
34
35
# File 'lib/datadog/core/utils/time.rb', line 33

def now_provider=(block)
  define_singleton_method(:now, &block)
end