Skip to content

Filter context only when tracking errors #100

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 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion guides/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ In certain cases, you may want to include some additional information when track

The `ErrorTracker.set_context/1` function stores the given context in the current process so any errors that occur in that process (for example, a Phoenix request or an Oban job) will include this given context along with the default integration context.

There are some requirements on the type of data that can be included in the context, so we recommend taking a look at `ErrorTracker.set_context/1` documentation

```elixir
ErrorTracker.set_context(%{user_id: conn.assigns.current_user.id})
```

There are some requirements on the type of data that can be included in the context, so we recommend taking a look at `set_context/1` documentation
You may also want to sanitize or filter out some information from the context before saving it. To do that you can take a look at the `ErrorTracker.Filter` behaviour.

## Manual error tracking

Expand Down
15 changes: 7 additions & 8 deletions lib/error_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ defmodule ErrorTracker do
{kind, reason} = normalize_exception(exception, stacktrace)
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
{:ok, error} = Error.new(kind, reason, stacktrace)
context = get_context() |> Map.merge(given_context) |> filter_context_data()
context = Map.merge(get_context(), given_context)

if enabled?() && !ignored?(error, context) do
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
sanitized_context = sanitize_context(context)
{_error, occurrence} = upsert_error!(error, stacktrace, sanitized_context, reason)
occurrence
else
:noop
Expand Down Expand Up @@ -209,14 +210,12 @@ defmodule ErrorTracker do
ignorer && ignorer.ignore?(error, context)
end

defp filter_context_data(context) do
defp sanitize_context(context) do
filter_mod = Application.get_env(:error_tracker, :filter)

if filter_mod do
filter_mod.sanitize(context)
else
context
end
if filter_mod,
do: filter_mod.sanitize(context),
else: context
end

defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do
Expand Down