-
Notifications
You must be signed in to change notification settings - Fork 33
Prune resolved errors #69
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 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f34d3f
Pruner plugin
crbelaus 778d8e0
Autostart pruner plugin
crbelaus 8d77207
Improve docs
crbelaus a676d27
Update docs
crbelaus b1c51d0
Update default values
crbelaus 3719cf1
Prune occurrences manually without CASCADE
crbelaus 1cb0873
Add seeds script
crbelaus 420d9c2
Count pruned occurrences
crbelaus ff60a6c
Add index to errors last_occurrence_at
crbelaus 467b3fb
Fix pruning deadline calculation
crbelaus 3f3fff8
Limit inside Stream.unfold
crbelaus 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,142 @@ | ||
| defmodule ErrorTracker.Plugins.Pruner do | ||
crbelaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @moduledoc """ | ||
| Periodically delete resolved errors based on their age. | ||
| Pruning allows you to keep your database size under control by removing old errors that are not | ||
| needed anymore. | ||
| ## Using the pruner | ||
| To enable the pruner you must register the plugin in the ErrorTracker configuration. This will use | ||
| the default options, which is to prune errors resolved after 5 minutes. | ||
| config :error_tracker, | ||
| plugins: [ErrorTracker.Plugins.Pruner] | ||
| You can override the default options by passing them as an argument when registering the plugin. | ||
| config :error_tracker, | ||
| plugins: [{ErrorTracker.Plugins.Pruner, max_age: :timer.minutes(30)}] | ||
| ## Options | ||
| - `:limit` - the maximum number of errors to prune on each execution. Occurrences are removed | ||
crbelaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| along the errors. The default is 200 to prevent timeouts and unnecesary database load. | ||
| - `:max_age` - the number of milliseconds after a resolved error may be pruned. The default is 24 | ||
| hours. | ||
| - `:interval` - the interval in milliseconds between pruning runs. The default is 30 minutes. | ||
| You may find the `:timer` module functions useful to pass readable values to the `:max_age` and | ||
| `:interval` options. | ||
| ## Manual pruning | ||
crbelaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| In certain cases you may prefer to run the pruner manually. This can be done by calling the | ||
| `prune_errors/2` function from your application code. This function supports the `:limit` and | ||
| `:max_age` options as described above. | ||
| For example, you may call this function from an Oban worker so you can leverage Oban's cron | ||
| capabilities and have a more granular control over when pruning is run. | ||
| defmodule MyApp.ErrorPruner do | ||
| use Oban.Job | ||
| def perform(%Job{}) do | ||
| ErrorTracker.Plugins.Pruner.prune_errors(limit: 10_000, max_age: :timer.minutes(60)) | ||
| end | ||
| end | ||
| """ | ||
| use GenServer | ||
|
|
||
| import Ecto.Query | ||
|
|
||
| alias ErrorTracker.Error | ||
| alias ErrorTracker.Occurrence | ||
| alias ErrorTracker.Repo | ||
|
|
||
| @doc """ | ||
| Prunes resolved errors. | ||
| You do not need to use this function if you activate the Pruner plugin. This function is exposed | ||
| only for advanced use cases and Oban integration. | ||
| ## Options | ||
| - `:limit` - the maximum number of errors to prune on each execution. Occurrences are removed | ||
| along the errors. The default is 200 to prevent timeouts and unnecesary database load. | ||
| - `:max_age` - the number of milliseconds after a resolved error may be pruned. The default is 24 | ||
| hours. You may find the `:timer` module functions useful to pass readable values to this option. | ||
| """ | ||
| @spec prune_errors(keyword()) :: {:ok, list(Error.t())} | ||
| def prune_errors(opts \\ []) do | ||
| limit = opts[:limit] || raise ":limit option is required" | ||
| max_age = opts[:max_age] || raise ":max_age option is required" | ||
| time = DateTime.add(DateTime.utc_now(), max_age, :millisecond) | ||
odarriba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| errors = | ||
| Repo.all( | ||
| from error in Error, | ||
| select: [:id, :kind, :source_line, :source_function], | ||
| where: error.status == :resolved, | ||
| where: error.last_occurrence_at < ^time, | ||
odarriba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| limit: ^limit | ||
| ) | ||
|
|
||
| if Enum.any?(errors) do | ||
| :ok = | ||
| errors | ||
| |> Ecto.assoc(:occurrences) | ||
| |> limit(1000) | ||
odarriba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| |> prune_occurrences() | ||
| |> Stream.run() | ||
|
|
||
| Repo.delete_all(from error in Error, where: error.id in ^Enum.map(errors, & &1.id)) | ||
| end | ||
|
|
||
| {:ok, errors} | ||
| end | ||
|
|
||
| defp prune_occurrences(occurrences_query) do | ||
| Stream.unfold(occurrences_query, fn occurrences_query -> | ||
| occurrences_ids = Repo.all(from occurrence in occurrences_query, select: occurrence.id) | ||
|
|
||
| case Repo.delete_all(from o in Occurrence, where: o.id in ^occurrences_ids) do | ||
| {0, _} -> nil | ||
| {deleted, _} -> {deleted, occurrences_query} | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| def start_link(state \\ []) do | ||
| GenServer.start_link(__MODULE__, state, name: __MODULE__) | ||
| end | ||
|
|
||
| @impl GenServer | ||
| @doc false | ||
| def init(state \\ []) do | ||
| state = %{ | ||
| limit: state[:limit] || 200, | ||
| max_age: state[:max_age] || :timer.hours(24), | ||
| interval: state[:interval] || :timer.minutes(30) | ||
| } | ||
|
|
||
| {:ok, schedule_prune(state)} | ||
| end | ||
|
|
||
| @impl GenServer | ||
| @doc false | ||
| def handle_info(:prune, state) do | ||
| {:ok, _pruned} = prune_errors(state) | ||
|
|
||
| {:noreply, schedule_prune(state)} | ||
| end | ||
|
|
||
| defp schedule_prune(state = %{interval: interval}) do | ||
| Process.send_after(self(), :prune, interval) | ||
|
|
||
| state | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| adapter = | ||
| case Application.get_env(:error_tracker, :ecto_adapter) do | ||
| :postgres -> Ecto.Adapters.Postgres | ||
| :sqlite3 -> Ecto.Adapters.SQLite3 | ||
| end | ||
|
|
||
| defmodule ErrorTrackerDev.Repo do | ||
| use Ecto.Repo, otp_app: :error_tracker, adapter: adapter | ||
| end | ||
|
|
||
| ErrorTrackerDev.Repo.start_link() | ||
|
|
||
| ErrorTrackerDev.Repo.delete_all(ErrorTracker.Error) | ||
|
|
||
| errors = | ||
| for i <- 1..100 do | ||
| %{ | ||
| kind: "Error #{i}", | ||
| reason: "Reason #{i}", | ||
| source_line: "line", | ||
| source_function: "function", | ||
| status: :unresolved, | ||
| fingerprint: "#{i}", | ||
| last_occurrence_at: DateTime.utc_now(), | ||
| inserted_at: DateTime.utc_now(), | ||
| updated_at: DateTime.utc_now() | ||
| } | ||
| end | ||
|
|
||
| {_, errors} = dbg(ErrorTrackerDev.Repo.insert_all(ErrorTracker.Error, errors, returning: [:id])) | ||
|
|
||
| for error <- errors do | ||
| occurrences = | ||
| for _i <- 1..200 do | ||
| %{ | ||
| context: %{}, | ||
| reason: "REASON", | ||
| stacktrace: %ErrorTracker.Stacktrace{}, | ||
| error_id: error.id, | ||
| inserted_at: DateTime.utc_now() | ||
| } | ||
| end | ||
|
|
||
| ErrorTrackerDev.Repo.insert_all(ErrorTracker.Occurrence, occurrences) | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.