Class: Fluent::LibratoMetricsOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
MetricsMixin
Defined in:
lib/fluent/plugin/out_librato_metrics.rb

Defined Under Namespace

Modules: MetricsMixin Classes: CounterAggregator, GaugeAggregator

Instance Attribute Summary

Attributes included from MetricsMixin

#metrics

Instance Method Summary collapse

Methods included from MetricsMixin

#add_metrics, #each_metrics

Constructor Details

#initializeLibratoMetricsOutput

Returns a new instance of LibratoMetricsOutput.



32
33
34
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 32

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



36
37
38
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 36

def configure(conf)
  super
end

#format(tag, time, record) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 40

def format(tag, time, record)
  out = ''.force_encoding('ASCII-8BIT')
  each_metrics(tag, time, record) {|d|
    name = d.name
    if d.each_keys.empty?
      key = name
    else
      key = "#{name}.#{d.keys.join('.')}"
    end
    partitioned_time = time / 60 * 60
    [key, partitioned_time, d.value*d.count].to_msgpack(out)
  }
  out
end

#write(chunk) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 55

def write(chunk)
  counters = {}  #=> {partitioned_time => {name => CounterAggregator}}
  gauges = {}    #=> {partitioned_time => {name => GaugeAggregator}}

  chunk.msgpack_each {|key,partitioned_time,value|
    if m = /^counter\.(.*)$/.match(key)
      name = m[1]
      ((counters[partitioned_time] ||= {})[name] ||= CounterAggregator.new).add(value)
    elsif m = /^gauge\.(.*)$/.match(key)
      name = m[1]
      ((gauges[partitioned_time] ||= {})[name] ||= GaugeAggregator.new).add(value)
    else
      name = key
      ((gauges[partitioned_time] ||= {})[name] ||= GaugeAggregator.new).add(value)
    end
  }

  #http = Net::HTTP.new('metrics-api.librato.com', 80)
  http = Net::HTTP.new('metrics-api.librato.com', 443)
  http.use_ssl = true
  #http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE  # TODO verify
  http.cert_store = OpenSSL::X509::Store.new
  header = {}

  begin
    {'counters'=>counters, 'gauges'=>gauges}.each_pair {|type,partitions|
      partitions.each_pair {|partitioned_time,name_aggrs|
        req = Net::HTTP::Post.new('/v1/metrics', header)
        req.basic_auth @librato_user, @librato_token

        params = {
          'measure_time' => partitioned_time.to_s,
        }
        params['source'] = @source if @source

        name_aggrs.each_with_index {|(name,aggr),i|
          params["#{type}[#{i}][name]"] = name
          params["#{type}[#{i}][value]"] = aggr.get.to_s
        }

        $log.trace { "librato metrics: #{params.inspect}" }
        req.set_form_data(params)
        res = http.request(req)

        # TODO error handling
        if res.code != "200"
          $log.warn "librato_metrics: #{res.code}: #{res.body}"
        end
      }
    }
  ensure
    http.finish if http.started?
  end
end