-
Notifications
You must be signed in to change notification settings - Fork 34
Add ErrorTracker.Filter behavior for modifying error context before saving
#94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| defmodule ErrorTracker.Filter do | ||
| @moduledoc """ | ||
| Behaviour for sanitizing & modifying the error context before it's saved. | ||
|
|
||
| defmodule MyApp.ErrorFilter do | ||
| @behaviour ErrorTracker.Filter | ||
|
|
||
| @impl true | ||
| def sanitize(context) do | ||
| context # Modify the context object (add or remove fields as much as you need.) | ||
| end | ||
| end | ||
|
|
||
| Once implemented, include it in the ErrorTracker configuration: | ||
|
|
||
| config :error_tracker, filter: MyApp.Filter | ||
|
|
||
| With this configuration in place, the ErrorTracker will call `MyApp.Filter.sanitize/1` to get a context before | ||
| saving error occurrence. | ||
|
|
||
| > #### A note on performance {: .warning} | ||
| > | ||
| > Keep in mind that the `sanitize/1` will be called in the context of the ErrorTracker itself. | ||
| > Slow code will have a significant impact in the ErrorTracker performance. Buggy code can bring | ||
| > the ErrorTracker process down. | ||
| """ | ||
|
|
||
| @doc """ | ||
| This function will be given an error context to inspect/modify before it's saved. | ||
| """ | ||
| @callback sanitize(context :: map()) :: map() | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| defmodule ErrorTracker.FilterTest do | ||
| use ErrorTracker.Test.Case | ||
|
|
||
| setup context do | ||
| if filter = context[:filter] do | ||
| previous_setting = Application.get_env(:error_tracker, :filter) | ||
| Application.put_env(:error_tracker, :filter, filter) | ||
| # Ensure that the application env is restored after each test | ||
| on_exit(fn -> Application.put_env(:error_tracker, :filter, previous_setting) end) | ||
| end | ||
|
|
||
| [] | ||
| end | ||
|
|
||
| @sensitive_ctx %{ | ||
| "request" => %{ | ||
| "headers" => %{ | ||
| "accept" => "application/json, text/plain, */*", | ||
| "authorization" => "Bearer 12341234" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test "without an filter, context objects are saved as they are." do | ||
| assert %ErrorTracker.Occurrence{context: ctx} = | ||
| report_error(fn -> raise "BOOM" end, @sensitive_ctx) | ||
|
|
||
| assert ctx == @sensitive_ctx | ||
| end | ||
|
|
||
| @tag filter: ErrorTracker.FilterTest.AuthHeaderHider | ||
| test "user defined filter should be used to sanitize the context before it's saved." do | ||
| assert %ErrorTracker.Occurrence{context: ctx} = | ||
| report_error(fn -> raise "BOOM" end, @sensitive_ctx) | ||
|
|
||
| assert ctx != @sensitive_ctx | ||
|
|
||
| cleaned_header_value = | ||
| ctx |> Map.get("request") |> Map.get("headers") |> Map.get("authorization") | ||
|
|
||
| assert cleaned_header_value == "REMOVED" | ||
| end | ||
| end | ||
|
|
||
| defmodule ErrorTracker.FilterTest.AuthHeaderHider do | ||
| @behaviour ErrorTracker.Filter | ||
|
|
||
| def sanitize(context) do | ||
| context | ||
| |> Enum.map(fn | ||
| {"authorization", _} -> | ||
| {"authorization", "REMOVED"} | ||
|
|
||
| o -> | ||
| o | ||
| end) | ||
| |> Enum.map(fn | ||
| {key, val} when is_map(val) -> {key, sanitize(val)} | ||
| o -> o | ||
| end) | ||
| |> Map.new() | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it may make sense to filter the context data only after we are sure that we want to track the error.
This would reduce the number of operations that the ErrorTracker does for ignored errors, which I think is important given that this calls user-defined code that we don't have any control over.
This would mean that the ignorer, if defined, has access to the full unfiltered context but I think that would be OK as long as we document it.
What do you think @odarriba?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, as that function may do heavy calculations (or not, who knows)