-
-
Notifications
You must be signed in to change notification settings - Fork 205
post event hook #175
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
post event hook #175
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ defmodule Sentry.Client do | |
it is sent. Accepts an anonymous function or a {module, function} tuple, and | ||
the event will be passed as the only argument. | ||
|
||
* `:after_send_event` - callback that is called after an event is successfully sent. | ||
Accepts an anonymous function or a {module, function} tuple, and the event will be passed as the only argument. | ||
|
||
Example configuration of putting Logger metadata in the extra context: | ||
|
||
config :sentry, | ||
|
@@ -49,32 +52,35 @@ defmodule Sentry.Client do | |
event = maybe_call_before_send_event(event) | ||
case Poison.encode(event) do | ||
{:ok, body} -> | ||
do_send_event(body, result) | ||
do_send_event(event, body, result) | ||
{:error, error} -> | ||
log_api_error("Unable to encode Sentry error - #{inspect(error)}") | ||
:error | ||
end | ||
end | ||
|
||
defp do_send_event(body, :async) do | ||
defp do_send_event(event, body, :async) do | ||
{endpoint, public_key, secret_key} = get_dsn!() | ||
auth_headers = authorization_headers(public_key, secret_key) | ||
{:ok, Task.Supervisor.async_nolink(Sentry.TaskSupervisor, fn -> | ||
try_request(:post, endpoint, auth_headers, body) | ||
|> maybe_call_after_send_event(event) | ||
end)} | ||
end | ||
|
||
defp do_send_event(body, :sync) do | ||
defp do_send_event(event, body, :sync) do | ||
{endpoint, public_key, secret_key} = get_dsn!() | ||
auth_headers = authorization_headers(public_key, secret_key) | ||
try_request(:post, endpoint, auth_headers, body) | ||
|> maybe_call_after_send_event(event) | ||
end | ||
|
||
defp do_send_event(body, :none) do | ||
defp do_send_event(event, body, :none) do | ||
{endpoint, public_key, secret_key} = get_dsn!() | ||
auth_headers = authorization_headers(public_key, secret_key) | ||
Task.Supervisor.start_child(Sentry.TaskSupervisor, fn -> | ||
try_request(:post, endpoint, auth_headers, body) | ||
|> maybe_call_after_send_event(event) | ||
end) | ||
|
||
{:ok, ""} | ||
|
@@ -157,6 +163,22 @@ defmodule Sentry.Client do | |
{endpoint, public_key, secret_key} | ||
end | ||
|
||
def maybe_call_after_send_event({:ok, _} = result, event) do | ||
case Application.get_env(:sentry, :after_send_event) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can probably break this case into a function and use it for both scenarios instead of copying it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they each call the configured function differently because of different arities, so this makes less sense now right? |
||
function when is_function(function, 1) -> function.(event) | ||
{module, function} -> apply(module, function, [event]) | ||
nil -> nil | ||
_ -> | ||
raise ArgumentError, message: ":after_send_event must be an anonymous function or a {Module, Function} tuple" | ||
end | ||
|
||
result | ||
end | ||
|
||
def maybe_call_after_send_event(result, _event) do | ||
result | ||
end | ||
|
||
def maybe_call_before_send_event(event) do | ||
case Application.get_env(:sentry, :before_send_event) do | ||
function when is_function(function, 1) -> | ||
|
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.
@jeregrine Right now we only try to call the hook if the sending event was successful, but I was thinking since we're passing the result, we could call
function.(result, event)
and let the user decide so we can skip the logic on our side?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.
Sounds good to me