From 824b911f1faf5b84e8d9103646c31774ee94393a Mon Sep 17 00:00:00 2001 From: Martin Svalin <166497265+martinsvalin-kivra@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:01:40 +0200 Subject: [PATCH] Include zeros for missing queue states in Oban polling metrics (#245) * Include zeros for missing queue states in polling metrics DB query will only count jobs that exist. When counts for a particular queue state combination disappears from one poll to the next, some tools will show the stale last-seen count, when the true count is zero. This change will include zeros for all missing queue + state combinations. Queues are taken from the Oban config, and Oban exposes all possible states in `Oban.Job.states/0`. This addresses issue #202 * string map keys for zeros --------- Co-authored-by: Martin Svalin --- lib/prom_ex/plugins/oban.ex | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/prom_ex/plugins/oban.ex b/lib/prom_ex/plugins/oban.ex index 78a64ed..b5682a8 100644 --- a/lib/prom_ex/plugins/oban.ex +++ b/lib/prom_ex/plugins/oban.ex @@ -438,7 +438,8 @@ if Code.ensure_loaded?(Oban) do config |> Oban.Repo.all(query) - |> Enum.each(fn {queue, state, count} -> + |> include_zeros_for_missing_queue_states() + |> Enum.each(fn {{queue, state}, count} -> measurements = %{count: count} metadata = %{name: normalize_module_name(oban_supervisor), queue: queue, state: state} @@ -446,6 +447,16 @@ if Code.ensure_loaded?(Oban) do end) end + defp include_zeros_for_missing_queue_states(query_result) do + all_queues = Keyword.keys(Oban.config().queues) + all_states = Oban.Job.states() + + zeros = for queue <- all_queues, state <- all_states, into: %{}, do: {{to_string(queue), to_string(state)}, 0} + counts = for {queue, state, count} <- query_result, into: %{}, do: {{queue, state}, count} + + Map.merge(zeros, counts) + end + defp get_oban_supervisors(opts) do opts |> Keyword.get(:oban_supervisors, [Oban])