Class: Fluent::RollbarOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_rollbar.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

This method is called before starting. ‘conf’ is a Hash that includes configuration parameters. If the configuration is invalid, raise Fluent::ConfigError.



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

def configure(conf)
    super
    $log.info 'Rollbar Output initializing'
end

#format(tag, time, record) ⇒ Object

This method is called when an event reaches to Fluentd. Convert the event to a raw string.



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

def format(tag, time, record)
    # [tag, time, record].to_json + "\n"
    ## Alternatively, use msgpack to serialize the object.
    [tag, time, record].to_msgpack
end

#shutdownObject

This method is called when shutting down. Shutdown the thread and close sockets or files here.



33
34
35
36
# File 'lib/fluent/plugin/out_rollbar.rb', line 33

def shutdown
    super
    $log.info 'Rollbar Output shutting down'
end

#startObject

This method is called when starting. Open sockets or files here.



27
28
29
# File 'lib/fluent/plugin/out_rollbar.rb', line 27

def start
    super
end

#write(chunk) ⇒ Object

Optionally, you can use chunk.msgpack_each to deserialize objects.



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

def write(chunk)
    chunk.msgpack_each do |(_tag, _time, record)|
        record = record['log'] if record.key? 'log'

        EventMachine.run do
            record['access_token'] = @access_token
            headers = { 'X-Rollbar-Access-Token' => @access_token }
            req = EventMachine::HttpRequest.new(@endpoint).post(body: record.to_json, head: headers)

            req.callback do
                if req.response_header.status != 200
                    $log.warn "rollbar: Got unexpected status code from Rollbar.io api: #{req.response_header.status}"
                    $log.warn "rollbar: Response: #{req.response}"
                end

                EventMachine.stop
            end

            req.errback do
                $log.warn "rollbar: Call to API failed, status code: #{req.response_header.status}"
                $log.warn "rollbar: Error's response: #{req.response}"

                EventMachine.stop
            end
        end
    end
rescue Exception => e
    $log.warn "rollbar: #{e}"
end