Class: Fluent::Plugin::MackerelOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::MackerelOutput
- Defined in:
- lib/fluent/plugin/out_mackerel.rb
Constant Summary collapse
- DEFAULT_FLUSH_INTERVAL =
60
- MAX_BUFFER_CHUNK_LIMIT =
100 * 1024
Instance Attribute Summary collapse
-
#mackerel ⇒ Object
readonly
Returns the value of attribute mackerel.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
- #formatted_to_msgpack_binary ⇒ Object
- #generate_metric(key, tokens, time, value) ⇒ Object
-
#initialize ⇒ MackerelOutput
constructor
A new instance of MackerelOutput.
- #send(metrics) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ MackerelOutput
Returns a new instance of MackerelOutput.
37 38 39 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 37 def initialize super end |
Instance Attribute Details
#mackerel ⇒ Object (readonly)
Returns the value of attribute mackerel.
30 31 32 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 30 def mackerel @mackerel end |
Instance Method Details
#configure(conf) ⇒ Object
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 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 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 41 def configure(conf) compat_parameters_convert(conf, :buffer) super @mackerel = Mackerel::Client.new(:mackerel_api_key => conf['api_key'], :mackerel_origin => conf['origin']) if @out_keys @out_keys = @out_keys.split(',') end if @out_key_pattern @out_key_pattern = Regexp.new(@out_key_pattern) end if @out_keys.nil? and @out_key_pattern.nil? raise Fluent::ConfigError, "Either 'out_keys' or 'out_key_pattern' must be specifed." end if @buffer_config.flush_interval and @buffer_config.flush_interval < 60 raise Fluent::ConfigError, "flush_interval less than 60s is not allowed." 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 if @remove_prefix and @service.nil? raise Fluent::ConfigError, "'remove_prefix' must be used with 'service'." 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| Proc.new{ |args| token.gsub(/\${(out_key|\[(-?\d+)\])}/) { if $1 == 'out_key' args[:out_key] else args[:tokens][$1[1..-1].to_i] end } } } end end |
#format(tag, time, record) ⇒ Object
113 114 115 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 113 def format(tag, time, record) [tag, time, record].to_msgpack end |
#formatted_to_msgpack_binary ⇒ Object
109 110 111 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 109 def formatted_to_msgpack_binary true end |
#generate_metric(key, tokens, time, value) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 117 def generate_metric(key, tokens, time, value) name = @name_processor.nil? ? key : @name_processor.map{ |p| p.call(:out_key => key, :tokens => tokens) }.join('.') metric = { 'value' => value, 'time' => time, 'name' => @remove_prefix ? name : "%s.%s" % ['custom', name] } metric['hostId'] = @hostid_processor.call(:tokens => tokens) if @hostid return metric end |
#send(metrics) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 167 def send(metrics) log.debug("out_mackerel: #{metrics}") begin if @hostid @mackerel.post_metrics(metrics) else @mackerel.post_service_metrics(@service, metrics) end rescue => e log.error("out_mackerel:", :error_class => e.class, :error => e.) raise e end end |
#shutdown ⇒ Object
105 106 107 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 105 def shutdown super end |
#start ⇒ Object
101 102 103 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 101 def start super end |
#write(chunk) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/fluent/plugin/out_mackerel.rb', line 130 def write(chunk) metrics = [] processed = {} = {} time_latest = 0 chunk.msgpack_each do |(tag,time,record)| [tag] = true tokens = tag.split('.') if @out_keys out_keys = @out_keys.select{|key| record.has_key?(key)} else # @out_key_pattern out_keys = record.keys.select{|key| @out_key_pattern.match(key)} end out_keys.map do |key| metrics << generate_metric(key, tokens, time, record[key].to_f) time_latest = time if time_latest == 0 || time_latest < time processed[tag + "." + key] = true end end if @out_keys && @use_zero_for_empty .each_key do |tag| tokens = tag.split('.') @out_keys.each do |key| unless processed[tag + "." + key] metrics << generate_metric(key, tokens, time_latest, 0.0) end end end end send(metrics) unless metrics.empty? metrics.clear end |