Class: Fluent::SendGridEventInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_sendgrid_event.rb

Instance Method Summary collapse

Constructor Details

#initializeSendGridEventInput

Returns a new instance of SendGridEventInput.



22
23
24
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 22

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 26

def configure(conf)
  log.trace "in_sendgrid_event: configure"
  super

  if @tag.nil?
    raise Fluent::ConfigError, "sendgrid_event: 'tag' parameter is required"
  end
end

#emit_event(event) ⇒ Object

The end of run method



102
103
104
105
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 102

def emit_event(event)
  log.trace "in_sendgrid_event: emit_event"
  Engine.emit("#{tag}", Engine.now, event)
end

#runObject



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
100
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 50

def run
  log.trace "in_sendgrid_event: run"
  listen = {
    :BindAddress => @host,
    :Port => @port
  }
  if @ssl
    if File.exists?(@certificate) && File.exists?(@private_key)
      listen[:SSLEnable] = @ssl
      listen[:SSLCertificate] = OpenSSL::X509::Certificate.new(open(@certificate).read)
      listen[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(open(@private_key).read)
    else
      log.error "in_sendgrid_event: couldn't find certificate: '#{@certificate}' or ssl key: '#{@private_key}'"
    end
  end
  if @username && @password
    listen[:RequestCallback] = lambda do |req, res|
      WEBrick::HTTPAuth.basic_auth(req, res, "fluent-plugin-sendgrid-event") do |username, password|
        username == @username && password == @password
      end
    end
  end

  @server = WEBrick::HTTPServer.new(listen)
  @server.mount_proc(@request_uri) do |req, res|
    begin
      if req.request_method == "POST" && req.body
        events = JSON.parse(req.body)
        events.each do |event|
          emit_event(event)
        end
        log.trace "in_sendgrid_event: success"
        res.status = 200
      else
        log.error "in_sendgrid_event: invalid request"
        res.status = 400
      end
    rescue JSON::ParserError => e
      log.error "in_sendgrid_event: #{e}"
      res.status = 400
    rescue WEBrick::HTTPStatus::LengthRequired => e
      log.error "in_sendgrid_event: #{e}"
      res.status = 411
    rescue Exception => e
      log.warn "in_sendgrid_event: Retry: Reason: #{e}"
      log.warn "#{e.backtrace.join('\n')}"
      res.status = 503
    end
  end
  @server.start
end

#shutdownObject



42
43
44
45
46
47
48
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 42

def shutdown
  log.trace "in_sendgrid_event: shutdown"
  super

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

#startObject



35
36
37
38
39
40
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 35

def start
  log.trace "in_sendgrid_event: start"
  super

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