Skip to content

Commit 036f278

Browse files
committed
conditionally add client_reports
1 parent 07ba319 commit 036f278

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

lib/sentry.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ defmodule Sentry do
341341
cond do
342342
is_nil(event.message) and event.exception == [] ->
343343
LoggerUtils.log("Cannot report event without message or exception: #{inspect(event)}")
344+
Client.record_discarded_event(:event_processor, "no message or exception")
345+
# record discarded_event here
344346
:ignored
345347

346348
# If we're in test mode, let's send the event down the pipeline anyway.

lib/sentry/application.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ defmodule Sentry.Application do
1919
[]
2020
end
2121

22+
client_report_enabled =
23+
if Config.dsn() && Config.send_client_reports?() do
24+
Sentry.ClientReport
25+
end
26+
2227
integrations_config = Config.integrations()
2328

2429
children =
2530
[
2631
{Registry, keys: :unique, name: Sentry.Transport.SenderRegistry},
2732
Sentry.Sources,
2833
Sentry.Dedupe,
29-
# Sentry.ClientReports,
3034
{Sentry.Integrations.CheckInIDMappings,
3135
[
3236
max_expected_check_in_time:
@@ -36,6 +40,13 @@ defmodule Sentry.Application do
3640
maybe_http_client_spec ++
3741
[Sentry.Transport.SenderPool]
3842

43+
# only add client_reports if enabled
44+
children =
45+
case client_report_enabled do
46+
nil -> children
47+
_ -> children ++ client_report_enabled
48+
end
49+
3950
cache_loaded_applications()
4051

4152
with {:ok, pid} <-

lib/sentry/client.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ defmodule Sentry.Client do
1515
Interfaces,
1616
LoggerUtils,
1717
Transport,
18-
Options
18+
Options,
19+
ClientReport
1920
}
2021

2122
require Logger
@@ -91,6 +92,33 @@ defmodule Sentry.Client do
9192
end
9293
end
9394

95+
@spec send_client_report(ClientReport.t()) ::
96+
{:ok, client_report_id :: String.t()} | {:error, ClientError.t()}
97+
def send_client_report(%ClientReport{} = client_report) do
98+
# We don't pass options because this is internal. But should we use default client or users?
99+
client = Config.client()
100+
101+
# This is a "private" option, only really used in testing.
102+
request_retries =
103+
Application.get_env(:sentry, :request_retries, Transport.default_retries())
104+
105+
send_result =
106+
client_report
107+
|> Envelope.from_client_report()
108+
|> Transport.encode_and_post_envelope(client, request_retries)
109+
110+
send_result
111+
end
112+
113+
def record_discarded_event(reason, category) do
114+
# if Config.send_client_reports?() == true do
115+
# call to client report genserver
116+
ClientReport.add_discarded_event({reason, category}) |> IO.inspect()
117+
# end
118+
119+
:ok
120+
end
121+
94122
defp sample_event(sample_rate) do
95123
cond do
96124
sample_rate == 1 -> :ok

lib/sentry/client_report.ex

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Sentry.ClientReport do
1010
@typedoc since: "10.0.0"
1111

1212
use GenServer
13-
alias Sentry.Transport
13+
alias Sentry.Client
1414

1515
@typedoc """
1616
The possible reasons of the discarded event.
@@ -31,7 +31,8 @@ defmodule Sentry.ClientReport do
3131
"""
3232
@type t() :: %__MODULE__{
3333
timestamp: String.t() | number(),
34-
discarded_events: %{{String.t(), reasons()} => pos_integer()}
34+
discarded_events:
35+
list(%{reason: reasons(), category: String.t(), quantity: pos_integer()})
3536
}
3637

3738
defstruct [:timestamp, :discarded_events]
@@ -56,10 +57,10 @@ defmodule Sentry.ClientReport do
5657
GenServer.start_link(__MODULE__, %__MODULE__{}, name: __MODULE__)
5758
end
5859

59-
@spec add_discarded_event(String.t(), String.t()) :: :ok
60-
def add_discarded_event(type, reason) do
60+
@spec add_discarded_event({reasons(), String.t()}) :: :ok
61+
def add_discarded_event({reason, category}) do
6162
if Enum.member?(@client_report_reasons, reason) do
62-
GenServer.cast(__MODULE__, {:add_discarded_event, {type, reason}})
63+
GenServer.cast(__MODULE__, {:add_discarded_event, {reason, category}})
6364
end
6465

6566
:ok
@@ -72,25 +73,33 @@ defmodule Sentry.ClientReport do
7273
end
7374

7475
@impl true
75-
def handle_cast({:add_discarded_event, {type, reason}}, client_report) do
76+
def handle_cast({:add_discarded_event, {reason, category}}, client_report) do
7677
if client_report.discarded_events == nil do
77-
{:noreply, %{client_report | discarded_events: %{{type, reason} => 1}}}
78+
{:noreply, %{client_report | discarded_events: %{{reason, category} => 1}}}
7879
else
79-
discarded_events = Map.update(client_report.discarded_events, {type, reason}, 1, &(&1 + 1))
80+
discarded_events =
81+
Map.update(client_report.discarded_events, {reason, category}, 1, &(&1 + 1))
82+
8083
{:noreply, %{client_report | discarded_events: discarded_events}}
8184
end
8285
end
8386

8487
@impl true
8588
def handle_info(:send_report, state) do
8689
if state.discarded_events != nil do
87-
updated_state = %{state | timestamp: timestamp()}
88-
# Transport.send_client_report(updated_state)
90+
updated_state = %{
91+
state
92+
| timestamp: timestamp(),
93+
discarded_events: transform_map(state.discarded_events)
94+
}
95+
96+
Client.send_client_report(updated_state)
8997
schedule_report()
9098
{:noreply, %__MODULE__{}}
9199
else
92100
# state is nil so nothing to send but keep looping
93101
schedule_report()
102+
IO.inspect(state)
94103
{:noreply, state}
95104
end
96105
end
@@ -105,4 +114,15 @@ defmodule Sentry.ClientReport do
105114
|> DateTime.to_iso8601()
106115
|> String.trim_trailing("Z")
107116
end
117+
118+
defp transform_map(discarded_events_map) do
119+
discarded_events_map
120+
|> Enum.map(fn {{reason, category}, quantity} ->
121+
%{
122+
reason: reason,
123+
category: category,
124+
quantity: quantity
125+
}
126+
end)
127+
end
108128
end

0 commit comments

Comments
 (0)