Module: InstStatsd::Statsd

Extended by:
Event
Defined in:
lib/inst_statsd/statsd.rb

Constant Summary

Constants included from Event

Event::SUPPORTED_TYPES

Class Method Summary collapse

Methods included from Event

data_dog?, dog_tags, event, instance

Class Method Details

.append_hostname? ⇒ Boolean

Returns:

  • (Boolean)


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

def self.append_hostname?
  @append_hostname
end

.batch ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/inst_statsd/statsd.rb', line 98

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



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/inst_statsd/statsd.rb', line 79

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

Returns:

  • (Boolean)


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

def self.data_dog?
  @data_dog
end

.dog_tags ⇒ Object



38
39
40
# File 'lib/inst_statsd/statsd.rb', line 38

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



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

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

.hostname ⇒ Object



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

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

.instance ⇒ Object



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

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, namespace: statsd_settings[:namespace])
      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_instance ⇒ Object



144
145
146
147
# File 'lib/inst_statsd/statsd.rb', line 144

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



91
92
93
94
95
96
# File 'lib/inst_statsd/statsd.rb', line 91

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