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



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

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

.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.

Parameters:

  • block (Proc)

    block that accepts unit and returns timestamp in the requested unit



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

.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