Module: InstStatsd

Defined in:
lib/inst_statsd/statsd.rb,
lib/inst_statsd.rb,
lib/inst_statsd/counter.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: 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].freeze

Class Method Summary collapse

Class Method Details

.convert_bool(hash, key) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/inst_statsd.rb', line 64

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inst_statsd.rb', line 44

def env_settings(env = ENV)
  dog_tags = JSON.load(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.delete_if { |_k, v| v.nil? }
  convert_bool(config, :append_hostname)
  if config[:host]
    config
  elsif config[:dog_tags]
    config
  else
    {}
  end
end

.settingsObject



21
22
23
# File 'lib/inst_statsd.rb', line 21

def settings
  @settings ||= env_settings
end

.settings=(value) ⇒ Object



25
26
27
# File 'lib/inst_statsd.rb', line 25

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

.validate_settings(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inst_statsd.rb', line 29

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

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

  env_settings.merge(validated)
end