-
Notifications
You must be signed in to change notification settings - Fork 324
Centralize telemetry collector error handling via Poller.safe_invoke/3 #4148
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
Open
alco
wants to merge
4
commits into
main
Choose a base branch
from
erik/issue-32-safe-invoke
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
13b3967
Centralize telemetry collector error handling in Poller.safe_invoke/3
erik-the-implementer 275e0d9
Restore local defensive handling in StackSupervisor.Telemetry + forma…
91d26de
changeset: drop function-name references that go stale fast
alco e3a0fb8
Drop local defensive handling in StackSupervisor.Telemetry
alco 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@core/electric-telemetry': patch | ||
| '@core/sync-service': patch | ||
| --- | ||
|
|
||
| Wrap telemetry-poller MFAs in `ElectricTelemetry.Poller.safe_invoke/3` so that transient collector failures (`:noproc`, `:timeout`, `:shutdown`/`:normal` exits, `ArgumentError` from not-yet-created ETS tables) no longer cause `:telemetry_poller` to permanently remove the measurement from its polling list. Unexpected errors are now logged as warnings with the offending MFA and the collector keeps being polled on subsequent ticks. Strips now-redundant defensive `try/catch` / `with`-fallthrough code from individual collectors. | ||
|
|
||
| Note: user-supplied periodic measurement functions no longer have exceptions propagated up to `:telemetry_poller`'s own error logger — they are caught and logged via `ElectricTelemetry.Poller` instead. |
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
115 changes: 115 additions & 0 deletions
115
packages/electric-telemetry/test/electric/telemetry/poller_test.exs
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,115 @@ | ||
| defmodule ElectricTelemetry.PollerTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| import ExUnit.CaptureLog | ||
|
|
||
| alias ElectricTelemetry.Poller | ||
|
|
||
| defmodule Fixture do | ||
| def ok(), do: :done | ||
| def raise_argument(), do: raise(ArgumentError, "boom") | ||
| def raise_runtime(), do: raise(RuntimeError, "kaboom") | ||
| def exit_noproc(), do: exit({:noproc, {GenServer, :call, [:nowhere, :hi]}}) | ||
| def exit_timeout(), do: exit({:timeout, {GenServer, :call, [:slow, :hi]}}) | ||
| def exit_shutdown(), do: exit({:shutdown, :foo}) | ||
| def exit_normal_atom(), do: exit(:normal) | ||
| def exit_shutdown_atom(), do: exit(:shutdown) | ||
| def exit_weird(), do: exit(:weird) | ||
| def throw_it(), do: throw(:nope) | ||
| end | ||
|
|
||
| describe "safe_invoke/3" do | ||
| test "returns :ok and runs the function on success" do | ||
| assert Poller.safe_invoke(Fixture, :ok, []) == :ok | ||
| end | ||
|
|
||
| test "swallows ArgumentError (ETS missing, etc.) silently" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :raise_argument, []) == :ok end) | ||
| refute log =~ "crashed" | ||
| end | ||
|
|
||
| test "swallows generic exceptions with a warning" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :raise_runtime, []) == :ok end) | ||
| assert log =~ "crashed" | ||
| assert log =~ "kaboom" | ||
| end | ||
|
|
||
| test "swallows :noproc exit silently" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_noproc, []) == :ok end) | ||
| refute log =~ "exit" | ||
| end | ||
|
|
||
| test "swallows :timeout exit silently" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_timeout, []) == :ok end) | ||
| refute log =~ "exit" | ||
| end | ||
|
|
||
| test "swallows :shutdown exit silently" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_shutdown, []) == :ok end) | ||
| refute log =~ "exit" | ||
| end | ||
|
|
||
| test "swallows bare :normal exit silently" do | ||
| log = | ||
| capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_normal_atom, []) == :ok end) | ||
|
|
||
| refute log =~ "exit" | ||
| end | ||
|
|
||
| test "swallows bare :shutdown exit silently" do | ||
| log = | ||
| capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_shutdown_atom, []) == :ok end) | ||
|
|
||
| refute log =~ "exit" | ||
| end | ||
|
|
||
| test "logs a warning for unexpected exits" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :exit_weird, []) == :ok end) | ||
| assert log =~ "exit" | ||
| assert log =~ "weird" | ||
| end | ||
|
|
||
| test "logs a warning for throws" do | ||
| log = capture_log(fn -> assert Poller.safe_invoke(Fixture, :throw_it, []) == :ok end) | ||
| assert log =~ "throw" | ||
| end | ||
| end | ||
|
|
||
| describe "periodic_measurements/2 wrapping" do | ||
| defmodule CallbackMod do | ||
| @behaviour ElectricTelemetry.Poller | ||
| def builtin_periodic_measurements(_opts), do: [] | ||
| def some_measurement(_opts), do: :ok | ||
| end | ||
|
|
||
| test "wraps {m, f, a} tuples in safe_invoke" do | ||
| opts = %{periodic_measurements: [{CallbackMod, :some_measurement, []}]} | ||
|
|
||
| assert [{ElectricTelemetry.Poller, :safe_invoke, [CallbackMod, :some_measurement, [_]]}] = | ||
| Poller.periodic_measurements(opts, CallbackMod) | ||
| end | ||
|
|
||
| test "wraps bare function atoms in safe_invoke" do | ||
| opts = %{periodic_measurements: [:some_measurement]} | ||
|
|
||
| assert [{ElectricTelemetry.Poller, :safe_invoke, [CallbackMod, :some_measurement, [_]]}] = | ||
| Poller.periodic_measurements(opts, CallbackMod) | ||
| end | ||
|
|
||
| test "wraps anonymous functions in safe_invoke around user_measurement" do | ||
| f = fn _ -> :ok end | ||
| opts = %{periodic_measurements: [f]} | ||
|
|
||
| assert [ | ||
| {ElectricTelemetry.Poller, :safe_invoke, | ||
| [ElectricTelemetry.Poller, :user_measurement, [^f, _]]} | ||
| ] = | ||
| Poller.periodic_measurements(opts, CallbackMod) | ||
| end | ||
|
|
||
| test "leaves telemetry_poller builtins unwrapped" do | ||
| opts = %{periodic_measurements: [:memory, :persistent_term]} | ||
| assert Poller.periodic_measurements(opts, CallbackMod) == [:memory, :persistent_term] | ||
| end | ||
| 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
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.