-
-
Notifications
You must be signed in to change notification settings - Fork 205
Add support for client reports #801
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
15 commits
Select commit
Hold shift + click to select a range
9d6bdd4
add initial structs for client report
savhappy 91528a0
add genserver
savhappy 6bf15ac
refactor and add to envelope
savhappy 2381f1c
conditionally add client_reports
savhappy e67bd09
update test
savhappy a97df7f
fix dialyzer
savhappy a852e86
async true
savhappy 0240096
initial feddback #1
savhappy 5564099
add data category
savhappy c84b3b9
refactor data category and add in more recordings
savhappy 02975c0
feedback round 2 - update genserver and clean up
savhappy d639c0b
third round of feedback
savhappy fa43765
updated test + third round of feedback
savhappy 5c44239
docs added
savhappy 507745a
Update lib/sentry/client_report.ex
whatyouhide 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
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,147 @@ | ||
defmodule Sentry.ClientReport do | ||
@moduledoc """ | ||
A struct and GenServer implementation to represent and manage **client reports** for Sentry. | ||
|
||
Client reports are used to provide insights into which events are being dropped and for what reason. | ||
|
||
This module is responsible for recording, storing, and periodically sending these client | ||
reports to Sentry. You can choose to turn off these reports by configuring the | ||
option `send_client_reports?`. | ||
|
||
Refer to <https://develop.sentry.dev/sdk/client-reports/> for more details. | ||
|
||
*Available since v10.8.0*. | ||
""" | ||
|
||
@moduledoc since: "10.8.0" | ||
|
||
use GenServer | ||
alias Sentry.{Client, Config, Envelope} | ||
|
||
@client_report_reasons [ | ||
:ratelimit_backoff, | ||
:queue_overflow, | ||
:cache_overflow, | ||
:network_error, | ||
:sample_rate, | ||
:before_send, | ||
:event_processor, | ||
:insufficient_data, | ||
:backpressure, | ||
:send_error, | ||
:internal_sdk_error | ||
] | ||
|
||
@typedoc """ | ||
The possible reasons of the discarded event. | ||
""" | ||
@typedoc since: "10.8.0" | ||
@type reason() :: | ||
unquote(Enum.reduce(@client_report_reasons, "e(do: unquote(&1) | unquote(&2)))) | ||
|
||
@typedoc """ | ||
The struct for a **client report**. | ||
""" | ||
@typedoc since: "10.8.0" | ||
@type t() :: %__MODULE__{ | ||
timestamp: String.t() | number(), | ||
discarded_events: [%{reason: reason(), category: String.t(), quantity: pos_integer()}] | ||
} | ||
|
||
defstruct [:timestamp, discarded_events: %{}] | ||
|
||
@send_interval 30_000 | ||
|
||
@doc false | ||
@spec start_link([]) :: GenServer.on_start() | ||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, %{}, name: Keyword.get(opts, :name, __MODULE__)) | ||
end | ||
|
||
@doc false | ||
@spec record_discarded_events( | ||
reason(), | ||
[item] | ||
) :: :ok | ||
when item: | ||
Sentry.Attachment.t() | ||
| Sentry.CheckIn.t() | ||
| Sentry.ClientReport.t() | ||
| Sentry.Event.t() | ||
def record_discarded_events(reason, event_items, genserver \\ __MODULE__) | ||
when is_list(event_items) do | ||
if Enum.member?(@client_report_reasons, reason) do | ||
_ = | ||
event_items | ||
|> Enum.each( | ||
&GenServer.cast( | ||
genserver, | ||
{:record_discarded_events, reason, Envelope.get_data_category(&1)} | ||
) | ||
) | ||
end | ||
|
||
# We silently ignore events whose reasons aren't valid because we have to add it to the allowlist in Snuba | ||
# https://develop.sentry.dev/sdk/client-reports/ | ||
|
||
:ok | ||
end | ||
|
||
@doc false | ||
@impl true | ||
def init(state) do | ||
schedule_report() | ||
{:ok, state} | ||
end | ||
|
||
@doc false | ||
@impl true | ||
def handle_cast({:record_discarded_events, reason, category}, discarded_events) do | ||
{:noreply, Map.update(discarded_events, {reason, category}, 1, &(&1 + 1))} | ||
end | ||
|
||
@doc false | ||
@impl true | ||
def handle_info(:send_report, discarded_events) do | ||
if map_size(discarded_events) != 0 do | ||
discarded_events = | ||
discarded_events | ||
|> Enum.map(fn {{reason, category}, quantity} -> | ||
%{ | ||
reason: reason, | ||
category: category, | ||
quantity: quantity | ||
} | ||
end) | ||
|
||
client_report = | ||
%__MODULE__{ | ||
timestamp: timestamp(), | ||
discarded_events: discarded_events | ||
} | ||
|
||
_ = | ||
if Config.dsn() != nil && Config.send_client_reports?() do | ||
Client.send_client_report(client_report) | ||
end | ||
|
||
schedule_report() | ||
{:noreply, %{}} | ||
else | ||
# state is nil so nothing to send but keep looping | ||
schedule_report() | ||
{:noreply, %{}} | ||
end | ||
end | ||
|
||
defp schedule_report do | ||
Process.send_after(self(), :send_report, @send_interval) | ||
end | ||
|
||
defp timestamp do | ||
DateTime.utc_now() | ||
|> DateTime.truncate(:second) | ||
|> DateTime.to_iso8601() | ||
|> String.trim_trailing("Z") | ||
end | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.