Class: Percy::Stats

Inherits:
Datadog::Statsd
  • Object
show all
Defined in:
lib/percy/stats.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, tags: DEFAULT_TAGS, **kwargs) ⇒ Stats

Returns a new instance of Stats.



22
23
24
25
26
27
28
29
# File 'lib/percy/stats.rb', line 22

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

Instance Method Details

#start_timingObject

Equivalent to stats.time, but without wrapping in blocks and dealing with var scoping issues.

Examples:

Report the time taken to activate an account.

stats.start_timing
.activate!
stats.stop_timing('account.activate')


38
39
40
41
# File 'lib/percy/stats.rb', line 38

def start_timing
  @_timing_start = now
  true
end

#stop_timing(stat, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/percy/stats.rb', line 43

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

#time_since(stat, start, opts = {}) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/percy/stats.rb', line 64

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

#time_since_monotonic(stat, start, opts = {}) ⇒ Object

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.



56
57
58
59
60
61
62
# File 'lib/percy/stats.rb', line 56

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