Repository: quantum-elixir/quantum-core
Pull request #480 (2021-06-17 20:05:57)

feat: Use `:telemetry.span/3`
There doesn't seem to be a clear reason to not use the proper span function.

I figured I would do the legwork here.

isaacsanders: There seems to be an issue with the build process.
coveralls: ## Pull Request Test Coverage Report for [Build 6d46dc69994cca31ff29c7b9d3f107c7b8e9811b-PR-480](https://coveralls.io/builds/40701570)

* **2** of **2**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.5%**) to **85.994%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/40701570/badge)](https://coveralls.io/builds/40701570) |
| :-- | --: |
| Change from base [Build 7d24f247e4b000a4d050c157a30b01cc8cb1864d](https://coveralls.io/builds/40701483): |  -0.5% |
| Covered Lines: | 307 |
| Relevant Lines: | 357 |

---
##### 💛  - [Coveralls](https://coveralls.io)

maennchen: Thanks a lot @isaacsanders :heart: 

Patch:
diff --git a/lib/quantum/executor.ex b/lib/quantum/executor.ex
index f73c309..de435ce 100644
--- a/lib/quantum/executor.ex
+++ b/lib/quantum/executor.ex
@@ -93,17 +93,11 @@ defmodule Quantum.Executor do
           {"Execute started for job", node: Node.self(), name: job_name}
         end)
 
-      # Note: we are intentionally mimicking the ":telemetry.span" here to keep current functionality
-      start_monotonic_time = :erlang.monotonic_time()
-
-      :telemetry.execute([:quantum, :job, :start], %{system_time: start_monotonic_time}, %{
-        job: job,
-        node: node,
-        scheduler: scheduler
-      })
-
       try do
-        execute_task(task)
+        :telemetry.span([:quantum, :job], %{job: job, node: node, scheduler: scheduler}, fn ->
+          result = execute_task(task)
+          {result, %{job: job, node: node, scheduler: scheduler, result: result}}
+        end)
       catch
         type, value ->
           debug_logging &&
@@ -115,31 +109,12 @@ defmodule Quantum.Executor do
             end)
 
           log_exception(type, value, __STACKTRACE__)
-
-          duration = :erlang.monotonic_time() - start_monotonic_time
-
-          :telemetry.execute([:quantum, :job, :exception], %{duration: duration}, %{
-            job: job,
-            node: node,
-            reason: value,
-            stacktrace: __STACKTRACE__,
-            scheduler: scheduler
-          })
       else
         result ->
           debug_logging &&
             Logger.debug(fn ->
               {"Execution ended for job", node: Node.self(), name: job_name, result: result}
             end)
-
-          duration = :erlang.monotonic_time() - start_monotonic_time
-
-          :telemetry.execute([:quantum, :job, :stop], %{duration: duration}, %{
-            job: job,
-            node: node,
-            scheduler: scheduler,
-            result: result
-          })
       end
 
       :ok
