class Fluent::Plugin::MachinistOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_machinist.rb, line 12
def initialize
  super
  require "net/http"
  require "uri"
end

Public Instance Methods

create_payload(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_machinist.rb, line 56
def create_payload(tag, time, record)
  metrics = []
  @value_keys = [@value_key] if @value_key
  ns = if @namespace
         @namespace
       elsif @namespace_key
         record[@namespace_key]
       else
         nil
       end
  tags = if @tags
           @tags
         elsif @tag_keys
           @tag_keys.map{|key|
             [key, record[key].to_s]
           }.to_h
         else
           nil
         end

  metrics += @value_keys.map{|key|
    metric = {
      :name => key,
      :value => record[key].to_f
    }
    metric[:namespace] = ns if ns
    metric[:tags] = tags if tags
    metric
  }

  json = {
    :agent_id => @agent_id,
    :api_key => @api_key,
    :metrics => metrics
  }

  return JSON.dump(json)
end
handle_record(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_machinist.rb, line 50
def handle_record(tag, time, record)
  data = create_payload(tag, time, record)

  send_request(data)
end
process(tag, es) click to toggle source
# File lib/fluent/plugin/out_machinist.rb, line 44
def process(tag, es)
  es.each do |time, record|
    handle_record(tag, time, record)
  end
end
send_request(data) click to toggle source
# File lib/fluent/plugin/out_machinist.rb, line 95
def send_request(data)
  url = URI(@endpoint_url)
  http = Net::HTTP.new(url.host, url.port)
  if @use_ssl
    http.use_ssl = true
    http.ca_file = OpenSSL::X509::DEFAULT_CERT_FILE
    unless @verify_ssl
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end

  req = Net::HTTP::Post.new(url)
  req["content-type"] = 'application/json'

  req.body = data
  res = http.request(req)

  unless res and res.is_a?(Net::HTTPSuccess)
    summary = if res
                "#{res.code} #{res.message} #{res.body}"
              else
                "res=nil"
              end
    $log.warn "failed to #{req.method} #{url} #{summary}"
  end
rescue => e
  $log.warn "Net::HTTP.#{req.method.capitalize} raises exception : #{e.class}, '#{e.message}'"
  raise e
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_machinist.rb, line 40
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_machinist.rb, line 36
def start
  super
end