Class: Statsd_Taggable::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd_taggable.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
# File 'lib/statsd_taggable.rb', line 5

def initialize(config)
  @host = config[:host] || 'localhost'
  @port = (config[:port] || 8125).to_i
  @app_name = config[:app_name] || "my_app"
  @tag_prefix = config[:tag_prefix] || '_t_'
  @client = Statsd.new(@host,@port)
end

Instance Method Details

#basic_metricsObject

TODO: consider extracting



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/statsd_taggable.rb', line 36

def basic_metrics
  rss, pcpu = `ps -o rss=,pcpu= -p #{Process.pid}`.split
  rss = rss.to_i
  pcpu = pcpu.to_f

  tags = expand_tags({})
  puts "Basic rss,pcpu: #{rss} #{pcpu}"

  gauge "statsd.apps.rss",  tags, rss
  gauge "statsd.apps.pcpu", tags, pcpu
end

#encode_tags(metric, tags) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/statsd_taggable.rb', line 17

def encode_tags(metric, tags)
  terms = [metric]
  tags.each do |k,v|
    terms << "#{@tag_prefix}#{sanitize(k)}.#{sanitize(v)}"
  end
  terms.join(".")
end

#expand_tags(tags) ⇒ Object



25
26
27
28
29
# File 'lib/statsd_taggable.rb', line 25

def expand_tags tags
  tags['host'] = `hostname`.strip
  tags['app']  = @app_name
  tags
end

#gauge(metric, tags, value) ⇒ Object



13
14
15
# File 'lib/statsd_taggable.rb', line 13

def gauge(metric, tags, value)
  @client.gauge(encode_tags(metric, tags), value)
end

#sanitize(v) ⇒ Object



31
32
33
# File 'lib/statsd_taggable.rb', line 31

def sanitize v
  v.to_s.gsub ".", "_"
end