Skip to content

Commit

Permalink
Send array and hash context as custom_data
Browse files Browse the repository at this point in the history
When reporting via the Rails error reporter, there was no way to send
custom data, with this change it can be included in
context[:appsignal][:custom_data] and it will be sent to Appsignal.

Fixes #984
  • Loading branch information
beathyate committed Jul 24, 2023
1 parent e238e08 commit d048c77
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .changesets/send-custom-data-via-rails-error-reporter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
bump: "patch"
type: "add"
---

Allow passing custom data using the `appsignal` context via the Rails error reporter:

```ruby
custom_data = { :hash => { :one => 1, :two => 2 }, :array => [1, 2] }
Rails.error.handle(:context => { :appsignal => { :custom_data => custom_data } }) do
raise "Test"
end
```
9 changes: 7 additions & 2 deletions lib/appsignal/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def report(error, handled:, severity:, context: {}, source: nil)
return unless handled

Appsignal.send_error(error) do |transaction|
namespace, action_name, tags = context_for(context.dup)
namespace, action_name, tags, custom_data = context_for(context.dup)
transaction.set_namespace(namespace) if namespace
transaction.set_action(action_name) if action_name
transaction.set_sample_data("custom_data", custom_data) if custom_data

tags[:severity] = severity
tags[:source] = source.to_s if source
Expand All @@ -69,6 +70,7 @@ def report(error, handled:, severity:, context: {}, source: nil)

def context_for(context)
tags = {}
custom_data = nil

appsignal_context = context.delete(:appsignal)
# Fetch the namespace and action name based on the Rails execution
Expand Down Expand Up @@ -102,10 +104,13 @@ def context_for(context)

context_action_name = appsignal_context[:action]
action_name = context_action_name if context_action_name

context_custom_data = appsignal_context[:custom_data]
custom_data = context_custom_data if context_custom_data
end
tags.merge!(context)

[namespace, action_name, tags]
[namespace, action_name, tags, custom_data]
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/appsignal/integrations/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,33 @@ def subscribe(subscriber)
end
end

it "sends tags stored in :appsignal -> :custom_data as custom data" do
current_transaction = http_request_transaction

with_current_transaction current_transaction do
given_context = {
:appsignal => {
:custom_data => {
:array => [1, 2],
:hash => { :one => 1, :two => 2 }
}
}
}
Rails.error.handle(:context => given_context) { raise ExampleStandardError }

transaction = last_transaction
transaction_hash = transaction.to_h
expect(transaction_hash).to include(
"sample_data" => hash_including(
"custom_data" => {
"array" => [1, 2],
"hash" => { "one" => 1, "two" => 2 }
}
)
)
end
end

it "overwrites duplicated namespace and action with custom from context" do
current_transaction = http_request_transaction
current_transaction.set_namespace "custom"
Expand Down

0 comments on commit d048c77

Please sign in to comment.