Class: Fluent::WebhookMackerelInput

Inherits:
Input
  • Object
show all
Includes:
DetachProcessMixin
Defined in:
lib/fluent/plugin/in_webhook_mackerel.rb

Instance Method Summary collapse

Instance Method Details

#process(payload) ⇒ Object



51
52
53
54
55
56
# File 'lib/fluent/plugin/in_webhook_mackerel.rb', line 51

def process(payload)
  event = payload.delete "event"
  payload[:event] = event
  $log.info "tag: #{@tag.dup}.#{event}, payload:#{payload}"
  Engine.emit("#{@tag.dup}.#{event}", Engine.now, payload)
end

#runObject



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

def run
  @server = WEBrick::HTTPServer.new(
    :BindAddress => @bind,
    :Port => @port,
  )
  $log.debug "Listen on http://#{@bind}:#{@port}#{@mount}"
  @server.mount_proc(@mount) do |req, res|
    begin
      $log.debug req.header

      if req.request_method != "POST"
        res.status = 405
      else
        payload = JSON.parse(req.body)
        process(payload)
        res.status = 204
      end
    rescue => e
      $log.error e.inspect
      $log.error e.backtrace.join("\n")
      res.status = 400
    end
  end
  @server.start
end

#shutdownObject



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

def shutdown
  @server.shutdown
  Thread.kill(@thread)
end

#startObject



16
17
18
# File 'lib/fluent/plugin/in_webhook_mackerel.rb', line 16

def start
  @thread = Thread.new(&method(:run))
end