Skip to content

Commit

Permalink
Include zeros for missing queue states in Oban polling metrics (#245)
Browse files Browse the repository at this point in the history
* 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 <martin@lite.nu>
  • Loading branch information
martinsvalin-kivra and martinsvalin authored Oct 24, 2024
1 parent 115e851 commit 824b911
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 @@ -438,14 +438,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: {{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])
Expand Down

0 comments on commit 824b911

Please sign in to comment.