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.



26
27
28
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 26

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 30

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



106
107
108
109
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 106

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

#runObject



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
101
102
103
104
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 54

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



46
47
48
49
50
51
52
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 46

def shutdown
  log.trace "in_sendgrid_event: shutdown"
  super

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

#startObject



39
40
41
42
43
44
# File 'lib/fluent/plugin/in_sendgrid_event.rb', line 39

def start
  log.trace "in_sendgrid_event: start"
  super

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