From 711318d2f9c56102c3e17f381c14f3ce9a2838c6 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 10 Mar 2017 11:07:47 +1300 Subject: [PATCH 1/4] Introduced proc_config_option for config options that optionally take a proc, changed custom_data to use it --- lib/raygun.rb | 9 +++++---- lib/raygun/configuration.rb | 27 ++++++++++++++------------- test/unit/configuration_test.rb | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/lib/raygun.rb b/lib/raygun.rb index 658f28b..cc1840f 100644 --- a/lib/raygun.rb +++ b/lib/raygun.rb @@ -5,6 +5,11 @@ require "rack" require "ostruct" +begin + require "pry" +rescue LoadError +end + require "raygun/version" require "raygun/configuration" require "raygun/client" @@ -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 diff --git a/lib/raygun/configuration.rb b/lib/raygun/configuration.rb index c9a207a..242ec60 100644 --- a/lib/raygun/configuration.rb +++ b/lib/raygun/configuration.rb @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/test/unit/configuration_test.rb b/test/unit/configuration_test.rb index 2597673..8c26e3e 100644 --- a/test/unit/configuration_test.rb +++ b/test/unit/configuration_test.rb @@ -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 From 08e19e7d6d80a38a9b5d968d86f0472afae26244 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 10 Mar 2017 11:19:30 +1300 Subject: [PATCH 2/4] Update client to handle Configuration.custom_data being a proc --- lib/raygun/client.rb | 10 +++++++++- test/unit/client_test.rb | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/raygun/client.rb b/lib/raygun/client.rb index 866b80f..9f6e7fd 100644 --- a/lib/raygun/client.rb +++ b/lib/raygun/client.rb @@ -155,12 +155,20 @@ def build_payload_hash(exception_instance, env = {}, user = nil) grouping_key = env.delete(:grouping_key) + configuration_custom_data = Raygun.configuration.custom_data + user_custom_data = custom_data.merge(exception_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: user_custom_data.merge(configured_custom_data), tags: Raygun.configuration.tags.concat(tags).compact.uniq, request: request_information(env) } diff --git a/test/unit/client_test.rb b/test/unit/client_test.rb index 70a1e40..3774f42 100644 --- a/test/unit/client_test.rb +++ b/test/unit/client_test.rb @@ -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") From 1352ed195756e29f7fc87f94d7862d2ed0a8b380 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 10 Mar 2017 11:27:30 +1300 Subject: [PATCH 3/4] Update CHANGELOG and README --- CHANGELOG.md | 1 + README.md | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd6670..dbd66fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 19987ed..1840b36 100644 --- a/README.md +++ b/README.md @@ -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: From e677bd0731b9acb09e15ee25ee1bdcde5b46dcdf Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 10 Mar 2017 11:38:50 +1300 Subject: [PATCH 4/4] Small refactor --- lib/raygun/client.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/raygun/client.rb b/lib/raygun/client.rb index 9f6e7fd..32469c5 100644 --- a/lib/raygun/client.rb +++ b/lib/raygun/client.rb @@ -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) || [] @@ -156,7 +160,6 @@ def build_payload_hash(exception_instance, env = {}, user = nil) grouping_key = env.delete(:grouping_key) configuration_custom_data = Raygun.configuration.custom_data - user_custom_data = custom_data.merge(exception_custom_data) configured_custom_data = if configuration_custom_data.is_a?(Proc) configuration_custom_data.call(exception_instance, env) else @@ -168,7 +171,7 @@ def build_payload_hash(exception_instance, env = {}, user = nil) version: version, client: client_details, error: error_details(exception_instance), - userCustomData: user_custom_data.merge(configured_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) }