Class: Fluent::MackerelOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_mackerel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMackerelOutput

Returns a new instance of MackerelOutput.



23
24
25
# File 'lib/fluent/plugin/out_mackerel.rb', line 23

def initialize
  super
end

Instance Attribute Details

#mackerelObject (readonly)

Returns the value of attribute mackerel.



16
17
18
# File 'lib/fluent/plugin/out_mackerel.rb', line 16

def mackerel
  @mackerel
end

Instance Method Details

#configure(conf) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fluent/plugin/out_mackerel.rb', line 27

def configure(conf)
  super

  @mackerel = Mackerel.new(conf['api_key'])
  @out_keys = @out_keys.split(',')

  if @flush_interval < 60
    log.info("flush_interval less than 60s is not allowed and overwriteen to 60s")
    @flush_interval = 60
  end

  if @hostid.nil? and @hostid_path.nil?
    raise Fluent::ConfigError, "Either 'hostid' or 'hostid_path' must be specifed."
  end

  unless @hostid_path.nil?
    @hostid = File.open(@hostid_path).read
  end

  log.info("metrics_prefix is configured to #{@metrics_prefix}")
end

#format(tag, time, record) ⇒ Object



57
58
59
# File 'lib/fluent/plugin/out_mackerel.rb', line 57

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#shutdownObject



53
54
55
# File 'lib/fluent/plugin/out_mackerel.rb', line 53

def shutdown
  super
end

#startObject



49
50
51
# File 'lib/fluent/plugin/out_mackerel.rb', line 49

def start
  super
end

#write(chunk) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fluent/plugin/out_mackerel.rb', line 61

def write(chunk)
  metrics = []
  chunk.msgpack_each do |(tag,time,record)|
    out_keys.map do |key|
      metrics << {
        'hostId' => @hostid,
        'value' => record[key].to_f,
        'time' => time,
        'name' => "%s.%s" % [@metrics_prefix, key]
      }
    end
  end

  begin
    @mackerel.post_metrics(metrics) unless metrics.empty?
  rescue => e
    log.error("out_mackerel:", :error_class => e.class, :error => e.message)
  end
  metrics.clear
end