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

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

Overview

Common database-related utility functions.

Constant Summary collapse

MONOTONIC_CLOCK_ID =
RUBY_PLATFORM.include?("darwin") ? Process::CLOCK_MONOTONIC_RAW : Process::CLOCK_MONOTONIC

Class Method Summary collapse

Class Method Details

.as_utc_epoch_ns(time) ⇒ Object



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

def self.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) ⇒ Float|Integer

Current monotonic time On macOS, CLOCK_MONOTONIC only has microsecond precision, so we use CLOCK_MONOTONIC_RAW which has nanosecond precision instead.

Parameters:

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

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

Returns:

  • (Float|Integer)

    timestamp in the requested unit, since some unspecified starting point



25
26
27
# File 'lib/datadog/core/utils/time.rb', line 25

def self.get_time(unit = :float_second)
  original_clock_gettime(MONOTONIC_CLOCK_ID, unit)
end

.measure(unit = :float_second) ⇒ Object



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

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