module CanvasStatsd

Proxy class to communicate messages to statsd Available statsd messages are described in:

https://github.com/etsy/statsd/blob/master/README.md
https://github.com/reinh/statsd/blob/master/lib/statsd.rb

So for instance:

ms = Benchmark.ms { ..code.. }
CanvasStatsd::Statsd.timing("my_stat", ms)

Configured in config/statsd.yml, see config/statsd.yml.example At least a host needs to be defined for the environment, all other config is optional

If a namespace is defined in statsd.yml, it'll be prepended to the stat name. The hostname of the server will be appended to the stat name, unless `append_hostname: false` is specified in the config. So if the namespace is “canvas” and the hostname is “app01”, the final stat name of “my_stat” would be “stats.canvas.my_stat.app01” (assuming the default statsd/graphite configuration)

If statsd isn't configured and enabled, then calls to CanvasStatsd::Statsd.* will do nothing and return nil

Constants

VALID_SETTINGS

Public Class Methods

convert_bool(hash, key) click to toggle source
# File lib/canvas_statsd.rb, line 55
def convert_bool(hash, key)
  value = hash[key]
  return if value.nil?
  unless ['true', 'True', 'false', 'False', true, false].include?(value)
    message = "#{key} must be a boolean, or the string representation of a boolean, got: #{value}"
    raise CanvasStatsd::ConfigurationError, message
  end
  hash[key] = ['true', 'True', true].include?(value)
end
env_settings(env=ENV) click to toggle source
# File lib/canvas_statsd.rb, line 43
def env_settings(env=ENV)
  config = {
    host: env.fetch('CANVAS_STATSD_HOST', nil),
    port: env.fetch('CANVAS_STATSD_PORT', nil),
    namespace: env.fetch('CANVAS_STATSD_NAMESPACE', nil),
    append_hostname: env.fetch('CANVAS_STATSD_APPEND_HOSTNAME', nil),
  }
  config.delete_if {|k,v| v.nil?}
  convert_bool(config, :append_hostname)
  config[:host] ? config : {}
end
settings() click to toggle source
# File lib/canvas_statsd.rb, line 20
def settings
  @settings ||= env_settings
end
settings=(value) click to toggle source
# File lib/canvas_statsd.rb, line 24
def settings=(value)
  @settings = validate_settings(value)
end
validate_settings(value) click to toggle source
# File lib/canvas_statsd.rb, line 28
def validate_settings(value)
  return nil if value.nil?
  
  validated = {}
  value.each do |k,v|
    if !VALID_SETTINGS.include?(k.to_sym)
      raise CanvasStatsd::ConfigurationError, "Invalid key: #{k}"
    end
    v = Regexp.new(v) if [:mask, :negative_mask].include?(k.to_sym) && v.is_a?(String)
    validated[k.to_sym] = v
  end
  
  env_settings.merge(validated)
end