Sha256: 0b64c99d88fa7e8106463a1efa30ae8abc5c094c76cfe1e6c3a639e907a9a40c

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

require 'datadog/statsd'
require 'time'

module Percy
  class Stats < ::Datadog::Statsd
    DEFAULT_HOST = ENV.fetch(
      'DATADOG_AGENT_HOST',
      ::Datadog::Statsd::UDPConnection::DEFAULT_HOST,
    )

    DEFAULT_PORT = Integer(
      ENV.fetch(
        'DATADOG_AGENT_PORT',
        ::Datadog::Statsd::UDPConnection::DEFAULT_PORT,
      ),
    )

    DEFAULT_TAGS = %W[
      env:#{ENV.fetch('PERCY_ENV', 'development')}
    ].freeze

    def initialize(
      host = DEFAULT_HOST,
      port = DEFAULT_PORT,
      tags: DEFAULT_TAGS,
      **kwargs
    )
      super(host, port, tags: tags, **kwargs)
    end

    # Equivalent to stats.time, but without wrapping in blocks and dealing with
    # var scoping issues.
    #
    # @example Report the time taken to activate an account.
    #   stats.start_timing
    #   account.activate!
    #   stats.stop_timing('account.activate')
    def start_timing
      @_timing_start = now
      true
    end

    def stop_timing(stat, options = {})
      # Programmer mistake, so raise an error.
      raise 'no timing started' unless @_timing_start

      time_since_monotonic(stat, @_timing_start, options)
      @_timing_start = nil
      true
    end

    # dogstatsd uses a monotonic (linearly increasing) clock to calculate time
    # intervals, so this should be used where necessary. However, it's not
    # possible to compare monotonic time values with fixed times, so both are
    # available.
    def time_since_monotonic(stat, start, opts = {})
      unless start.instance_of? Float
        raise ArgumentError, 'start value must be Float'
      end

      timing(stat, ((now.to_f - start.to_f) * 1000).round, opts)
    end

    def time_since(stat, start, opts = {})
      unless start.instance_of? Time
        raise ArgumentError, 'start value must be Time'
      end

      timing(stat, ((Time.now.to_f - start.to_f) * 1000).round, opts)
    end

    private def now
      Process.clock_gettime(Process::CLOCK_MONOTONIC)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
percy-common-3.1.9 lib/percy/stats.rb
percy-common-3.1.8 lib/percy/stats.rb
percy-common-3.1.7.pre.beta lib/percy/stats.rb
percy-common-3.1.6 lib/percy/stats.rb
percy-common-3.1.5 lib/percy/stats.rb
percy-common-3.1.4 lib/percy/stats.rb