From 6cc44c03e24fa24aa65fddd9d6c87ee550f42bcb Mon Sep 17 00:00:00 2001 From: Sean Lang Date: Thu, 29 Aug 2024 16:37:18 -0400 Subject: [PATCH] add was_leader metadata to oban.peer.election event --- lib/oban/peers/global.ex | 2 +- lib/oban/peers/postgres.ex | 4 ++-- lib/oban/telemetry.ex | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/oban/peers/global.ex b/lib/oban/peers/global.ex index 9799f4a3..24e0a44c 100644 --- a/lib/oban/peers/global.ex +++ b/lib/oban/peers/global.ex @@ -96,7 +96,7 @@ defmodule Oban.Peers.Global do :telemetry.span([:oban, :peer, :election], meta, fn -> locked? = :global.set_lock(key(state), nodes(), 0) - {locked?, %{meta | leader: locked?}} + {locked?, %{was_leader: meta.leader}, %{meta | leader: locked?}} end) if locked?, do: notify_lock(state.conf) diff --git a/lib/oban/peers/postgres.ex b/lib/oban/peers/postgres.ex index b94f9f77..238a9c49 100644 --- a/lib/oban/peers/postgres.ex +++ b/lib/oban/peers/postgres.ex @@ -106,14 +106,14 @@ defmodule Oban.Peers.Postgres do case Repo.transaction(state.conf, fun, retry: 1) do {:ok, state} -> - {state, %{meta | leader: state.leader?}} + {state, %{was_leader: meta.leader}, %{meta | leader: state.leader?}} {:error, :rollback} -> # The peer maintains its current `leader?` status on rollback—this may cause # inconsistency if the leader encounters an error and multiple rollbacks happen in # sequence. That tradeoff is acceptable because the situation is unlikely and less of # an issue than crashing the peer. - {state, meta} + {state, %{was_leader: meta.leader}, meta} end end) diff --git a/lib/oban/telemetry.ex b/lib/oban/telemetry.ex index a3937900..4fe6bc4d 100644 --- a/lib/oban/telemetry.ex +++ b/lib/oban/telemetry.ex @@ -177,13 +177,14 @@ defmodule Oban.Telemetry do | event | measures | metadata | | ------------ | -------------- | -------------------------------------------------------------- | | `:start` | `:system_time` | `:conf`, `:leader`, `:peer`, | - | `:stop` | `:duration` | `:conf`, `:leader`, `:peer`, | + | `:stop` | `:duration` | `:conf`, `:leader`, `:peer`, `:was_leader`, | | `:exception` | `:duration` | `:conf`, `:leader`, `:peer`, `:kind`, `:reason`, `:stacktrace` | #### Metadata * `:conf`, `:kind`, `:reason`, `:stacktrace` — see the explanation in notifier metadata above * `:leader` — whether the peer is the current leader + * `:was_leader` — whether the peer was the leader before the election occurred * `:peer` — the module used for peering ## Queue Shutdown Events