Skip to content

Commit

Permalink
Merge pull request #108 from MindscapeHQ/global-custom-data-hook
Browse files Browse the repository at this point in the history
Global custom data hook
  • Loading branch information
UberMouse authored Mar 9, 2017
2 parents c31a73f + e677bd0 commit dd81789
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Features:
- Improve affected user handling to let you specify all Raygun parameters, identifier, email, first name, full name and uuid. See [README.md](https://github.com/MindscapeHQ/raygun4ruby#affected-user-tracking) for details
- Pass a user object as the third parameter to `Raygun.track_exception` to have affected user tracking for manually tracked exceptions, see the above link for more information on configuring this
- If the exception instance responds to `:raygun_custom_data` that method will be called and the return value merged into the `custom_data` hash sent to Raygun. For convenience a `Raygun::Error` class is provided that takes this custom data as a second argument
- Allowed `Configuration.custom_data` to be set to a proc to allow a global custom data hook for all exceptions. It is passed as arguments the exception and the environment hash

## 1.2.1 (09/03/2017)

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ end

```

Custom data can also be specified globally either by setting `config.custom_data` to a hash

```ruby
Raygun.setup do |config|
config.api_key = "YOUR_RAYGUN_API_KEY"
config.custom_data = {custom_data: 'goes here'}
end
```

or to a proc, which gets passed the exception and environment hash

```ruby
Raygun.setup do |config|
config.api_key = "YOUR_RAYGUN_API_KEY"
config.custom_data do |e, env|
{message: e.message, server: env["SERVER_NAME"]}
end
end
```

### Ignoring Some Errors

You can ignore certain types of Exception using the `ignore` option in the setup block, like so:
Expand Down
9 changes: 5 additions & 4 deletions lib/raygun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
require "rack"
require "ostruct"

begin
require "pry"
rescue LoadError
end

require "raygun/version"
require "raygun/configuration"
require "raygun/client"
Expand All @@ -14,10 +19,6 @@
require "raygun/affected_user"
require "raygun/services/apply_whitelist_filter_to_payload"
require "raygun/railtie" if defined?(Rails)
begin
require "pry"
rescue LoadError
end

module Raygun

Expand Down
15 changes: 13 additions & 2 deletions lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ def filter_custom_data(env)
# see http://raygun.io/raygun-providers/rest-json-api?v=1
def build_payload_hash(exception_instance, env = {}, user = nil)
custom_data = filter_custom_data(env) || {}
exception_custom_data = exception_instance.respond_to?(:raygun_custom_data) ? exception_instance.raygun_custom_data : {}
exception_custom_data = if exception_instance.respond_to?(:raygun_custom_data)
exception_instance.raygun_custom_data
else
{}
end

tags = env.delete(:tags) || []

Expand All @@ -155,12 +159,19 @@ def build_payload_hash(exception_instance, env = {}, user = nil)

grouping_key = env.delete(:grouping_key)

configuration_custom_data = Raygun.configuration.custom_data
configured_custom_data = if configuration_custom_data.is_a?(Proc)
configuration_custom_data.call(exception_instance, env)
else
configuration_custom_data
end

error_details = {
machineName: hostname,
version: version,
client: client_details,
error: error_details(exception_instance),
userCustomData: Raygun.configuration.custom_data.merge(custom_data).merge(exception_custom_data),
userCustomData: exception_custom_data.merge(custom_data).merge(configured_custom_data),
tags: Raygun.configuration.tags.concat(tags).compact.uniq,
request: request_information(env)
}
Expand Down
27 changes: 14 additions & 13 deletions lib/raygun/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ def self.config_option(name)
end
end

def self.proc_config_option(name)
define_method(name) do |&block|
set_value(name, block) unless block == nil
read_value(name)
end

define_method("#{name}=") do |value|
set_value(name, value)
end
end

# Your Raygun API Key - this can be found on your dashboard at Raygun.io
config_option :api_key

Expand All @@ -21,7 +32,7 @@ def self.config_option(name)
config_option :version

# Custom Data to send with each exception
config_option :custom_data
proc_config_option :custom_data

# Tags to send with each exception
config_option :tags
Expand All @@ -42,13 +53,13 @@ def self.config_option(name)
config_option :affected_user_mapping

# Which parameter keys should we filter out by default?
config_option :filter_parameters
proc_config_option :filter_parameters

# Should we switch to a white listing mode for keys instead of the default blacklist?
config_option :filter_payload_with_whitelist

# If :filter_payload_with_whitelist is true, which keys should we whitelist?
config_option :whitelist_payload_shape
proc_config_option :whitelist_payload_shape

# Hash of proxy settings - :address, :port (defaults to 80), :username and :password (both default to nil)
config_option :proxy_settings
Expand Down Expand Up @@ -125,16 +136,6 @@ def affected_user_identifier_methods
read_value(:affected_user_method_mapping).Identifier
end

def filter_parameters(&filter_proc)
set_value(:filter_parameters, filter_proc) if block_given?
read_value(:filter_parameters)
end

def whitelist_payload_shape(&filter_proc)
set_value(:whitelist_payload_shape, filter_proc) if block_given?
read_value(:whitelist_payload_shape)
end

private

def read_value(name)
Expand Down
19 changes: 19 additions & 0 deletions test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,25 @@ def test_error_raygun_custom_data
assert_equal expected_form_hash, @client.send(:build_payload_hash, e, test_env)[:details][:userCustomData]
end

def test_custom_data_configuration_with_hash
custom_data = {foo: '123'}
Raygun.configuration.custom_data = custom_data

assert_equal custom_data, @client.send(:build_payload_hash, test_exception, sample_env_hash)[:details][:userCustomData]
end

def test_custom_data_configuration_with_proc
Raygun.configuration.custom_data do |exception, env|
{exception_message: exception.message, server_name: env["SERVER_NAME"]}
end
expected = {
exception_message: "A test message",
server_name: "localhost"
}

assert_equal expected, @client.send(:build_payload_hash, test_exception, sample_env_hash)[:details][:userCustomData]
end

def test_filtering_parameters
post_body_env_hash = sample_env_hash.merge(
"rack.input"=>StringIO.new("a=b&c=4945438&password=swordfish")
Expand Down
21 changes: 21 additions & 0 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,25 @@ def test_setting_whitelist_payload_keys_to_proc
Raygun.configuration.whitelist_payload_shape = nil
end

def test_setting_custom_data_to_proc
Raygun.setup do |config|
config.custom_data do |exception, env|
# No-op
end
end

assert Raygun.configuration.custom_data.is_a?(Proc)
ensure
Raygun.configuration.custom_data = nil
end

def test_setting_custom_data_to_hash
Raygun.setup do |config|
config.custom_data = {}
end

assert Raygun.configuration.custom_data.is_a?(Hash)
ensure
Raygun.configuration.custom_data = nil
end
end

0 comments on commit dd81789

Please sign in to comment.