Class: Sapience::Appender::Statsd

Inherits:
Subscriber show all
Defined in:
lib/sapience/appender/statsd.rb

Overview

Example:

Sapience.add_appender(:statsd, {url: "udp://localhost:2222"})

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 = {}, &block) ⇒ Statsd

Create Appender

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


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sapience/appender/statsd.rb', line 22

def initialize(options = {}, &block)
  options = options.is_a?(Hash) ? options.dup : { level: options }
  url     = options.delete(:url) || "udp://localhost:8125"
  uri     = URI.parse(url)
  fail('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 != ""

  super(options, &block)
end

Instance Method Details

#decrement(metric, amount) ⇒ Object



63
64
65
66
67
# File 'lib/sapience/appender/statsd.rb', line 63

def decrement(metric, amount)
  @statsd.batch do
    amount.abs.times { @statsd.decrement(metric) }
  end
end

#increment(metric, amount) ⇒ Object



57
58
59
60
61
# File 'lib/sapience/appender/statsd.rb', line 57

def increment(metric, amount)
  @statsd.batch do
    amount.times { @statsd.increment(metric) }
  end
end

#log(log) ⇒ Object

Send an error notification to sentry



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sapience/appender/statsd.rb', line 36

def log(log)
  metric = log.metric
  return false unless metric

  if log.duration
    timing(metric, log.duration)
  else
    amount = (log.metric_amount || 1).round
    if amount < 0
      decrement(metric, amount)
    else
      increment(metric, amount)
    end
  end
  true
end

#timing(metric, duration) ⇒ Object



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

def timing(metric, duration)
  @statsd.timing(metric, duration)
end