Module: InstStatsd::Statsd

Extended by:
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

Class Method Details

.append_hostname?Boolean

Returns:

  • (Boolean)


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

def self.append_hostname?
  @append_hostname
end

.batchObject



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

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



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

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)


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

def self.data_dog?
  @data_dog
end

.dog_tagsObject



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

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



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

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

.hostnameObject



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

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

.initialized?Boolean

Returns:

  • (Boolean)


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

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

.instanceObject



112
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
# File 'lib/inst_statsd/statsd.rb', line 112

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



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

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



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

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