Skip to content

Commit 3b06c3c

Browse files
feat: Use :telemetry.span/3 (#480)
* feat: Uses `:telemetry.span/3` * fix: Preserves more of the "stop" metadata * fix: Add more of the original context Co-authored-by: Isaac Sanders <[email protected]> Co-authored-by: Jonatan Männchen <[email protected]>
1 parent 7d24f24 commit 3b06c3c

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

lib/quantum/executor.ex

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,11 @@ defmodule Quantum.Executor do
9393
{"Execute started for job", node: Node.self(), name: job_name}
9494
end)
9595

96-
# Note: we are intentionally mimicking the ":telemetry.span" here to keep current functionality
97-
start_monotonic_time = :erlang.monotonic_time()
98-
99-
:telemetry.execute([:quantum, :job, :start], %{system_time: start_monotonic_time}, %{
100-
job: job,
101-
node: node,
102-
scheduler: scheduler
103-
})
104-
10596
try do
106-
execute_task(task)
97+
:telemetry.span([:quantum, :job], %{job: job, node: node, scheduler: scheduler}, fn ->
98+
result = execute_task(task)
99+
{result, %{job: job, node: node, scheduler: scheduler, result: result}}
100+
end)
107101
catch
108102
type, value ->
109103
debug_logging &&
@@ -115,31 +109,12 @@ defmodule Quantum.Executor do
115109
end)
116110

117111
log_exception(type, value, __STACKTRACE__)
118-
119-
duration = :erlang.monotonic_time() - start_monotonic_time
120-
121-
:telemetry.execute([:quantum, :job, :exception], %{duration: duration}, %{
122-
job: job,
123-
node: node,
124-
reason: value,
125-
stacktrace: __STACKTRACE__,
126-
scheduler: scheduler
127-
})
128112
else
129113
result ->
130114
debug_logging &&
131115
Logger.debug(fn ->
132116
{"Execution ended for job", node: Node.self(), name: job_name, result: result}
133117
end)
134-
135-
duration = :erlang.monotonic_time() - start_monotonic_time
136-
137-
:telemetry.execute([:quantum, :job, :stop], %{duration: duration}, %{
138-
job: job,
139-
node: node,
140-
scheduler: scheduler,
141-
result: result
142-
})
143118
end
144119