diff --git a/mix.exs b/mix.exs
index f00f033..9e7bd15 100644
--- a/mix.exs
+++ b/mix.exs
@@ -99,7 +99,7 @@ defmodule Quantum.Mixfile do
     [
       {:crontab, "~> 1.1"},
       {:gen_stage, "~> 0.14 or ~> 1.0"},
-      {:telemetry, "~> 0.4"},
+      {:telemetry, ">= 0.4.3 and < 1.0.0"},
       {:tzdata, "~> 1.0", only: [:dev, :test]},
       {:ex_doc, ">= 0.0.0", only: [:dev, :docs], runtime: false},
       {:excoveralls, "~> 0.5", only: [:test], runtime: false},


Test patch:
diff --git a/test/quantum/executor_test.exs b/test/quantum/executor_test.exs
index b72b309..4f7a5a6 100644
--- a/test/quantum/executor_test.exs
+++ b/test/quantum/executor_test.exs
@@ -23,20 +23,40 @@ defmodule Quantum.ExecutorTest do
     def handle_event(
           [:quantum, :job, :start],
           %{system_time: _system_time} = _measurements,
-          %{job: %Job{name: job_name}, node: _node, scheduler: _scheduler} = _metadata,
+          %{
+            job: %Job{name: job_name},
+            node: _node,
+            scheduler: _scheduler,
+            telemetry_span_context: telemetry_span_context
+          } = _metadata,
           %{parent_thread: parent_thread, test_id: test_id}
         ) do
-      send(parent_thread, %{test_id: test_id, job_name: job_name, type: :start})
+      send(parent_thread, %{
+        test_id: test_id,
+        job_name: job_name,
+        type: :start,
+        telemetry_span_context: telemetry_span_context
+      })
     end
 
     def handle_event(
           [:quantum, :job, :stop],
           %{duration: _duration} = _measurements,
-          %{job: %Job{name: job_name}, node: _node, scheduler: _scheduler, result: _result} =
-            _metadata,
+          %{
+            job: %Job{name: job_name},
+            node: _node,
+            scheduler: _scheduler,
+            result: _result,
+            telemetry_span_context: telemetry_span_context
+          } = _metadata,
           %{parent_thread: parent_thread, test_id: test_id}
         ) do
-      send(parent_thread, %{test_id: test_id, job_name: job_name, type: :stop})
+      send(parent_thread, %{
+        test_id: test_id,
+        job_name: job_name,
+        telemetry_span_context: telemetry_span_context,
+        type: :stop
+      })
     end
 
     def handle_event(
@@ -46,15 +66,19 @@ defmodule Quantum.ExecutorTest do
             job: %Job{name: job_name},
             node: _node,
             scheduler: _scheduler,
+            kind: kind,
             reason: reason,
-            stacktrace: stacktrace
+            stacktrace: stacktrace,
+            telemetry_span_context: telemetry_span_context
           } = _metadata,
           %{parent_thread: parent_thread, test_id: test_id}
         ) do
       send(parent_thread, %{
         test_id: test_id,
         job_name: job_name,
+        telemetry_span_context: telemetry_span_context,
         type: :exception,
+        kind: kind,
         reason: reason,
         stacktrace: stacktrace
       })
@@ -316,10 +340,13 @@ defmodule Quantum.ExecutorTest do
       assert_receive %{
                        test_id: ^test_id,
                        type: :exception,
+                       kind: :error,
                        reason: %RuntimeError{message: "failed"},
                        stacktrace: [
                          {Quantum.ExecutorTest, _, _, _},
                          {Quantum.Executor, _, _, _},
+                         {:telemetry, _, _, _},
+                         {Quantum.Executor, _, _, _},
                          {Task.Supervised, _, _, _},
                          {Task.Supervised, _, _, _},
                          {:proc_lib, _, _, _}
@@ -366,10 +393,13 @@ defmodule Quantum.ExecutorTest do
       assert_receive %{
                        test_id: ^test_id,
                        type: :exception,
+                       kind: :error,
                        reason: %RuntimeError{message: "failed"},
                        stacktrace: [
                          {Quantum.ExecutorTest, _, _, _},
                          {Quantum.Executor, _, _, _},
+                         {:telemetry, _, _, _},
+                         {Quantum.Executor, _, _, _},
                          {Task.Supervised, _, _, _},
                          {Task.Supervised, _, _, _},
                          {:proc_lib, _, _, _}
@@ -415,10 +445,13 @@ defmodule Quantum.ExecutorTest do
       assert_receive %{
                        test_id: ^test_id,
                        type: :exception,
+                       kind: :exit,
                        reason: :failure,
                        stacktrace: [
                          {Quantum.ExecutorTest, _, _, _},
                          {Quantum.Executor, _, _, _},
+                         {:telemetry, _, _, _},
+                         {Quantum.Executor, _, _, _},
                          {Task.Supervised, _, _, _},
                          {Task.Supervised, _, _, _},
                          {:proc_lib, _, _, _}
@@ -468,10 +501,13 @@ defmodule Quantum.ExecutorTest do
       assert_receive %{
                        test_id: ^test_id,
                        type: :exception,
+                       kind: :throw,
                        reason: ^ref,
                        stacktrace: [
                          {Quantum.ExecutorTest, _, _, _},
                          {Quantum.Executor, _, _, _},
+                         {:telemetry, _, _, _},
+                         {Quantum.Executor, _, _, _},
                          {Task.Supervised, _, _, _},
                          {Task.Supervised, _, _, _},
                          {:proc_lib, _, _, _}