Class: Metrics::Reporters::Librato

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-metrics/reporters/librato.rb

Constant Summary collapse

API_URL =
"https://metrics-api.librato.com/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Librato

Returns a new instance of Librato.



13
14
15
16
17
18
19
# File 'lib/ruby-metrics/reporters/librato.rb', line 13

def initialize(options = {})
  @api_token = options[:api_token]
  @user = options[:user] 
  @headers = {
    'User-Agent' => "ruby-metrics #{Metrics::VERSION}"
  }
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



8
9
10
# File 'lib/ruby-metrics/reporters/librato.rb', line 8

def api_token
  @api_token
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/ruby-metrics/reporters/librato.rb', line 9

def user
  @user
end

Instance Method Details

#report(agent) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby-metrics/reporters/librato.rb', line 45

def report(agent)
  agent.instruments.each do |name, instrument|
    measure_time = Time.now.to_i

    case instrument
    when Metrics::Instruments::Counter
      send_data "#{API_URL}/counters/#{name}.json",
                :measure_time => measure_time,
                :value        => instrument.to_i

    when Metrics::Instruments::Gauge
      post_url = "#{API_URL}/gauges/#{name}.json"
      if instrument.get.is_a? Hash
        instrument.get.each do |key, value|
          send_data post_url,
                    :measure_time => measure_time,
                    :source       => key,
                    :value        => value
        end
      else
        send_data post_url,
                  :measure_time => measure_time,
                  :value        => instrument.get
      end

    when Metrics::Instruments::Timer
      [:count, :fifteen_minute_rate, :five_minute_rate, :one_minute_rate, :min, :max, :mean].each do |attribute|
        send_data "#{API_URL}/gauges/#{name}.json",
                  :measure_time => measure_time,
                  :source       => attribute,
                  :value        => instrument.send(attribute)
      end

    else
      puts "Unhandled instrument"
    end
  end
end

#send_data(post_url, post_data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby-metrics/reporters/librato.rb', line 21

def send_data(post_url, post_data)
  url = URI.parse(post_url)
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth @user, @api_token
  @headers.each do |k,v|
    req.add_field(k, v)
  end
  req.set_form_data(post_data)
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  #https.set_debug_output($stdout)
      
  https.start do |http| 
    result = http.request(req) 
    case result
    when Net::HTTPCreated
      # OK
      puts "SENT!"
    else
      puts "FAILED TO SEND: #{https.inspect}"
    end
  end
end