Sha256: 8f504a54f0b160242228609f8abe5bfba60247d0dab8b5ec0c63a601c2a2d673

Contents?: true

Size: 1.95 KB

Versions: 9

Compression:

Stored size: 1.95 KB

Contents

require 'datadog/statsd'
require 'time'

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

    DEFAULT_PORT = Integer(
      ENV.fetch(
        'DATADOG_AGENT_PORT',
        ::Datadog::Statsd::Connection::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

9 entries across 9 versions & 1 rubygems

Version Path
percy-common-3.1.3 lib/percy/stats.rb
percy-common-3.1.2 lib/percy/stats.rb
percy-common-3.1.2.pre.rc.3 lib/percy/stats.rb
percy-common-3.1.2.pre.rc.2 lib/percy/stats.rb
percy-common-3.1.2.rc.1 lib/percy/stats.rb
percy-common-3.1.1 lib/percy/stats.rb
percy-common-3.1.0 lib/percy/stats.rb
percy-common-3.0.2 lib/percy/stats.rb
percy-common-3.0.1 lib/percy/stats.rb