Class: Fluent::Plugin::MachinistOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_machinist.rb

Instance Method Summary collapse

Constructor Details

#initializeMachinistOutput

Returns a new instance of MachinistOutput.



12
13
14
15
16
# File 'lib/fluent/plugin/out_machinist.rb', line 12

def initialize
  super
  require "net/http"
  require "uri"
end

Instance Method Details

#create_payload(tag, time, record) ⇒ Object



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
# 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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



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
# 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

#shutdownObject



40
41
42
# File 'lib/fluent/plugin/out_machinist.rb', line 40

def shutdown
  super
end

#startObject



36
37
38
# File 'lib/fluent/plugin/out_machinist.rb', line 36

def start
  super
end