Class: SemanticLogger::Metrics::Statsd

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/metrics/statsd.rb

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level

Methods inherited from Base

#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(options = {}) ⇒ Statsd

Create Statsd metrics subscriber

Parameters:

url: [String]
  Valid URL to post to.
  Example:
    udp://localhost:8125
  Example, send all metrics to a particular namespace:
    udp://localhost:8125/namespace
  Default: udp://localhost:8125

Example:

subscriber = SemanticLogger::Metrics::Statsd.new(url: 'udp://localhost:8125')
SemanticLogger.on_metric(subscriber)


25
26
27
28
29
30
31
32
33
34
# File 'lib/semantic_logger/metrics/statsd.rb', line 25

def initialize(options = {})
  options = options.dup
  @url    = options.delete(:url) || 'udp://localhost:8125'
  uri     = URI.parse(@url)
  raise('Statsd only supports udp. Example: "udp://localhost:8125"') if uri.scheme != 'udp'

  @statsd           = ::Statsd.new(uri.host, uri.port)
  path              = uri.path.chomp('/')
  @statsd.namespace = path.sub('/', '') if path != ''
end

Instance Method Details

#call(log) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/semantic_logger/metrics/statsd.rb', line 36

def call(log)
  metric = log.metric
  if duration = log.duration
    @statsd.timing(metric, duration)
  else
    amount = (log.metric_amount || 1).round
    if amount < 0
      amount.times { @statsd.decrement(metric) }
    else
      amount.times { @statsd.increment(metric) }
    end
  end
end