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.



21
22
23
# File 'lib/fluent/plugin/out_mackerel.rb', line 21

def initialize
  super
end

Instance Attribute Details

#mackerelObject (readonly)

Returns the value of attribute mackerel.



14
15
16
# File 'lib/fluent/plugin/out_mackerel.rb', line 14

def mackerel
  @mackerel
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/fluent/plugin/out_mackerel.rb', line 25

def configure(conf)
  super

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

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

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

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

  if @hostid and @service
    raise Fluent::ConfigError, "Niether 'hostid' and 'service' cannot be specifed."
  end

  unless @hostid.nil?
    if matched = @hostid.match(/^\${tag_parts\[(\d+)\]}$/)
      hostid_idx = matched[1].to_i
      @hostid_processor = Proc.new{ |args| args[:tokens][hostid_idx] }
    else
      @hostid_processor = Proc.new{ @hostid }
    end
  end

  if @metrics_name
    @name_processor = @metrics_name.split('.').map{ |token|
      if token.start_with?('$')
        token = token[2..-2]
        if token == 'out_key'
          Proc.new{ |args| args[:out_key] }
        else
          idx = token.match(/\[(-?\d+)\]/)[1].to_i
          Proc.new{ |args| args[:tokens][idx] }
        end
      else
        Proc.new{ token }
      end
    }
  end
end

#format(tag, time, record) ⇒ Object



82
83
84
# File 'lib/fluent/plugin/out_mackerel.rb', line 82

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

#shutdownObject



78
79
80
# File 'lib/fluent/plugin/out_mackerel.rb', line 78

def shutdown
  super
end

#startObject



74
75
76
# File 'lib/fluent/plugin/out_mackerel.rb', line 74

def start
  super
end

#write(chunk) ⇒ Object



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
# File 'lib/fluent/plugin/out_mackerel.rb', line 86

def write(chunk)
  metrics = []
  chunk.msgpack_each do |(tag,time,record)|

    tokens = tag.split('.')
    out_keys.map do |key|
      name = @name_processor.nil? ? key :
        @name_processor.map{ |p| p.call(:out_key => key, :tokens => tokens) }.join('.')

      metric = {
        'value' => record[key].to_f,
        'time' => time,
        'name' => "%s.%s" % ['custom', name]
      }
      metric['hostId'] = @hostid_processor.call(:tokens => tokens) if @hostid

      metrics << metric
    end
  end

  begin
    while true
      partial = metrics.slice!(0, 100)
      if partial.empty?
        break
      end
      if @hostid
        @mackerel.post_metrics(partial)
      else
        @mackerel.post_service_metrics(@service, partial)
      end
    end
  rescue => e
    log.error("out_mackerel:", :error_class => e.class, :error => e.message)
  end
  metrics.clear
end