analytics/lib/oban_error_reporter.ex
Adrian Gruntkowski 41fef85d29
Implement resumable GA4 imports to work around rate limiting (#4049)
* Add support for resuming import to GA4 importer

* Handle rate limiting gracefully for all remainig GA4 HTTP requests

* Show notice tooltip for long running imports

* Bump resume job schedule delay to 65 minutes

* Fix tooltip styling
2024-04-30 18:06:18 +02:00

69 lines
2.0 KiB
Elixir

defmodule ObanErrorReporter do
require Logger
def handle_event(name, measurements, metadata, _) do
# handling telemetry event in a try/catch block
# to avoid handler detachment in the case of an error
# see https://hexdocs.pm/telemetry/telemetry.html#attach/4
try do
handle_event(name, measurements, metadata)
catch
kind, reason ->
message = Exception.format(kind, reason, __STACKTRACE__)
Logger.error(message)
end
end
defp handle_event([:oban, :job, :exception], measure, %{job: job} = meta) do
extra =
job
|> Map.take([:id, :args, :meta, :queue, :worker])
|> Map.merge(measure)
on_job_exception(job)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
end
defp handle_event([:oban, :notifier, :exception], _timing, meta) do
extra = Map.take(meta, ~w(channel payload)a)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
end
defp handle_event([:oban, :plugin, :exception], _timing, meta) do
extra = Map.take(meta, ~w(plugin)a)
Sentry.capture_exception(meta.reason, stacktrace: meta.stacktrace, extra: extra)
end
defp on_job_exception(%Oban.Job{
queue: "analytics_imports",
args: %{"import_id" => import_id},
state: "executing",
attempt: attempt,
max_attempts: max_attempts
})
when attempt >= max_attempts do
site_import = Plausible.Repo.get(Plausible.Imported.SiteImport, import_id)
if site_import do
Plausible.Workers.ImportAnalytics.import_fail(site_import, [])
end
end
defp on_job_exception(%Oban.Job{
queue: "analytics_imports",
args: %{"import_id" => import_id},
state: "executing"
}) do
site_import = Plausible.Repo.get(Plausible.Imported.SiteImport, import_id)
if site_import do
Plausible.Workers.ImportAnalytics.import_fail_transient(site_import)
end
end
defp on_job_exception(_job), do: :ignore
end