Module: InstStatsd::Statsd

Defined in:
lib/inst_statsd/statsd.rb

Class Method Summary collapse

Class Method Details

.append_hostname? ⇒ Boolean

Returns:

  • (Boolean)


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

def self.append_hostname?
  @append_hostname
end

.batch ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/inst_statsd/statsd.rb', line 62

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

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

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



23
24
25
# File 'lib/inst_statsd/statsd.rb', line 23

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

.hostname ⇒ Object



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

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

.instance ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inst_statsd/statsd.rb', line 72

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 && 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



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

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

.time(stat, sample_rate = 1) ⇒ Object



55
56
57
58
59
60
# File 'lib/inst_statsd/statsd.rb', line 55

def self.time(stat, sample_rate = 1)
  start = Time.now
  result = yield
  timing(stat, ((Time.now - start) * 1000).round, sample_rate)
  result
end