Class: Macmillan::Utils::StatsdDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/macmillan/utils/statsd_decorator.rb

Overview

Utility class to wrap the Statsd class from statsd-ruby.

This will allow you to log Statsd messages to your logs, but only really send messages to the StatsD server when running in a ‘production’ environment.

Usage:

Add ‘statsd-ruby’ and ‘macmillan-utils’ to your Gemfile:

gem 'statsd-ruby'
gem 'macmillan-utils', require: false

Then in your code:

require 'statsd'
require 'macmillan/utils/statsd_decorator'

statsd = Statsd.new('http://statsd.example.com', 8080)
statsd = Macmillan::Utils::StatsdDecorator.new(statsd, ENV['RACK_ENV'])

i.e. when using rails, use the rails env and logger:

statsd = Statsd.new('http://statsd.example.com', 8080)
statsd = Macmillan::Utils::StatsdDecorator.new(statsd, Rails.env, Rails.logger)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegatee, env = 'development', logger = Macmillan::Utils::Logger::Factory.build_logger) ⇒ Statsd

Builds a new instance of StatsdDecorator

Parameters:

  • delegatee (Statsd)

    a Statsd object

  • env (String) (defaults to: 'development')

    the current application environment - i.e. ‘development’ or ‘production’

  • logger (Logger) (defaults to: Macmillan::Utils::Logger::Factory.build_logger)

    a Logger object



43
44
45
46
47
# File 'lib/macmillan/utils/statsd_decorator.rb', line 43

def initialize(delegatee, env = 'development', logger = Macmillan::Utils::Logger::Factory.build_logger)
  @env    = env
  @logger = logger
  super(delegatee)
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



33
34
35
# File 'lib/macmillan/utils/statsd_decorator.rb', line 33

def env
  @env
end

#loggerObject

Returns the value of attribute logger.



33
34
35
# File 'lib/macmillan/utils/statsd_decorator.rb', line 33

def logger
  @logger
end

Instance Method Details

#count(stat, count, sample_rate = 1) ⇒ Object



59
60
61
62
# File 'lib/macmillan/utils/statsd_decorator.rb', line 59

def count(stat, count, sample_rate = 1)
  log_stat %{count - "#{stat}" #{count} (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end

#decrement(stat, sample_rate = 1) ⇒ Object



54
55
56
57
# File 'lib/macmillan/utils/statsd_decorator.rb', line 54

def decrement(stat, sample_rate = 1)
  log_stat %{decrement - "#{stat}" (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end

#guage(stat, value, sample_rate = 1) ⇒ Object



64
65
66
67
# File 'lib/macmillan/utils/statsd_decorator.rb', line 64

def guage(stat, value, sample_rate = 1)
  log_stat %{gauge - "#{stat}" #{value} (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end

#increment(stat, sample_rate = 1) ⇒ Object



49
50
51
52
# File 'lib/macmillan/utils/statsd_decorator.rb', line 49

def increment(stat, sample_rate = 1)
  log_stat %{increment - "#{stat}" (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end

#set(stat, value, sample_rate = 1) ⇒ Object



69
70
71
72
# File 'lib/macmillan/utils/statsd_decorator.rb', line 69

def set(stat, value, sample_rate = 1)
  log_stat %{set - "#{stat}" #{value} (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end

#time(stat, sample_rate = 1) ⇒ Object



79
80
81
82
83
84
# File 'lib/macmillan/utils/statsd_decorator.rb', line 79

def time(stat, sample_rate = 1)
  start    = Time.now
  result   = yield
  timing(stat, ((Time.now - start) * 1000).round, sample_rate)
  result
end

#timing(stat, ms, sample_rate = 1) ⇒ Object



74
75
76
77
# File 'lib/macmillan/utils/statsd_decorator.rb', line 74

def timing(stat, ms, sample_rate = 1)
  log_stat %{timing - "#{stat}" #{ms}ms (sample_rate: #{sample_rate})}
  super if send_to_delegatee?
end