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: AggregationKey, AverageAggregator, LibratoMetricsMetrics, MaxAggregator, SumAggregator

Instance Attribute Summary

Attributes included from MetricsMixin

#metrics

Instance Method Summary collapse

Methods included from MetricsMixin

#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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 51

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
    value = d.value * d.count
    source = d.metrics.source
    [key, partitioned_time, value, source].to_msgpack(out)
  }
  out
end

#new_metrics(conf, pattern) ⇒ Object

override



45
46
47
48
49
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 45

def new_metrics(conf, pattern)
  m = LibratoMetricsMetrics.new(pattern)
  m.configure(conf)
  m
end

#write(chunk) ⇒ Object



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fluent/plugin/out_librato_metrics.rb', line 70

def write(chunk)
  counters = {}  #=> {AggregationKey => XxxAggregator}
  gauges = {}    #=> {AggregationKey => XxxAggregator}

  chunk.msgpack_each {|key,time,value,source|
    #if m = /^counter\.(.*)$/.match(key)
    #  # FIXME what's the actual behavior of counters?
    #  name = m[1]
    #  k = AggregationKey.new(name, time, source)
    #  (counters[k] ||= MaxAggregator.new).add(value)
    #elsif m = /^gauge\.(.*)$/.match(key)
    #  # FIXME what's the actual behavior of gauges?
    #  name = m[1]
    #  k = AggregationKey.new(name, time, source)
    #  (gauges[k] ||= AverageAggregator.new).add(value)
    #else
      name = key
      k = AggregationKey.new(name, time, source)
      (gauges[k] ||= SumAggregator.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,k_aggrs|
      # send upto 10 entries at once
      k_aggrs.each_slice(10) {|k_aggrs_slice|
        req = Net::HTTP::Post.new('/v1/metrics', header)
        req.basic_auth @librato_user, @librato_token

        params = {}
        params['source'] = @source if @source  # default source

        k_aggrs_slice.each_with_index {|(k,aggr),i|
          params["#{type}[#{i}][name]"] = k.name
          params["#{type}[#{i}][measure_time]"] = k.time.to_s
          params["#{type}[#{i}][source]"] = k.source if k.source
          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