Class: LogStash::Two::Outputs::Rollbar

Inherits:
Outputs::Base
  • Object
show all
Defined in:
lib/logstash/two/outputs/rollbar.rb

Overview

The Rollbar output will send events to the Rollbar event monitoring service. The only required field is a Rollbar project access token with post_server_item permissions. If you’re already using Rollbar to report errors directly from your applications, you can use the same token.

Instance Method Summary collapse

Instance Method Details

#hash_recursiveObject



36
37
38
39
40
# File 'lib/logstash/two/outputs/rollbar.rb', line 36

def hash_recursive
  Hash.new do |hash, key|
    hash[key] = hash_recursive
  end
end

#receive(event) ⇒ Object



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/logstash/two/outputs/rollbar.rb', line 55

def receive(event)
  return unless output?(event)

  rb_item = hash_recursive

  # We'll want to remove fields from data without removing them from the original event
  data = JSON.parse(event.to_json)

  #
  # If logstash has created 'rollbar' fields, we'll use those to populate the item...
  #
  if data['rollbar']

    merge_keys = %w{access_token client context environment fingerprint format framework
                    language level person platform request server title uuid }
    merge_keys.each do |key|
      data['rollbar'][key] && rb_item['data'][key] = data['rollbar'][key]
    end
    data.delete('rollbar')
  end

  # ...then put whatever's left in 'custom'...
  rb_item['data']['custom'] = data

  # ...and finally override the fields that have a specific meaning
  rb_item['data']['timestamp'] = event.timestamp.to_i
  rb_item['data']['level'] = @level unless rb_item['data'].has_key?('level')
  rb_item['data']['environment'] = @environment unless rb_item['data'].has_key?('environment')

  rb_item['data']['notifier']['name'] = 'logstash'
  rb_item['data']['notifier']['version'] = Gem.loaded_specs["logstash-two-output-rollbar"].version

  # Construct the message body using either:
  #
  # - The default format string defined above "%{message}"
  # - The format string specified in the rollbar plugin config section
  # - The format string specified in the [rollbar][format] event field
  #
  format = rb_item['data'].has_key?('format') ? rb_item['data']['format'] : @format
  rb_item['data']['body']['message']['body'] = event.sprintf(format)

  # Treat the [rollbar][access_token] field as a special case, since we don't need to
  # include it more than once in the Rollbar item
  #
  if rb_item['data'].has_key?('access_token')
    rb_item['access_token'] = rb_item['data']['access_token']
    rb_item['data'].delete('access_token')
  else
    rb_item['access_token'] = @access_token.value
  end


  @logger.debug("Rollbar Item", :rb_item => rb_item)

  begin
    request = Net::HTTP::Post.new(@rb_uri.path)
    request.body = JSON.dump(rb_item)
    @logger.debug("Rollbar Request", :request => request.body)
    response = @client.request(request)
    @logger.debug("Rollbar Response", :response => response.body)

  rescue Exception => e
    @logger.warn("Rollbar Exception", :rb_error => e.backtrace)
  end
end

#registerObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/logstash/two/outputs/rollbar.rb', line 43

def register
  require 'net/https'
  require 'uri'
  @rb_uri = URI.parse(@endpoint)
  @client = Net::HTTP.new(@rb_uri.host, @rb_uri.port)
  if @rb_uri.scheme == "https"
    @client.use_ssl = true
    @client.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
end