145120
:ok

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ defmodule Quantum.Mixfile do
9999
[
100100
{:crontab, "~> 1.1"},
101101
{:gen_stage, "~> 0.14 or ~> 1.0"},
102-
{:telemetry, "~> 0.4"},
102+
{:telemetry, ">= 0.4.3 and < 1.0.0"},
103103
{:tzdata, "~> 1.0", only: [:dev, :test]},
104104
{:ex_doc, ">= 0.0.0", only: [:dev, :docs], runtime: false},
105105
{:excoveralls, "~> 0.5", only: [:test], runtime: false},

test/quantum/executor_test.exs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,40 @@ defmodule Quantum.ExecutorTest do
2323
def handle_event(
2424
[:quantum, :job, :start],
2525
%{system_time: _system_time} = _measurements,
26-
%{job: %Job{name: job_name}, node: _node, scheduler: _scheduler} = _metadata,
26+
%{
27+
job: %Job{name: job_name},
28+
node: _node,
29+
scheduler: _scheduler,
30+
telemetry_span_context: telemetry_span_context
31+
} = _metadata,
2732
%{parent_thread: parent_thread, test_id: test_id}
2833
) do
29-
send(parent_thread, %{test_id: test_id, job_name: job_name, type: :start})
34+
send(parent_thread, %{
35+
test_id: test_id,
36+
job_name: job_name,
37+
type: :start,
38+
telemetry_span_context: telemetry_span_context
39+
})
3040
end
3141

3242
def handle_event(
3343
[:quantum, :job, :stop],
3444
%{duration: _duration} = _measurements,
35-
%{job: %Job{name: job_name}, node: _node, scheduler: _scheduler, result: _result} =
36-
_metadata,
45+
%{
46+
job: %Job{name: job_name},
47+
node: _node,
48+
scheduler: _scheduler,
49+
result: _result,
50+
telemetry_span_context: telemetry_span_context
51+
} = _metadata,
3752
%{parent_thread: parent_thread, test_id: test_id}
3853
) do
39-
send(parent_thread, %{test_id: test_id, job_name: job_name, type: :stop})
54+
send(parent_thread, %{
55+
test_id: test_id,
56+
job_name: job_name,
57+
telemetry_span_context: telemetry_span_context,
58+
type: :stop
59+
})
4060
end
4161

4262
def handle_event(
@@ -46,15 +66,19 @@ defmodule Quantum.ExecutorTest do
4666
job: %Job{name: job_name},
4767
node: _node,
4868
scheduler: _scheduler,
69+
kind: kind,
4970
reason: reason,
50-
stacktrace: stacktrace
71+
stacktrace: stacktrace,
72+
telemetry_span_context: telemetry_span_context
5173
} = _metadata,
5274
%{parent_thread: parent_thread, test_id: test_id}
5375
) do
5476
send(parent_thread, %{
5577
test_id: test_id,
5678
job_name: job_name,
79+
telemetry_span_context: telemetry_span_context,
5780
type: :exception,
81+
kind: kind,
5882
reason: reason,
5983
stacktrace: stacktrace
6084
})
@@ -316,10 +340,13 @@ defmodule Quantum.ExecutorTest do
316340
assert_receive %{
317341
test_id: ^test_id,
318342
type: :exception,
343+
kind: :error,
319344
reason: %RuntimeError{message: "failed"},
320345
stacktrace: [
321346
{Quantum.ExecutorTest, _, _, _},
322347
{Quantum.Executor, _, _, _},
348+
{:telemetry, _, _, _},
349+
{Quantum.Executor, _, _, _},
323350
{Task.Supervised, _, _, _},
324351
{Task.Supervised, _, _, _},
325352
{:proc_lib, _, _, _}
@@ -366,10 +393,13 @@ defmodule Quantum.ExecutorTest do
366393
assert_receive %{
367394
test_id: ^test_id,
368395
type: :exception,
396+
kind: :error,
369397
reason: %RuntimeError{message: "failed"},
370398
stacktrace: [
371399
{Quantum.ExecutorTest, _, _, _},
372400
{Quantum.Executor, _, _, _},
401+
{:telemetry, _, _, _},
402+
{Quantum.Executor, _, _, _},
373403
{Task.Supervised, _, _, _},
374404
{Task.Supervised, _, _, _},
375405
{:proc_lib, _, _, _}
@@ -415,10 +445,13 @@ defmodule Quantum.ExecutorTest do
415445
assert_receive %{
416446
test_id: ^test_id,
417447
type: :exception,
448+
kind: :exit,
418449
reason: :failure,
419450
stacktrace: [
420451
{Quantum.ExecutorTest, _, _, _},
421452
{Quantum.Executor, _, _, _},
453+
{:telemetry, _, _, _},
454+
{Quantum.Executor, _, _, _},
422455
{Task.Supervised, _, _, _},
423456
{Task.Supervised, _, _, _},
424457
{:proc_lib, _, _, _}
@@ -468,10 +501,13 @@ defmodule Quantum.ExecutorTest do
468501
assert_receive %{
469502
test_id: ^test_id,
470503
type: :exception,
504+
kind: :throw,
471505
reason: ^ref,
472506
stacktrace: [
473507
{Quantum.ExecutorTest, _, _, _},
474508
{Quantum.Executor, _, _, _},
509+
{:telemetry, _, _, _},
510+
{Quantum.Executor, _, _, _},
475511
{Task.Supervised, _, _, _},
476512
{Task.Supervised, _, _, _},
477513
{:proc_lib, _, _, _}

0 commit comments

Comments
 (0)