Sha256: 9deab3fecf76e4818db0c40e1d4da7905db07b4546e53cbd33015ff45e11c239

Contents?: true

Size: 1.55 KB

Versions: 28

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Datadog
  module Core
    module Utils
      # Common database-related utility functions.
      module Time
        module_function

        # Current monotonic time
        #
        # @param unit [Symbol] unit for the resulting value, same as ::Process#clock_gettime, defaults to :float_second
        # @return [Numeric] timestamp in the requested unit, since some unspecified starting point
        def get_time(unit = :float_second)
          Process.clock_gettime(Process::CLOCK_MONOTONIC, unit)
        end

        # Current wall time.
        #
        # @return [Time] current time object
        def now
          ::Time.now
        end

        # 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.
        #
        # @param block [Proc] block that returns a `Time` object representing the current wall time
        def now_provider=(block)
          define_singleton_method(:now, &block)
        end

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

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

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
datadog-2.3.0 lib/datadog/core/utils/time.rb
datadog-2.2.0 lib/datadog/core/utils/time.rb
ddtrace-1.23.3 lib/datadog/core/utils/time.rb
ddtrace-1.23.2 lib/datadog/core/utils/time.rb
datadog-2.1.0 lib/datadog/core/utils/time.rb
datadog-2.0.0 lib/datadog/core/utils/time.rb
ddtrace-1.23.1 lib/datadog/core/utils/time.rb
datadog-2.0.0.beta2 lib/datadog/core/utils/time.rb
ddtrace-1.22.0 lib/datadog/core/utils/time.rb
datadog-2.0.0.beta1 lib/datadog/core/utils/time.rb
ddtrace-1.21.1 lib/datadog/core/utils/time.rb
ddtrace-1.21.0 lib/datadog/core/utils/time.rb
ddtrace-1.20.0 lib/datadog/core/utils/time.rb
ddtrace-1.19.0 lib/datadog/core/utils/time.rb
ddtrace-1.18.0 lib/datadog/core/utils/time.rb
ddtrace-1.17.0 lib/datadog/core/utils/time.rb
ddtrace-1.16.2 lib/datadog/core/utils/time.rb
ddtrace-1.16.1 lib/datadog/core/utils/time.rb
ddtrace-1.16.0 lib/datadog/core/utils/time.rb
ddtrace-1.15.0 lib/datadog/core/utils/time.rb