Skip to content

Commit

Permalink
Oban : logs des composants importants (#4410)
Browse files Browse the repository at this point in the history
* Oban : logs des composants importants

* Improve variable names

Co-authored-by: Thibaut Barrère <thibaut.barrere@gmail.com>

---------

Co-authored-by: Thibaut Barrère <thibaut.barrere@gmail.com>
  • Loading branch information
AntoineAugusti and thbar authored Dec 31, 2024
1 parent 82c79ca commit 36dc757
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
27 changes: 25 additions & 2 deletions apps/transport/lib/jobs/oban_logger.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
defmodule Transport.Jobs.ObanLogger do
@moduledoc """
Logs the Oban job exceptions as warnings
Setup telemetry/logging for Oban.
We:
- log job exceptions as warnings
- log Oban events related to the orchestration (notifier, queues, plugins etc.)
- we send an email when a job failed after its maximum attempt for jobs with a specific tag
"""
require Logger

@tag_email_on_failure "email_on_failure"

@doc """
Expand Down Expand Up @@ -35,5 +41,22 @@ defmodule Transport.Jobs.ObanLogger do
)
end

def setup, do: :telemetry.attach("oban-logger", [:oban, :job, :exception], &handle_event/4, nil)
def setup do
:telemetry.attach("oban-logger", [:oban, :job, :exception], &handle_event/4, nil)

# Log recommended events for production.
# We leave out `job` events because job start/end can be quite noisy.
# https://hexdocs.pm/oban/preparing_for_production.html#logging
# https://hexdocs.pm/oban/Oban.Telemetry.html
# We may simplify this when
# https://github.com/oban-bg/oban/commit/13eabe3f8019e350ef979369a26f186bdf7be63e
# will be released.
events = [
[:oban, :notifier, :switch],
[:oban, :queue, :shutdown],
[:oban, :stager, :switch]
]

:telemetry.attach_many("oban-default-logger", events, &Oban.Telemetry.handle_event/4, encode: true, level: :info)
end
end
13 changes: 13 additions & 0 deletions apps/transport/test/transport/jobs/oban_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ defmodule Transport.Test.Transport.Jobs.ObanLoggerTest do
"Un job Oban Transport.Test.Transport.Jobs.ObanLoggerJobTag vient d'échouer, il serait bien d'investiguer."
)
end

test "oban default logger is set up for important components" do
registered_handlers = Enum.filter(:telemetry.list_handlers([]), &(&1.id == "oban-default-logger"))

assert Enum.count(registered_handlers) > 0

components =
registered_handlers
|> Enum.map(fn %{event_name: [:oban, component, _]} -> component end)
|> MapSet.new()

assert MapSet.new([:notifier, :queue, :stager]) == MapSet.new(components)
end
end

0 comments on commit 36dc757

Please sign in to comment.