Class: Lookout::StatsdClient

Inherits:
Object
  • Object
show all
Defined in:
lib/lookout/statsd.rb

Direct Known Subclasses

Batch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StatsdClient

Returns a new instance of StatsdClient.



42
43
44
45
46
47
48
49
50
51
# File 'lib/lookout/statsd.rb', line 42

def initialize(opts={})
  @host = opts[:host] || 'localhost'
  @port = opts[:port] || 8125
  @batch_size = opts[:batch_size] || 10
  @prefix = opts[:prefix]
  # set resolve_always to true unless localhost or specified
  @resolve_always = opts.fetch(:resolve_always, !is_localhost?)
  @socket = UDPSocket.new
  @send_data = send_method
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



40
41
42
# File 'lib/lookout/statsd.rb', line 40

def batch_size
  @batch_size
end

#hostObject

Returns the value of attribute host.



40
41
42
# File 'lib/lookout/statsd.rb', line 40

def host
  @host
end

#portObject

Returns the value of attribute port.



40
41
42
# File 'lib/lookout/statsd.rb', line 40

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



40
41
42
# File 'lib/lookout/statsd.rb', line 40

def prefix
  @prefix
end

#resolve_alwaysObject

Returns the value of attribute resolve_always.



40
41
42
# File 'lib/lookout/statsd.rb', line 40

def resolve_always
  @resolve_always
end

Instance Method Details

#batch {|Batch| ... } ⇒ Object

Creates and yields a Batch that can be used to batch instrument reports into larger packets. Batches are sent either when the packet is “full” (defined by batch_size), or when the block completes, whichever is the sooner.

Good artists copy github.com/reinh/statsd/blob/master/lib/statsd.rb#L410

Examples:

Batch two instument operations:

$statsd.batch do |batch|
  batch.increment 'sys.requests'
  batch.gauge {'user.count' => User.count}
end

Yields:

  • (Batch)

    a statsd subclass that collects and batches instruments



146
147
148
# File 'lib/lookout/statsd.rb', line 146

def batch(&block)
  Batch.new(self).easy(&block)
end

#decrement(stats, sample_rate = 1) ⇒ Object

stats can be a string or an array of strings



95
96
97
# File 'lib/lookout/statsd.rb', line 95

def decrement(stats, sample_rate = 1)
  update_counter stats, -1, sample_rate
end

#gauge(stat_or_stats, value = nil, opts = nil) ⇒ Object

stat_or_stats may either be a Hash OR a String. If it’s a String, then value must be specified. Other statsd client gems have mostly standardized on using the String+value format, but this gem traditionally supported just a Hash. This now supports both for compatibility.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lookout/statsd.rb', line 113

def gauge(stat_or_stats, value=nil, opts=nil)
  # Can't use duck-typing here, since String responds to :map
  if stat_or_stats.is_a?(Hash)
    send_stats(stat_or_stats.map { |s,val|
                 if @prefix
                   s = "#{@prefix}.#{s}"
                 end
                 "#{s}:#{val}|g"
               })
  else
    if @prefix
      stat_or_stats = "#{@prefix}.#{stat_or_stats}"
    end
    send_stats("#{stat_or_stats}:#{value}|g")
  end
end

#host_ip_addrObject



53
54
55
# File 'lib/lookout/statsd.rb', line 53

def host_ip_addr
  @host_ip_addr ||= Resolv.getaddress(host)
end

#increment(stats, sample_rate = 1) ⇒ Object

stats can be a string or an array of strings



90
91
92
# File 'lib/lookout/statsd.rb', line 90

def increment(stats, sample_rate = 1)
  update_counter stats, 1, sample_rate
end

#send_data(*args) ⇒ Object



130
131
132
# File 'lib/lookout/statsd.rb', line 130

def send_data(*args)
  @send_data.call(*args)
end

#time(stat, sample_rate = 1) ⇒ Object

stat to log timing for, from provided block



81
82
83
84
85
86
87
# File 'lib/lookout/statsd.rb', line 81

def time(stat, sample_rate = 1)
  start_time = Time.now.to_f
  value = yield
ensure
  timing(stat, ((Time.now.to_f - start_time) * 1000).floor, sample_rate)
  value
end

#timing(stat, time = nil, sample_rate = 1) ⇒ Object

stat to log timing for time is the time to log in ms



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lookout/statsd.rb', line 64

def timing(stat, time = nil, sample_rate = 1)
  value = nil
  if block_given?
    start_time = Time.now.to_f
    value = yield
    time = ((Time.now.to_f - start_time) * 1000).floor
  end

  if @prefix
    stat = "#{@prefix}.#{stat}"
  end

  send_stats("#{stat}:#{time}|ms", sample_rate)
  value
end

#update_counter(stats, delta = 1, sample_rate = 1) ⇒ Object Also known as: count

stats can be a string or array of strings



100
101
102
103
104
# File 'lib/lookout/statsd.rb', line 100

def update_counter(stats, delta = 1, sample_rate = 1)
  stats = Array(stats)
  p = @prefix ? "#{@prefix}." : '' # apply prefix to each
  send_stats(stats.map { |s| "#{p}#{s}:#{delta}|c" }, sample_rate)
end