raygun4ruby 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ module Raygun
2
+ class InstallGenerator < Rails::Generators::Base
3
+
4
+ argument :api_key
5
+
6
+ desc "This generator creates a configuration file for the Raygun ruby adapter inside config/initializers"
7
+ def create_configuration_file
8
+ initializer "raygun.rb" do
9
+ <<-EOS
10
+ Raygun.setup do |config|
11
+ config.api_key = "#{api_key}"
12
+ end
13
+ EOS
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,75 @@
1
+ require "bundler"
2
+ Bundler.setup(:default)
3
+
4
+ require "httparty"
5
+ require "logger"
6
+ require "json"
7
+ require "socket"
8
+ require "rack"
9
+ require "active_support/core_ext"
10
+
11
+ require "raygun/version"
12
+ require "raygun/configuration"
13
+ require "raygun/client"
14
+ require "raygun/rack_exception_interceptor"
15
+ require "raygun/testable"
16
+ require "raygun/railtie" if defined?(Rails)
17
+
18
+ module Raygun
19
+
20
+ # used to identify ourselves to Raygun
21
+ CLIENT_URL = "https://github.com/MindscapeHQ/raygun4ruby"
22
+ CLIENT_NAME = "Raygun4Ruby Gem"
23
+
24
+ class << self
25
+
26
+ include Testable
27
+
28
+ # Configuration Object (instance of Raygun::Configuration)
29
+ attr_writer :configuration
30
+
31
+ def setup
32
+ yield(configuration)
33
+ end
34
+
35
+ def configuration
36
+ @configuration ||= Configuration.new
37
+ end
38
+
39
+ def track_exception(exception_instance, env = {})
40
+ if should_report?(exception_instance)
41
+ log("[Raygun] Tracking Exception...")
42
+ Client.new.track_exception(exception_instance, env)
43
+ end
44
+ rescue Exception => e
45
+ if configuration.failsafe_logger
46
+ failsafe_log("Problem reporting exception to Raygun: #{e.class}: #{e.message}\n\n#{e.backrace.join("\n")}")
47
+ else
48
+ raise e
49
+ end
50
+ end
51
+
52
+ def track_exceptions
53
+ yield
54
+ rescue => e
55
+ track_exception(e)
56
+ end
57
+
58
+ def log(message)
59
+ configuration.logger.info(message) if configuration.logger
60
+ end
61
+
62
+ def failsafe_log(message)
63
+ configuration.failsafe_logger.info(message)
64
+ end
65
+
66
+ private
67
+
68
+ def should_report?(exception)
69
+ return false if configuration.silence_reporting
70
+ return false if configuration.ignore.include?(exception.class.to_s)
71
+ true
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,104 @@
1
+ module Raygun
2
+ # client for the Raygun REST APIv1
3
+ # as per http://raygun.io/raygun-providers/rest-json-api?v=1
4
+ class Client
5
+ include HTTParty
6
+
7
+ base_uri "https://api.raygun.io/"
8
+
9
+ def initialize
10
+ @headers = {
11
+ "X-ApiKey" => Raygun.configuration.api_key
12
+ }
13
+ end
14
+
15
+ def track_exception(exception_instance, env = {})
16
+ create_entry(build_payload_hash(exception_instance, env))
17
+ end
18
+
19
+ private
20
+
21
+ def client_details
22
+ {
23
+ name: Raygun::CLIENT_NAME,
24
+ version: Raygun::VERSION,
25
+ clientUrl: Raygun::CLIENT_URL
26
+ }
27
+ end
28
+
29
+ def error_details(exception)
30
+ {
31
+ className: exception.class.to_s,
32
+ message: exception.message,
33
+ stackTrace: exception.backtrace.map { |line| stack_trace_for(line) }
34
+ }
35
+ end
36
+
37
+ def stack_trace_for(line)
38
+ # see http://www.ruby-doc.org/core-2.0/Exception.html#method-i-backtrace
39
+ file_name, line_number, method = line.split(":")
40
+ {
41
+ lineNumber: line_number,
42
+ fileName: file_name,
43
+ methodName: method.gsub(/^in `(.*?)'$/, "\\1")
44
+ }
45
+ end
46
+
47
+ def hostname
48
+ Socket.gethostname
49
+ end
50
+
51
+ def version
52
+ Raygun.configuration.version
53
+ end
54
+
55
+ def request_information(env)
56
+ return {} if env.blank?
57
+
58
+ {
59
+ hostName: env["SERVER_NAME"],
60
+ url: env["PATH_INFO"],
61
+ httpMethod: env["REQUEST_METHOD"],
62
+ ipAddress: env["REMOTE_ADDR"],
63
+ queryString: Rack::Utils.parse_nested_query(env["QUERY_STRING"]),
64
+ form: form_data(env),
65
+ headers: headers(env),
66
+ rawData: []
67
+ }
68
+ end
69
+
70
+ def headers(rack_env)
71
+ rack_env.select do |k, v|
72
+ k.starts_with?("HTTP_")
73
+ end
74
+ end
75
+
76
+ def form_data(rack_env)
77
+ request = Rack::Request.new(rack_env)
78
+
79
+ if request.form_data?
80
+ request.body
81
+ end
82
+ end
83
+
84
+ # see http://raygun.io/raygun-providers/rest-json-api?v=1
85
+ def build_payload_hash(exception_instance, env)
86
+ {
87
+ occurredOn: Time.now.utc.iso8601,
88
+ details: {
89
+ machineName: hostname,
90
+ version: version,
91
+ client: client_details,
92
+ error: error_details(exception_instance),
93
+ userCustomData: Raygun.configuration.custom_data,
94
+ request: request_information(env)
95
+ }
96
+ }
97
+ end
98
+
99
+ def create_entry(payload_hash)
100
+ self.class.post("/entries", headers: @headers, body: JSON.generate(payload_hash))
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,45 @@
1
+ module Raygun
2
+ class Configuration
3
+
4
+ # Your Raygun API Key - this can be found on your dashboard at Raygun.io
5
+ attr_accessor :api_key
6
+
7
+ # Array of exception classes to ignore
8
+ attr_accessor :ignore
9
+
10
+ # Version to use
11
+ attr_accessor :version
12
+
13
+ # Custom Data to send with each exception
14
+ attr_accessor :custom_data
15
+
16
+ # Logger to use when if we find an exception :)
17
+ attr_accessor :logger
18
+
19
+ # Should we silence exception reporting (e.g in Development environments)
20
+ attr_accessor :silence_reporting
21
+
22
+ # Failsafe logger (for exceptions that happen when we're attempting to report exceptions)
23
+ attr_accessor :failsafe_logger
24
+
25
+ # Exception classes to ignore by default
26
+ IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
27
+ 'ActionController::RoutingError',
28
+ 'ActionController::InvalidAuthenticityToken',
29
+ 'CGI::Session::CookieStore::TamperedWithCookie',
30
+ 'ActionController::UnknownAction',
31
+ 'AbstractController::ActionNotFound',
32
+ 'Mongoid::Errors::DocumentNotFound']
33
+
34
+ def initialize
35
+ # set default attribute values
36
+ @ignore = IGNORE_DEFAULT
37
+ @custom_data = {}
38
+ end
39
+
40
+ def [](key)
41
+ send(key)
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ module Raygun
2
+ class RackExceptionInterceptor
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ response = @app.call(env)
10
+ rescue Exception => exception
11
+ Raygun.track_exception(exception, env)
12
+ raise exception
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ class Raygun::Railtie < Rails::Railtie
2
+
3
+ initializer "raygun.configure_rails_initialization" do |app|
4
+
5
+ # Thanks Airbrake: See https://github.com/rails/rails/pull/8624
6
+ middleware = if defined?(ActionDispatch::DebugExceptions)
7
+ # Rails >= 3.2.0
8
+ "ActionDispatch::DebugExceptions"
9
+ else
10
+ # Rails < 3.2.0
11
+ "ActionDispatch::ShowExceptions"
12
+ end
13
+
14
+ app.config.middleware.insert_after middleware, "Raygun::RackExceptionInterceptor"
15
+ end
16
+
17
+ config.to_prepare do
18
+ Raygun.configuration.logger ||= Rails.logger
19
+ Raygun.configuration.silence_reporting ||= Rails.env.development?
20
+ end
21
+
22
+ rake_tasks do
23
+ load "tasks/raygun.tasks"
24
+ end
25
+
26
+ end
@@ -0,0 +1,19 @@
1
+ module Raygun
2
+
3
+ class ItWorksException < StandardError; end
4
+
5
+ module Testable
6
+
7
+ def track_test_exception
8
+ Raygun.configuration.silence_reporting = false
9
+ raise ItWorksException.new("Woohoo! Your Raygun<->Ruby connection is set up correctly")
10
+ rescue ItWorksException => e
11
+ if Raygun.track_exception(e).success?
12
+ puts "Success! Now go check your Raygun.io Dashboard"
13
+ else
14
+ puts "Oh-oh, something went wrong - double check your API key"
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Raygun
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1 @@
1
+ require "raygun"
@@ -0,0 +1,8 @@
1
+ namespace :raygun do
2
+
3
+ desc "Test Raygun integration"
4
+ task :test => :environment do
5
+ Raygun.track_test_exception
6
+ end
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raygun4ruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
10
+ platform: ruby
11
+ authors:
12
+ - Mindscape
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-07-31 00:00:00 +12:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 11
30
+ version: "0.11"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ - !ruby/object:Gem::Dependency
46
+ name: activesupport
47
+ prerelease: false
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ type: :runtime
56
+ version_requirements: *id003
57
+ - !ruby/object:Gem::Dependency
58
+ name: rack
59
+ prerelease: false
60
+ requirement: &id004 !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ type: :runtime
68
+ version_requirements: *id004
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ prerelease: false
72
+ requirement: &id005 !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 1
78
+ - 1
79
+ version: "1.1"
80
+ type: :development
81
+ version_requirements: *id005
82
+ - !ruby/object:Gem::Dependency
83
+ name: rake
84
+ prerelease: false
85
+ requirement: &id006 !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ type: :development
93
+ version_requirements: *id006
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakeweb
96
+ prerelease: false
97
+ requirement: &id007 !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 1
103
+ - 3
104
+ version: "1.3"
105
+ type: :development
106
+ version_requirements: *id007
107
+ - !ruby/object:Gem::Dependency
108
+ name: timecop
109
+ prerelease: false
110
+ requirement: &id008 !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ type: :development
118
+ version_requirements: *id008
119
+ - !ruby/object:Gem::Dependency
120
+ name: minitest
121
+ prerelease: false
122
+ requirement: &id009 !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 4
128
+ - 2
129
+ version: "4.2"
130
+ type: :development
131
+ version_requirements: *id009
132
+ description: Ruby Adapter for Raygun.io
133
+ email:
134
+ - hello@raygun.io
135
+ executables: []
136
+
137
+ extensions: []
138
+
139
+ extra_rdoc_files: []
140
+
141
+ files:
142
+ - lib/raygun.rb
143
+ - lib/raygun/client.rb
144
+ - lib/raygun/configuration.rb
145
+ - lib/raygun/rack_exception_interceptor.rb
146
+ - lib/raygun/railtie.rb
147
+ - lib/raygun/version.rb
148
+ - lib/raygun4ruby.rb
149
+ - lib/generators/raygun/install_generator.rb
150
+ - lib/raygun/testable.rb
151
+ - lib/tasks/raygun.tasks
152
+ has_rdoc: true
153
+ homepage: http://raygun.io
154
+ licenses:
155
+ - MIT
156
+ post_install_message:
157
+ rdoc_options: []
158
+
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ segments:
166
+ - 0
167
+ version: "0"
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ segments:
173
+ - 0
174
+ version: "0"
175
+ requirements: []
176
+
177
+ rubyforge_project:
178
+ rubygems_version: 1.3.6
179
+ signing_key:
180
+ specification_version: 3
181
+ summary: This gem provides support for Ruby and Ruby on Rails for the Raygun.io error reporter
182
+ test_files: []
183
+