Skip to content

Commit

Permalink
Include zeros for missing queue states in polling metrics
Browse files Browse the repository at this point in the history
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 akoutmos#202
  • Loading branch information
martinsvalin-kivra authored Sep 3, 2024
1 parent 6f97943 commit 8319317
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/prom_ex/plugins/oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,25 @@ 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}

:telemetry.execute([:prom_ex, :plugin, :oban, :queue, :length, :count], measurements, metadata)
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: {{queue, 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])
Expand Down

0 comments on commit 8319317

Please sign in to comment.