@@ -76,16 +76,32 @@ defmodule Sentry.LoggerHandler do
7676 default: nil
7777 ] ,
7878 sync_threshold: [
79- type: : non_neg_integer,
79+ type: { :or , [ nil , : non_neg_integer] } ,
8080 default: 100 ,
8181 doc: """
82- *since v10.6.0* - The number of queued events after which this handler switches
82+ ( *since v10.6.0*) The number of queued events after which this handler switches
8383 to *sync mode*. Generally, this handler sends messages to Sentry **asynchronously**,
8484 equivalent to using `result: :none` in `Sentry.send_event/2`. However, if the number
8585 of queued events exceeds this threshold, the handler will switch to *sync mode*,
8686 where it starts using `result: :sync` to block until the event is sent. If you always
8787 want to use sync mode, set this option to `0`. This option effectively implements
8888 **overload protection**.
89+
90+ If you would rather *drop events* to shed load instead, use the `:discard_threshold` option.
91+ `:sync_threshold` and `:discard_threshold` cannot be used together. The default behavior
92+ of the handler is to switch to sync mode, so to disable this option and discard events
93+ instead set `:sync_threshold` to `nil` and set `:discard_threshold` instead.
94+ """
95+ ] ,
96+ discard_threshold: [
97+ type: { :or , [ nil , :non_neg_integer ] } ,
98+ default: nil ,
99+ doc: """
100+ (*since v10.9.0*) The number of queued events after which this handler will start
101+ to **discard** events. This option effectively implements **load shedding**.
102+
103+ `:discard_threshold` and `:sync_threshold` cannot be used together. If you set this option,
104+ set `:sync_threshold` to `nil`.
89105 """
90106 ]
91107 ]
@@ -231,7 +247,8 @@ defmodule Sentry.LoggerHandler do
231247 :tags_from_metadata ,
232248 :capture_log_messages ,
233249 :rate_limiting ,
234- :sync_threshold
250+ :sync_threshold ,
251+ :discard_threshold
235252 ]
236253
237254 ## Logger handler callbacks
@@ -319,6 +336,11 @@ defmodule Sentry.LoggerHandler do
319336 config . rate_limiting && RateLimiter . increment ( handler_id ) == :rate_limited ->
320337 :ok
321338
339+ # Discard event.
340+ config . discard_threshold &&
341+ SenderPool . get_queued_events_counter ( ) >= config . discard_threshold ->
342+ :ok
343+
322344 true ->
323345 # Logger handlers run in the process that logs, so we already read all the
324346 # necessary Sentry context from the process dictionary (when creating the event).
@@ -407,7 +429,14 @@ defmodule Sentry.LoggerHandler do
407429 |> Map . to_list ( )
408430 |> NimbleOptions . validate! ( @ options_schema )
409431
410- struct! ( existing_config , validated_config )
432+ config = struct! ( existing_config , validated_config )
433+
434+ if config . sync_threshold && config . discard_threshold do
435+ raise ArgumentError ,
436+ ":sync_threshold and :discard_threshold cannot be used together, one of them must be nil"
437+ else
438+ config
439+ end
411440 end
412441
413442 defp log_from_crash_reason (
@@ -642,7 +671,8 @@ defmodule Sentry.LoggerHandler do
642671
643672 defp capture ( unquote ( function ) , exception_or_message , sentry_opts , % __MODULE__ { } = config ) do
644673 sentry_opts =
645- if SenderPool . get_queued_events_counter ( ) >= config . sync_threshold do
674+ if config . sync_threshold &&
675+ SenderPool . get_queued_events_counter ( ) >= config . sync_threshold do
646676 Keyword . put ( sentry_opts , :result , :sync )
647677 else
648678 sentry_opts
0 commit comments