Skip to content

Commit

Permalink
oban
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Nov 27, 2024
1 parent 634ed05 commit 22f9c16
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/tower/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ defmodule Tower.Event do
end

defp logger_metadata(log_event) do
Map.take(log_event[:meta] || %{}, logger_metadata_keys())
(log_event[:meta] || %{})
|> Map.merge(Enum.into(Logger.metadata(), %{}))
|> Map.take(logger_metadata_keys())
end

defp logger_metadata_keys do
Expand Down
6 changes: 6 additions & 0 deletions test/support/test_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule TestApp.UncaughtThrowWorker do

@impl Oban.Worker
def perform(%Oban.Job{}) do
Logger.metadata(user_id: 123, secret: "secret")

throw("something")

:ok
Expand All @@ -14,6 +16,8 @@ defmodule TestApp.AbnormalExitWorker do

@impl Oban.Worker
def perform(%Oban.Job{}) do
Logger.metadata(user_id: 123, secret: "secret")

exit(:abnormal)

:ok
Expand All @@ -25,6 +29,8 @@ defmodule TestApp.RuntimeErrorWorker do

@impl Oban.Worker
def perform(%Oban.Job{}) do
Logger.metadata(user_id: 123, secret: "secret")

raise "error from an Oban worker"

:ok
Expand Down
25 changes: 25 additions & 0 deletions test/tower/oban_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule TowerObanTest do
end

test "reports raised exception in an Oban worker" do
put_env(:logger_metadata, [:user_id])

TestApp.RuntimeErrorWorker.new(%{}, max_attempts: 1)
|> Oban.insert()

Expand All @@ -38,6 +40,7 @@ defmodule TowerObanTest do
kind: :error,
reason: %RuntimeError{message: "error from an Oban worker"},
stacktrace: stacktrace,
metadata: metadata,
by: Tower.ObanExceptionHandler
}
] = Tower.EphemeralReporter.events()
Expand All @@ -46,9 +49,12 @@ defmodule TowerObanTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
end

test "reports uncaught throw generated in an Oban worker" do
put_env(:logger_metadata, [:user_id])

TestApp.UncaughtThrowWorker.new(%{}, max_attempts: 1)
|> Oban.insert()

Expand All @@ -61,6 +67,7 @@ defmodule TowerObanTest do
kind: :error,
reason: %Oban.CrashError{reason: "something"},
stacktrace: stacktrace,
metadata: metadata,
by: Tower.ObanExceptionHandler
}
] = Tower.EphemeralReporter.events()
Expand All @@ -69,9 +76,12 @@ defmodule TowerObanTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
end

test "reports abnormal exit generated in an Oban worker" do
put_env(:logger_metadata, [:user_id])

TestApp.AbnormalExitWorker.new(%{}, max_attempts: 1)
|> Oban.insert()

Expand All @@ -84,6 +94,7 @@ defmodule TowerObanTest do
kind: :error,
reason: %Oban.CrashError{reason: :abnormal},
stacktrace: stacktrace,
metadata: metadata,
by: Tower.ObanExceptionHandler
}
] = Tower.EphemeralReporter.events()
Expand All @@ -92,6 +103,20 @@ defmodule TowerObanTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
end

defp put_env(key, value) do
original_value = Application.get_env(:tower, key)
Application.put_env(:tower, key, value)

on_exit(fn ->
if original_value == nil do
Application.delete_env(:tower, key)
else
Application.put_env(:tower, key, original_value)
end
end)
end

defp recent_datetime?(datetime) do
Expand Down
3 changes: 1 addition & 2 deletions test/tower_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,7 @@ defmodule TowerTest do

capture_log(fn ->
in_unlinked_process(fn ->
Logger.metadata(%{user_id: 123})
Logger.metadata(%{secret: "secret"})
Logger.metadata(user_id: 123, secret: "secret")

raise "an error"
end)
Expand Down

0 comments on commit 22f9c16

Please sign in to comment.