Skip to content

Commit c26a782

Browse files
whatyouhidemilmazz
andauthored
FIx monitor slug in Oban + Quantum integrations (#721)
Co-authored-by: Milton Mazzarri <[email protected]>
1 parent 5a496f9 commit c26a782

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

lib/sentry/config.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ defmodule Sentry.Config do
55
oban: [
66
type: :keyword_list,
77
doc: """
8-
Configuration for the [Oban](https://github.com/sorentwo/oban) integration. *Available
8+
Configuration for the [Oban](https://github.com/sorentwo/oban) integration. The Oban
9+
integration requires at minumum Oban Pro v0.14 or Oban v.2.17.6. *Available
910
since v10.2.0*.
1011
""",
1112
keys: [

lib/sentry/integrations/oban/cron.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ defmodule Sentry.Integrations.Oban.Cron do
7272
[
7373
check_in_id: "oban-#{job.id}",
7474
# This is already a binary.
75-
monitor_slug: job.worker,
75+
monitor_slug: slugify(job.worker),
7676
monitor_config: [schedule: schedule_opts]
7777
]
7878
else
@@ -99,4 +99,12 @@ defmodule Sentry.Integrations.Oban.Cron do
9999
|> System.convert_time_unit(:native, :millisecond)
100100
|> Kernel./(1000)
101101
end
102+
103+
# MyApp.SomeWorker -> "my-app-some-worker"
104+
defp slugify(worker_name) do
105+
worker_name
106+
|> String.split(".")
107+
|> Enum.map_join("-", &(&1 |> Macro.underscore() |> String.replace("_", "-")))
108+
|> String.slice(0, 50)
109+
end
102110
end

lib/sentry/integrations/quantum/cron.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule Sentry.Integrations.Quantum.Cron do
6565
[
6666
check_in_id: "quantum-#{id}",
6767
# This is already a binary.
68-
monitor_slug: "quantum-#{inspect(job.name)}",
68+
monitor_slug: "quantum-#{slugify(job.name)}",
6969
monitor_config: [schedule: schedule_opts]
7070
]
7171
else
@@ -92,4 +92,18 @@ defmodule Sentry.Integrations.Quantum.Cron do
9292
|> System.convert_time_unit(:native, :millisecond)
9393
|> Kernel./(1000)
9494
end
95+
96+
defp slugify(job_name) when is_atom(job_name) do
97+
case Atom.to_string(job_name) do
98+
"Elixir." <> module -> module |> Macro.underscore() |> slugify()
99+
other -> slugify(other)
100+
end
101+
end
102+
103+
defp slugify(job_name) when is_binary(job_name) do
104+
job_name
105+
|> String.downcase()
106+
|> String.replace(["_", "#", "<", ">", ".", " ", "/"], "-")
107+
|> String.slice(0, 50)
108+
end
95109
end

test/sentry/integrations/oban/cron_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if Version.match?(System.version(), "~> 1.13") do
6464

6565
assert check_in_body["check_in_id"] == "oban-123"
6666
assert check_in_body["status"] == "in_progress"
67-
assert check_in_body["monitor_slug"] == "Sentry.MyWorker"
67+
assert check_in_body["monitor_slug"] == "sentry-my-worker"
6868
assert check_in_body["duration"] == nil
6969
assert check_in_body["environment"] == "test"
7070

@@ -120,7 +120,7 @@ if Version.match?(System.version(), "~> 1.13") do
120120

121121
assert check_in_body["check_in_id"] == "oban-942"
122122
assert check_in_body["status"] == unquote(expected_status)
123-
assert check_in_body["monitor_slug"] == "Sentry.MyWorker"
123+
assert check_in_body["monitor_slug"] == "sentry-my-worker"
124124
assert check_in_body["duration"] == 12.099
125125
assert check_in_body["environment"] == "test"
126126

@@ -164,7 +164,7 @@ if Version.match?(System.version(), "~> 1.13") do
164164

165165
assert check_in_body["check_in_id"] == "oban-942"
166166
assert check_in_body["status"] == "error"
167-
assert check_in_body["monitor_slug"] == "Sentry.MyWorker"
167+
assert check_in_body["monitor_slug"] == "sentry-my-worker"
168168
assert check_in_body["duration"] == 12.099
169169
assert check_in_body["environment"] == "test"
170170

test/sentry/integrations/quantum/cron_test.exs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if Version.match?(System.version(), "~> 1.12") do
5959

6060
assert check_in_body["check_in_id"] == "quantum-#{:erlang.phash2(ref)}"
6161
assert check_in_body["status"] == "in_progress"
62-
assert check_in_body["monitor_slug"] == "quantum-:test_job"
62+
assert check_in_body["monitor_slug"] == "quantum-test-job"
6363
assert check_in_body["duration"] == nil
6464
assert check_in_body["environment"] == "test"
6565

@@ -99,7 +99,7 @@ if Version.match?(System.version(), "~> 1.12") do
9999

100100
assert check_in_body["check_in_id"] == "quantum-#{:erlang.phash2(ref)}"
101101
assert check_in_body["status"] == "error"
102-
assert check_in_body["monitor_slug"] == "quantum-:test_job"
102+
assert check_in_body["monitor_slug"] == "quantum-test-job"
103103
assert check_in_body["duration"] == 12.099
104104
assert check_in_body["environment"] == "test"
105105

@@ -141,7 +141,7 @@ if Version.match?(System.version(), "~> 1.12") do
141141

142142
assert check_in_body["check_in_id"] == "quantum-#{:erlang.phash2(ref)}"
143143
assert check_in_body["status"] == "ok"
144-
assert check_in_body["monitor_slug"] == "quantum-:test_job"
144+
assert check_in_body["monitor_slug"] == "quantum-test-job"
145145
assert check_in_body["duration"] == 12.099
146146
assert check_in_body["environment"] == "test"
147147

@@ -170,5 +170,38 @@ if Version.match?(System.version(), "~> 1.12") do
170170

171171
assert_receive {^ref, :done}, 1000
172172
end
173+
174+
for {job_name, expected_slug} <- [
175+
{:some_job, "quantum-some-job"},
176+
{MyApp.MyJob, "quantum-my-app-my-job"}
177+
] do
178+
test "works for a job named #{inspect(job_name)}", %{bypass: bypass} do
179+
test_pid = self()
180+
ref = make_ref()
181+
182+
Bypass.expect_once(bypass, "POST", "/api/1/envelope/", fn conn ->
183+
{:ok, body, conn} = Plug.Conn.read_body(conn)
184+
assert [{_headers, check_in_body}] = decode_envelope!(body)
185+
186+
assert check_in_body["monitor_slug"] == unquote(expected_slug)
187+
send(test_pid, {ref, :done})
188+
189+
Plug.Conn.send_resp(conn, 200, ~s<{"id": "1923"}>)
190+
end)
191+
192+
duration = System.convert_time_unit(12_099, :millisecond, :native)
193+
194+
:telemetry.execute([:quantum, :job, :stop], %{duration: duration}, %{
195+
job:
196+
Scheduler.new_job(
197+
name: unquote(job_name),
198+
schedule: Crontab.CronExpression.Parser.parse!("@daily")
199+
),
200+
telemetry_span_context: ref
201+
})
202+
203+
assert_receive {^ref, :done}, 1000
204+
end
205+
end
173206
end
174207
end

0 commit comments

Comments
 (0)