-
-
Notifications
You must be signed in to change notification settings - Fork 206
Add Telemetry integration #887
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
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,66 @@ | ||
defmodule Sentry.Integrations.Telemetry do | ||
@moduledoc """ | ||
Sentry integration for Telemetry. | ||
|
||
*Available since v10.10.0*. | ||
""" | ||
|
||
@moduledoc since: "10.10.0" | ||
|
||
@failure_event [:telemetry, :handler, :failure] | ||
|
||
@doc false | ||
@spec attach() :: :ok | ||
def attach do | ||
_ = | ||
:telemetry.attach( | ||
"#{inspect(__MODULE__)}-telemetry-failures", | ||
@failure_event, | ||
&handle_event/4, | ||
:no_config | ||
) | ||
|
||
:ok | ||
end | ||
|
||
@doc false | ||
@spec handle_event( | ||
:telemetry.event_name(), | ||
:telemetry.event_measurements(), | ||
:telemetry.event_metadata(), | ||
:telemetry.handler_config() | ||
) :: :ok | ||
def handle_event(@failure_event, _measurements, %{} = metadata, :no_config) do | ||
stacktrace = metadata[:stacktrace] || [] | ||
handler_id = stringified_handler_id(metadata[:handler_id]) | ||
|
||
options = [ | ||
stacktrace: stacktrace, | ||
tags: %{ | ||
telemetry_handler_id: handler_id, | ||
event_name: inspect(metadata[:event_name]) | ||
} | ||
] | ||
|
||
_ = | ||
case {metadata[:kind], metadata[:reason]} do | ||
{:error, reason} -> | ||
exception = Exception.normalize(:error, reason, stacktrace) | ||
Sentry.capture_exception(exception, options) | ||
|
||
{kind, reason} -> | ||
options = | ||
Keyword.merge(options, | ||
extra: %{kind: inspect(kind), reason: inspect(reason)}, | ||
interpolation_parameters: [handler_id] | ||
) | ||
|
||
Sentry.capture_message("Telemetry handler %s failed", options) | ||
end | ||
|
||
:ok | ||
end | ||
|
||
defp stringified_handler_id(handler_id) when is_binary(handler_id), do: handler_id | ||
defp stringified_handler_id(handler_id), do: inspect(handler_id) | ||
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
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,5 @@ | ||
# Telemetry Integration | ||
|
||
The Sentry SDK supports integrating with [Telemetry](https://github.com/beam-telemetry/telemetry). | ||
|
||
For documentation and setup instructions, see the [Sentry website](https://docs.sentry.io/platforms/elixir/integrations/telemetry/). |
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,75 @@ | ||
defmodule Sentry.Integrations.TelemetryTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Sentry.Integrations.Telemetry | ||
|
||
describe "handle_event/4" do | ||
test "reports errors" do | ||
Sentry.Test.start_collecting() | ||
|
||
handle_failure_event(:error, %RuntimeError{message: "oops"}, []) | ||
|
||
assert [event] = Sentry.Test.pop_sentry_reports() | ||
|
||
assert event.tags == %{ | ||
telemetry_handler_id: "my_handler", | ||
event_name: "[:my_app, :some_event]" | ||
} | ||
|
||
assert event.original_exception == %RuntimeError{message: "oops"} | ||
end | ||
|
||
test "reports Erlang errors (normalized)" do | ||
Sentry.Test.start_collecting() | ||
|
||
handle_failure_event(:error, {:badmap, :foo}, []) | ||
|
||
assert [event] = Sentry.Test.pop_sentry_reports() | ||
|
||
assert event.tags == %{ | ||
telemetry_handler_id: "my_handler", | ||
event_name: "[:my_app, :some_event]" | ||
} | ||
|
||
assert event.original_exception == %BadMapError{term: :foo} | ||
end | ||
|
||
for kind <- [:throw, :exit] do | ||
test "reports #{kind}s" do | ||
Sentry.Test.start_collecting() | ||
|
||
handle_failure_event(unquote(kind), :foo, []) | ||
|
||
assert [event] = Sentry.Test.pop_sentry_reports() | ||
|
||
assert event.message.message == "Telemetry handler %s failed" | ||
assert event.message.formatted == "Telemetry handler my_handler failed" | ||
|
||
assert event.tags == %{ | ||
telemetry_handler_id: "my_handler", | ||
event_name: "[:my_app, :some_event]" | ||
} | ||
|
||
assert event.extra == %{kind: inspect(unquote(kind)), reason: ":foo"} | ||
|
||
assert event.original_exception == nil | ||
end | ||
end | ||
end | ||
|
||
defp handle_failure_event(kind, reason, stacktrace) do | ||
Telemetry.handle_event( | ||
[:telemetry, :handler, :failure], | ||
%{system_time: System.system_time(:native), monotonic_time: System.monotonic_time(:native)}, | ||
%{ | ||
handler_id: "my_handler", | ||
handler_config: %{my_key: "my value"}, | ||
event_name: [:my_app, :some_event], | ||
kind: kind, | ||
reason: reason, | ||
stacktrace: stacktrace | ||
}, | ||
:no_config | ||
) | ||
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.
Shouldn't this be a warning or even an error?
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.
This is already an error that gets reported to Sentry. Not sure what you mean here?
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.
@whatyouhide oh I just assumed that
capture_message
doesn't send an error by default, and sends an info-level message instead. IIRC Sentry displays and manages these messages differently, especially when it comes to notifications.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 you're thinking about logging. These get displayed as errors.
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.
@whatyouhide messages in Sentry do have levels too, and our SDK supports
level
option incapture_message
. I just checked and luckily the default level iserror
.One thing I noticed is that the UI displays "(no error message)" which may be a little confusing for the users.