Skip to content

Commit 7c79ec4

Browse files
committed
feat(oban): add option to include job tags into reported exception tags
1 parent 49ef2f7 commit 7c79ec4

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

lib/sentry/application.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ defmodule Sentry.Application do
7979
end
8080

8181
if config[:oban][:capture_errors] do
82-
Sentry.Integrations.Oban.ErrorReporter.attach()
82+
Sentry.Integrations.Oban.ErrorReporter.attach(config[:oban])
8383
end
8484

8585
if config[:quantum][:cron][:enabled] do

lib/sentry/config.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ defmodule Sentry.Config do
5151
tuples. *Available since 10.3.0*.
5252
"""
5353
],
54+
oban_tags: [
55+
type: :boolean,
56+
default: false,
57+
doc: """
58+
Whether to include Oban job tags in Sentry error tags. When enabled, the `job.tags`
59+
will be joined with a "," and added as an `oban_tags` tag to Sentry events.
60+
"""
61+
],
5462
cron: [
5563
doc: """
5664
Configuration options for configuring [*crons*](https://docs.sentry.io/product/crons/)

lib/sentry/integrations/oban/error_reporter.ex

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ defmodule Sentry.Integrations.Oban.ErrorReporter do
44
# See this blog post:
55
# https://getoban.pro/articles/enhancing-error-reporting
66

7-
@spec attach() :: :ok
8-
def attach do
7+
@spec attach(keyword()) :: :ok
8+
def attach(config \\ []) when is_list(config) do
99
_ =
1010
:telemetry.attach(
1111
__MODULE__,
1212
[:oban, :job, :exception],
1313
&__MODULE__.handle_event/4,
14-
:no_config
14+
config
1515
)
1616

1717
:ok
@@ -21,32 +21,41 @@ defmodule Sentry.Integrations.Oban.ErrorReporter do
2121
[atom(), ...],
2222
term(),
2323
%{required(:job) => struct(), optional(term()) => term()},
24-
:no_config
24+
keyword()
2525
) :: :ok
2626
def handle_event(
2727
[:oban, :job, :exception],
2828
_measurements,
2929
%{job: job, kind: kind, reason: reason, stacktrace: stacktrace} = _metadata,
30-
:no_config
30+
config
3131
) do
3232
if report?(reason) do
33-
report(job, kind, reason, stacktrace)
33+
report(job, kind, reason, stacktrace, config)
3434
else
3535
:ok
3636
end
3737
end
3838

39-
defp report(job, kind, reason, stacktrace) do
39+
defp report(job, kind, reason, stacktrace, config) do
4040
stacktrace =
4141
case {apply(Oban.Worker, :from_string, [job.worker]), stacktrace} do
4242
{{:ok, atom_worker}, []} -> [{atom_worker, :process, 1, []}]
4343
_ -> stacktrace
4444
end
4545

46+
base_tags = %{oban_worker: job.worker, oban_queue: job.queue, oban_state: job.state}
47+
48+
tags =
49+
if config[:oban_tags] === true and is_list(job.tags) and not Enum.empty?(job.tags) do
50+
Map.put(base_tags, :oban_tags, Enum.join(job.tags, ","))
51+
else
52+
base_tags
53+
end
54+
4655
opts =
4756
[
4857
stacktrace: stacktrace,
49-
tags: %{oban_worker: job.worker, oban_queue: job.queue, oban_state: job.state},
58+
tags: tags,
5059
fingerprint: [job.worker, "{{ default }}"],
5160
extra:
5261
Map.take(job, [:args, :attempt, :id, :max_attempts, :meta, :queue, :tags, :worker]),

test/sentry/integrations/oban/error_reporter_test.exs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Sentry.Integrations.Oban.ErrorReporterTest do
44
alias Sentry.Integrations.Oban.ErrorReporter
55

66
defmodule MyWorker do
7-
use Oban.Worker
7+
use Oban.Worker, tags: ["tag1", "tag2"]
88

99
@impl Oban.Worker
1010
def perform(%Oban.Job{}), do: :ok
@@ -154,11 +154,29 @@ defmodule Sentry.Integrations.Oban.ErrorReporterTest do
154154
assert Sentry.Test.pop_sentry_reports() == []
155155
end
156156
end
157+
158+
test "includes oban_tags when config option is enabled" do
159+
Sentry.Test.start_collecting()
160+
161+
emit_telemetry_for_failed_job(:error, %RuntimeError{message: "oops"}, [], oban_tags: true)
162+
163+
assert [event] = Sentry.Test.pop_sentry_reports()
164+
assert event.tags.oban_tags == "tag1,tag2"
165+
end
166+
167+
test "excludes oban_tags when config option is disabled" do
168+
Sentry.Test.start_collecting()
169+
170+
emit_telemetry_for_failed_job(:error, %RuntimeError{message: "oops"}, [], oban_tags: false)
171+
172+
assert [event] = Sentry.Test.pop_sentry_reports()
173+
assert is_nil(Map.get(event.tags, :oban_tags))
174+
end
157175
end
158176

159177
## Helpers
160178

161-
defp emit_telemetry_for_failed_job(kind, reason, stacktrace) do
179+
defp emit_telemetry_for_failed_job(kind, reason, stacktrace, config \\ []) do
162180
job =
163181
%{"id" => "123", "entity" => "user", "type" => "delete"}
164182
|> MyWorker.new()
@@ -169,7 +187,7 @@ defmodule Sentry.Integrations.Oban.ErrorReporterTest do
169187
[:oban, :job, :exception],
170188
%{},
171189
%{job: job, kind: kind, reason: reason, stacktrace: stacktrace},
172-
:no_config
190+
config
173191
)
174192

175193
job

test/sentry/logger_handler_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ defmodule Sentry.LoggerHandlerTest do
678678
%ArgumentError{
679679
message:
680680
":sync_threshold and :discard_threshold cannot be used together, one of them must be nil"
681-
}, _}}} =
681+
},
682+
_}}} =
682683
:logger.update_handler_config(
683684
handler_name,
684685
:config,

0 commit comments

Comments
 (0)