Module: InstStatsd::Statsd

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

Constant Summary

Constants included from Event

Event::DEPLOY_EVENT, Event::FEATURE_EVENT, Event::PROVISION_EVENT, Event::REFRESH_EVENT, Event::SUPPORTED_TYPES

Class Method Summary collapse

Methods included from Event

data_dog?, dog_tags, event, instance

Methods included from Distribution

distributed_increment, distribution

Class Method Details

.append_hostname?Boolean

Returns:

  • (Boolean)


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

def self.append_hostname?
  @append_hostname
end

.batchObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/inst_statsd/statsd.rb', line 102

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



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

def self.convert_tags(tags)
  new_tags = []
  return tags unless tags.is_a? Hash

  tags.each do |tag, v|
    new_tags << "#{tag}:#{v}"
  end

  new_tags
end

.data_dog?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/inst_statsd/statsd.rb', line 150

def self.data_dog?
  @data_dog
end

.dog_tagsObject



43
44
45
# File 'lib/inst_statsd/statsd.rb', line 43

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



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

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

.hostnameObject



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

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

.initialized?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/inst_statsd/statsd.rb', line 154

def self.initialized?
  defined?(@statsd)
end

.instanceObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/inst_statsd/statsd.rb', line 113

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
      socket_path = statsd_settings[:socket_path]
      require "datadog/statsd"
      @statsd = if socket_path
                  Datadog::Statsd.new(socket_path: socket_path, namespace: statsd_settings[:namespace])
                else
                  Datadog::Statsd.new(host, port, namespace: statsd_settings[:namespace])
                end
      dog_tags.replace(statsd_settings[:dog_tags] || {})
      @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[:append_hostname]
    else
      @statsd = nil
    end
  end
  @statsd
end

.reset_instanceObject



158
159
160
161
# File 'lib/inst_statsd/statsd.rb', line 158

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



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

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