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)


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

def self.append_hostname?
  @append_hostname
end

.batchObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/inst_statsd/statsd.rb', line 110

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



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

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)


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

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)


162
163
164
# File 'lib/inst_statsd/statsd.rb', line 162

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

.instanceObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/inst_statsd/statsd.rb', line 121

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

.prepare_datadog_tags(tags) ⇒ Object



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

def self.prepare_datadog_tags(tags)
  tags = tags ? tags.dup : {}
  tags.merge!(dog_tags) if tags.is_a? Hash
  tags = convert_tags(tags)
  tags << "host:" unless append_hostname?

  tags
end

.reset_instanceObject



166
167
168
169
# File 'lib/inst_statsd/statsd.rb', line 166

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

.stat_name_for(metric) ⇒ Object



77
78
79
80
81
# File 'lib/inst_statsd/statsd.rb', line 77

def self.stat_name_for(metric)
  return "#{metric}.#{hostname}" if append_hostname?

  metric.to_s
end

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



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

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