Module: InstStatsd::Statsd

Defined in:
lib/inst_statsd/statsd.rb

Class Method Summary collapse

Class Method Details

.append_hostname?Boolean



132
133
134
# File 'lib/inst_statsd/statsd.rb', line 132

def self.append_hostname?
  @append_hostname
end

.batchObject



94
95
96
97
98
99
100
101
102
# File 'lib/inst_statsd/statsd.rb', line 94

def self.batch
  return yield unless (old_instance = instance)
  old_instance.batch do |batch|
    Thread.current[:inst_statsd] = batch
    yield
  end
ensure
  Thread.current[:inst_statsd] = old_instance
end

.convert_tags(tags) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/inst_statsd/statsd.rb', line 75

def self.convert_tags(tags)
  new_tags = []
  if tags.is_a? Hash
    tags.each do |tag, v|
      new_tags << "#{tag}:#{v}"
    end
  else
    return tags
  end
  new_tags
end

.data_dog?Boolean



136
137
138
# File 'lib/inst_statsd/statsd.rb', line 136

def self.data_dog?
  @data_dog
end

.dog_tagsObject



34
35
36
# File 'lib/inst_statsd/statsd.rb', line 34

def self.dog_tags
  @dog_tags ||= {}
end

.escape(str, replacement = '_') ⇒ Object

replace “.” in key names with another character to avoid creating spurious sub-folders in graphite



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

def self.escape(str, replacement = '_')
  str.respond_to?(:gsub) ? str.gsub('.', replacement) : str
end

.hostnameObject



30
31
32
# File 'lib/inst_statsd/statsd.rb', line 30

def self.hostname
  @hostname ||= Socket.gethostname.split('.').first
end

.instanceObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/inst_statsd/statsd.rb', line 104

def self.instance
  thread_statsd = Thread.current[:inst_statsd]
  return thread_statsd if thread_statsd

  unless defined?(@statsd)
    statsd_settings = InstStatsd.settings
    if statsd_settings.key?(:dog_tags)
      @data_dog = true
      host = statsd_settings[:host] || 'localhost'
      port = statsd_settings[:port] || 8125
      require 'datadog/statsd'
      @statsd = ::Datadog::Statsd.new(host, port)
      self.dog_tags.replace(statsd_settings[:dog_tags] || {})
      @append_hostname = !statsd_settings.key?(:append_hostname) || !!statsd_settings[:append_hostname]
    elsif statsd_settings && statsd_settings[:host]
      @statsd = ::Statsd.new(statsd_settings[:host])
      @statsd.port = statsd_settings[:port] if statsd_settings[:port]
      @statsd.namespace = statsd_settings[:namespace] if statsd_settings[:namespace]
      @statsd.batch_size = statsd_settings[:batch_size] if statsd_settings.key?(:batch_size)
      @statsd.batch_byte_size = statsd_settings[:batch_byte_size] if statsd_settings.key?(:batch_byte_size)
      @append_hostname = !statsd_settings.key?(:append_hostname) || !!statsd_settings[:append_hostname]
    else
      @statsd = nil
    end
  end
  @statsd
end

.reset_instanceObject



140
141
142
143
# File 'lib/inst_statsd/statsd.rb', line 140

def self.reset_instance
  remove_instance_variable(:@statsd) if defined?(@statsd)
  Thread.current[:inst_statsd] = nil
end

.time(stat, sample_rate = 1, tags: {}, short_stat: nil) ⇒ Object



87
88
89
90
91
92
# File 'lib/inst_statsd/statsd.rb', line 87

def self.time(stat, sample_rate = 1, tags: {}, short_stat: nil)
  start = Time.now
  result = yield
  timing(stat, ((Time.now - start) * 1000).round, sample_rate, tags: tags, short_stat: short_stat)
  result
end