Module: InstStatsd

Defined in:
lib/inst_statsd/statsd.rb,
lib/inst_statsd.rb,
lib/inst_statsd/event.rb,
lib/inst_statsd/counter.rb,
lib/inst_statsd/version.rb,
lib/inst_statsd/block_stat.rb,
lib/inst_statsd/null_logger.rb,
lib/inst_statsd/sql_tracker.rb,
lib/inst_statsd/request_stat.rb,
lib/inst_statsd/block_tracking.rb,
lib/inst_statsd/request_logger.rb,
lib/inst_statsd/default_tracking.rb,
lib/inst_statsd/request_tracking.rb

Overview

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.. }
InstStatsd::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 dog_tags is set in statsd.yml, it’ll use the tags param and will use Data Dog instead of Statsd

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

Defined Under Namespace

Modules: Event, Statsd Classes: BlockStat, BlockTracking, ConfigurationError, Counter, DefaultTracking, NullLogger, RequestLogger, RequestStat, RequestTracking, SqlTracker

Constant Summary collapse

VALID_SETTINGS =
%i[host
port
namespace
append_hostname
mask
negative_mask
batch_size
batch_byte_size
dog_tags
socket_path].freeze
VERSION =
"3.1.0"

Class Method Summary collapse

Class Method Details

.convert_bool(hash, key) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/inst_statsd.rb', line 73

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 InstStatsd::ConfigurationError, message
  end
  hash[key] = ["true", "True", true].include?(value)
end

.env_settings(env = ENV) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/inst_statsd.rb', line 55

def env_settings(env = ENV)
  dog_tags = JSON.parse(env["INST_DOG_TAGS"]).to_h if env["INST_DOG_TAGS"]
  config = {
    host: env.fetch("INST_STATSD_HOST", nil),
    port: env.fetch("INST_STATSD_PORT", nil),
    namespace: env.fetch("INST_STATSD_NAMESPACE", nil),
    append_hostname: env.fetch("INST_STATSD_APPEND_HOSTNAME", nil),
    dog_tags: dog_tags
  }
  config.compact!
  convert_bool(config, :append_hostname)
  if config[:host] || config[:dog_tags]
    config
  else
    {}
  end
end

.settingsObject



32
33
34
# File 'lib/inst_statsd.rb', line 32

def settings
  @settings ||= env_settings
end

.settings=(value) ⇒ Object



36
37
38
# File 'lib/inst_statsd.rb', line 36

def settings=(value)
  @settings = validate_settings(value)
end

.validate_settings(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inst_statsd.rb', line 40

def validate_settings(value)
  return nil if value.nil?

  validated = {}
  regexp_methods = %i[mask negative_mask]
  value.each do |k, v|
    raise InstStatsd::ConfigurationError, "Invalid key: #{k}" unless VALID_SETTINGS.include?(k.to_sym)

    v = Regexp.new(v) if regexp_methods.include?(k.to_sym) && v.is_a?(String)
    validated[k.to_sym] = v
  end

  env_settings.merge(validated)
end