lib/percy/stats.rb in percy-common-2.1.1 vs lib/percy/stats.rb in percy-common-3.0.0
- old
+ new
@@ -1,43 +1,60 @@
require 'datadog/statsd'
require 'time'
module Percy
class Stats < ::Datadog::Statsd
- def initialize(host = nil, port = nil, opts = {}, max_buffer_size = 50)
- host ||= ENV.fetch('DATADOG_AGENT_HOST', ::Datadog::Statsd::DEFAULT_HOST)
- port ||= Integer(ENV.fetch('DATADOG_AGENT_PORT', ::Datadog::Statsd::DEFAULT_PORT))
- opts[:tags] ||= []
- opts[:tags] << "env:#{ENV['PERCY_ENV'] || 'development'}"
- retry_delay = opts[:retry_delay] || 1
- retry_count = opts[:retry_count] || 3
- retries = 0
+ DEFAULT_HOST = ENV.fetch(
+ 'DATADOG_AGENT_HOST',
+ ::Datadog::Statsd::Connection::DEFAULT_HOST,
+ )
- begin
- super(host, port, opts, max_buffer_size)
- rescue SocketError
- host = 'localhost' if retries >= retry_count
- sleep retry_delay
- retries += 1
- retry
- end
+ 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.
+ # 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 = Time.now
+ @_timing_start = now
true
end
def stop_timing(stat, options = {})
- raise 'no timing started' unless @_timing_start # Programmer mistake, so raise an error.
+ # Programmer mistake, so raise an error.
+ raise 'no timing started' unless @_timing_start
+
time_since(stat, @_timing_start, options)
@_timing_start = nil
true
+ end
+
+ def time_since(stat, start, opts = {})
+ timing(stat, ((now.to_f - start.to_f) * 1000).round, opts)
+ end
+
+ private def now
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end