From 8f85e6edfabbb456ce2e73774b6d588efe4808cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20van=20Diemen?= Date: Fri, 12 Jul 2024 14:09:37 +0200 Subject: [PATCH] Add set_current_span and monitor functions to Tracing `Tracing.Monitor.monitor(Tracing.current_span())` is a bit on the long side for something that should 99% of the times do the same thing. We can add an extra function on `Tracing` and use the current span as fallback if nothing is given. --- lib/tracing.ex | 7 ++++++- lib/tracing/monitor.ex | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/tracing.ex b/lib/tracing.ex index fdddcd3..daed49b 100644 --- a/lib/tracing.ex +++ b/lib/tracing.ex @@ -112,6 +112,11 @@ defmodule Tracing do |> :otel_propagator_text_map.extract() end + @doc """ + Helper function to start Tracing.Monitor + """ + def monitor(span \\ nil), do: Tracing.Monitor.monitor(span) + @doc """ Use this function when defining an anonymous function that will be used in _another process_. This function will ensure that the span in the parent process is the parent span of the span in the spawned process. @@ -154,7 +159,7 @@ defmodule Tracing do parent_span = OpenTelemetry.Tracer.current_span_ctx() if unquote(opts)[:monitor] do - Tracing.Monitor.monitor(parent_span) + Tracing.monitor(parent_span) end fn unquote_splicing(fun_args) -> diff --git a/lib/tracing/monitor.ex b/lib/tracing/monitor.ex index 0fa91ea..a770c85 100644 --- a/lib/tracing/monitor.ex +++ b/lib/tracing/monitor.ex @@ -46,11 +46,16 @@ defmodule Tracing.Monitor do {:noreply, state} end - def monitor(span_ctx) do + @doc """ + Start monitoring the given span. If the span is not set, it defaults to `Tracing.current_span()` + """ + def monitor(span \\ nil) do + span = span || Tracing.current_span() + if Application.fetch_env!(:opentelemetry, :processors) != [] do # monitor first, because the monitor is necessary to clean the ets table. :ok = GenServer.call(__MODULE__, {:monitor, self()}) - true = :ets.insert(__MODULE__, {self(), span_ctx}) + true = :ets.insert(__MODULE__, {self(), span}) end end end