-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add telemetry handlers for Oban job events (#1029)
* chore: Add telemetry handlers for Oban job events * Test Oban job exception logger * Logger.warn -> Logger.warning
- Loading branch information
1 parent
414abeb
commit 9cd8436
Showing
7 changed files
with
129 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule Arrow.Telemetry do | ||
@moduledoc """ | ||
Telemetry listeners for Arrow business logic. | ||
""" | ||
require Logger | ||
|
||
@spec setup_telemetry() :: :ok | ||
def setup_telemetry do | ||
_ = | ||
:telemetry.attach_many( | ||
"oban", | ||
[[:oban, :job, :start], [:oban, :job, :stop], [:oban, :job, :exception]], | ||
# telemetry prefers event handler to be passed as a non-local function | ||
# capture--i.e., with module name included--for performance reasons. | ||
&Arrow.Telemetry.handle_event/4, | ||
[] | ||
) | ||
|
||
:ok | ||
end | ||
|
||
def handle_event(event, measures, meta, config) | ||
|
||
def handle_event([:oban, :job, :start], _measures, meta, _config) do | ||
Logger.info("Oban job started #{get_job_info(meta.job)}") | ||
end | ||
|
||
def handle_event([:oban, :job, :stop], measures, meta, _config) do | ||
Logger.info( | ||
"Oban job stopped #{get_job_info(meta.job)} state=#{meta.state} result=#{inspect(meta.result)} duration=#{measures.duration} memory=#{measures.memory} queue_time=#{measures.queue_time}" | ||
) | ||
end | ||
|
||
def handle_event([:oban, :job, :exception], measures, meta, _config) do | ||
details = | ||
case meta.kind do | ||
:error -> | ||
message = Exception.message(meta.reason) | ||
full_details = Exception.format(meta.kind, meta.reason, meta.stacktrace) | ||
"message=#{inspect(message)}\n#{full_details}" | ||
|
||
_other -> | ||
"\n#{Exception.format(meta.kind, meta.reason, meta.stacktrace)}" | ||
end | ||
|
||
Logger.warning( | ||
"Oban job exception #{get_job_info(meta.job)} state=#{meta.state} result=#{inspect(meta.result)} duration=#{measures.duration} memory=#{measures.memory} queue_time=#{measures.queue_time} #{details}" | ||
) | ||
end | ||
|
||
@gtfs_workers [inspect(Arrow.Gtfs.ImportWorker), inspect(Arrow.Gtfs.ValidationWorker)] | ||
|
||
defp get_job_info(%Oban.Job{worker: worker} = job) when worker in @gtfs_workers do | ||
Arrow.Gtfs.JobHelper.logging_params(job) | ||
end | ||
|
||
defp get_job_info(job) do | ||
"job_id=#{job.id} job_args=#{inspect(job.args)} job_worker=#{inspect(job.worker)}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule Arrow.ExceptionalWorker do | ||
@moduledoc """ | ||
Worker that raises an exception. | ||
""" | ||
use Oban.Worker | ||
|
||
@impl Oban.Worker | ||
def perform(%Oban.Job{args: %{"arg" => arg}}) do | ||
raise "argh! arg: #{arg}" | ||
end | ||
end | ||
|
||
defmodule Arrow.TelemetryTest do | ||
@moduledoc false | ||
use ExUnit.Case, async: true | ||
use Oban.Testing, repo: Arrow.Repo | ||
import ExUnit.CaptureLog | ||
|
||
describe "oban.job.exception listener" do | ||
test "logs exception info" do | ||
log = | ||
capture_log([level: :warning], fn -> | ||
try do | ||
perform_job(Arrow.ExceptionalWorker, %{ | ||
arg: "argyle gargoyle" | ||
}) | ||
rescue | ||
_ -> nil | ||
end | ||
end) | ||
|
||
assert log =~ "Oban job exception" | ||
assert log =~ ~s|message="argh! arg: argyle gargoyle"| | ||
end | ||
end | ||
end |