Class: Fluent::GrowthForecastOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::GrowthForecastOutput
- Defined in:
- lib/fluent/plugin/out_growthforecast.rb
Constant Summary collapse
- DEFAULT_GRAPH_PATH =
{ :ignore => '${service}/${section}/${key_name}', :service => '${tag}/${section}/${key_name}', :section => '${service}/${tag}/${key_name}', :name_prefix => '${service}/${section}/${tag}_${key_name}', }
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #connect_to(tag, name) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #format_url(tag, name) ⇒ Object
- #http_connection(host, port) ⇒ Object
-
#initialize ⇒ GrowthForecastOutput
constructor
A new instance of GrowthForecastOutput.
- #placeholder_mapping(tag, name) ⇒ Object
- #post(tag, name, value) ⇒ Object
- #post_events(events) ⇒ Object
-
#post_keepalive(events) ⇒ Object
[:tag=>”,:name=>”,:value=>X].
- #post_request(tag, name, value) ⇒ Object
- #poster ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ GrowthForecastOutput
Returns a new instance of GrowthForecastOutput.
4 5 6 7 8 9 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 4 def initialize super require 'net/http' require 'uri' require 'resolve/hostname' end |
Instance Method Details
#configure(conf) ⇒ Object
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 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 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 91 def configure(conf) super if @gfapi_url !~ /\/api\/\Z/ raise Fluent::ConfigError, "gfapi_url must end with /api/" end if not @graph_path.nil? and @graph_path !~ /^[^\/]+\/[^\/]+\/[^\/]+$/ raise Fluent::ConfigError, "graph_path must be like '${service}/${section}/${tag}_${key_name}'" end if @name_keys.nil? and @name_key_pattern.nil? raise Fluent::ConfigError, "missing both of name_keys and name_key_pattern" end if not @name_keys.nil? and not @name_key_pattern.nil? raise Fluent::ConfigError, "cannot specify both of name_keys and name_key_pattern" end if not @graphs.nil? and @name_keys.nil? raise Fluent::ConfigError, "graphs must be specified with name_keys" end if @name_keys @name_keys = @name_keys.split(',') end if @name_key_pattern @name_key_pattern = Regexp.new(@name_key_pattern) end if @graphs @graphs = @graphs.split(',') end if @name_keys and @graphs and @name_keys.size != @graphs.size raise Fluent::ConfigError, "sizes of name_keys and graphs do not match" end @mode = case @mode when 'count' then :count when 'modified' then :modified else :gauge end @tag_for = case @tag_for when 'ignore' then :ignore when 'section' then :section when 'service' then :service else :name_prefix end if @graph_path.nil? if @tag_for != :section and @section.nil? raise Fluent::ConfigError, "section parameter is needed when tag_for is not 'section'" end if @tag_for != :service and @service.nil? raise Fluent::ConfigError, "service parameter is needed when tag_for is not 'service'" end @graph_path = DEFAULT_GRAPH_PATH[@tag_for] end if @remove_prefix @removed_prefix_string = @remove_prefix + '.' @removed_length = @removed_prefix_string.length end @auth = case @authentication when 'basic' then :basic else :none end @resolver = Resolve::Hostname.new(:system_resolver => true) end |
#connect_to(tag, name) ⇒ Object
214 215 216 217 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 214 def connect_to(tag, name) url = URI.parse(format_url(tag,name)) return url.host, url.port end |
#emit(tag, es, chain) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 312 def emit(tag, es, chain) events = [] if @name_keys es.each {|time,record| @name_keys.each_with_index {|name, i| if value = record[name] events.push({:tag => tag, :name => (@graphs ? @graphs[i] : name), :value => value}) end } } else # for name_key_pattern es.each {|time,record| record.keys.each {|key| if @name_key_pattern.match(key) and record[key] events.push({:tag => tag, :name => key, :value => record[key]}) end } } end if @thread @mutex.synchronize do @queue += events end else begin post_events(events) rescue => e log.warn "HTTP POST Error occures to growthforecast server", :error_class => e.class, :error => e. raise if @retry end end chain.next end |
#format_url(tag, name) ⇒ Object
209 210 211 212 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 209 def format_url(tag, name) graph_path = @graph_path.gsub(/(\${[_a-z]+})/, placeholder_mapping(tag, name)) return @gfapi_url + URI.escape(graph_path) end |
#http_connection(host, port) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 219 def http_connection(host, port) http = Net::HTTP.new(@resolver.getaddress(host), port) if @timeout http.open_timeout = @timeout http.read_timeout = @timeout end if @ssl http.use_ssl = true unless @verify_ssl http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end http end |
#placeholder_mapping(tag, name) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 201 def placeholder_mapping(tag, name) if @remove_prefix and ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix) tag = tag[@removed_length..-1] end {'${service}' => @service, '${section}' => @section, '${tag}' => tag, '${key_name}' => name} end |
#post(tag, name, value) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 249 def post(tag, name, value) url = format_url(tag,name) res = nil begin host,port = connect_to(tag, name) req = post_request(tag, name, value) http = http_connection(host, port) res = http.start {|http| http.request(req) } rescue IOError, EOFError, SystemCallError # server didn't respond log.warn "net/http POST raises exception: #{$!.class}, '#{$!.}'" end unless res and res.is_a?(Net::HTTPSuccess) log.warn "failed to post to growthforecast: #{url}, number: #{value}, code: #{res && res.code}" end end |
#post_events(events) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 302 def post_events(events) if @keepalive post_keepalive(events) else events.each do |event| post(event[:tag], event[:name], event[:value]) end end end |
#post_keepalive(events) ⇒ Object
- :tag=>”,:name=>”,:value=>X
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 266 def post_keepalive(events) # [{:tag=>'',:name=>'',:value=>X}] return if events.size < 1 # gf host/port is same for all events (host is from configuration) host,port = connect_to(events.first[:tag], events.first[:name]) requests = events.map{|e| post_request(e[:tag], e[:name], e[:value])} http = nil requests.each do |req| begin unless http http = http_connection(host, port) http.start end res = http.request(req) unless res and res.is_a?(Net::HTTPSuccess) log.warn "failed to post to growthforecast: #{host}:#{port}#{req.path}, post_data: #{req.body} code: #{res && res.code}" end rescue IOError, EOFError, Errno::ECONNRESET, Errno::ETIMEDOUT, SystemCallError log.warn "net/http keepalive POST raises exception: #{$!.class}, '#{$!.}'" begin http.finish rescue => e # ignore all errors for connection with error end http = nil end end begin http.finish rescue => e # ignore end end |
#post_request(tag, name, value) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 234 def post_request(tag, name, value) url = URI.parse(format_url(tag,name)) req = Net::HTTP::Post.new(url.path) if @auth and @auth == :basic req.basic_auth(@username, @password) end req['Host'] = url.host if @keepalive req['Connection'] = 'Keep-Alive' end value = @enable_float_number ? value.to_f : value.to_i req.set_form_data({'number' => value, 'mode' => @mode.to_s}) req end |
#poster ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 182 def poster while @running if @queue.size < 1 sleep(0.2) next end events = @mutex.synchronize { es,@queue = @queue,[] es } begin post_events(events) if events.size > 0 rescue => e log.warn "HTTP POST in background Error occures to growthforecast server", :error_class => e.class, :error => e. end end end |
#shutdown ⇒ Object
176 177 178 179 180 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 176 def shutdown @running = false @thread.join if @thread super end |
#start ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fluent/plugin/out_growthforecast.rb', line 162 def start super @running = true @thread = nil @queue = nil @mutex = nil if @background_post @mutex = Mutex.new @queue = [] @thread = Thread.new(&method(:poster)) end end |