From f583165651982ea7d4af4665ec82bc66095d567d 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 1/3] 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 From 3feed776c62ab09f738ff0bd0b31fb20b01d2c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20van=20Diemen?= Date: Fri, 12 Jul 2024 14:09:56 +0200 Subject: [PATCH 2/3] Add set_current_span function We have the option to get the current span but not to set it, we can also use the current_span function a bit more throughout Tracing. --- lib/tracing.ex | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/tracing.ex b/lib/tracing.ex index daed49b..c707b1d 100644 --- a/lib/tracing.ex +++ b/lib/tracing.ex @@ -29,6 +29,7 @@ defmodule Tracing do defmacro __using__(_) do quote do require Tracing + require OpenTelemetry.Tracer use Tracing.Decorator end end @@ -38,7 +39,7 @@ defmodule Tracing do """ @spec trace_id() :: String.t() def trace_id do - case OpenTelemetry.Tracer.current_span_ctx() do + case current_span() do :undefined -> "span_not_found" @@ -57,6 +58,13 @@ defmodule Tracing do @spec current_span() :: any() def current_span, do: OpenTelemetry.Tracer.current_span_ctx() + @doc """ + Sets the current span to current_span() or what is provided + """ + def set_current_span(span \\ current_span()) do + OpenTelemetry.Tracer.set_current_span(span) + end + @doc """ Simple wrapper around `OpenTelemetry.Span.set_attributes/2` """ @@ -71,7 +79,8 @@ defmodule Tracing do Simple wrapper around `OpenTelemetry.Span.add_event/2` """ def add_event(name, attrs) do - OpenTelemetry.Tracer.add_event( + OpenTelemetry.Span.add_event( + current_span(), name, attrs ) @@ -99,18 +108,12 @@ defmodule Tracing do @doc """ Helper function for propogating opentelemetry """ - def inject(keywords \\ []) do - keywords - |> :otel_propagator_text_map.inject() - end + def inject(keywords \\ []), do: :otel_propagator_text_map.inject(keywords) @doc """ Helper function for propogating opentelemetry """ - def extract(keywords) do - keywords - |> :otel_propagator_text_map.extract() - end + def extract(keywords), do: :otel_propagator_text_map.extract(keywords) @doc """ Helper function to start Tracing.Monitor From 199d42cb34a47803daab522ff1c824f590a23eb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20van=20Diemen?= Date: Fri, 12 Jul 2024 14:14:56 +0200 Subject: [PATCH 3/3] Use Tracing functions throughout modules more Tracing has a few functions that cover the grounds of other functions that are used. These can be altered to use Tracings own functions. --- lib/tracing.ex | 7 +++++++ lib/tracing/monitor.ex | 14 +++++++------- lib/tracing/oban_telemetry.ex | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/tracing.ex b/lib/tracing.ex index c707b1d..344919d 100644 --- a/lib/tracing.ex +++ b/lib/tracing.ex @@ -105,6 +105,13 @@ defmodule Tracing do |> extract() end + @doc """ + Simple wrapper around `OpenTelemetry.Tracer.end_span/0` + """ + def end_span() do + OpenTelemetry.Tracer.end_span() + end + @doc """ Helper function for propogating opentelemetry """ diff --git a/lib/tracing/monitor.ex b/lib/tracing/monitor.ex index a770c85..775f9a6 100644 --- a/lib/tracing/monitor.ex +++ b/lib/tracing/monitor.ex @@ -18,8 +18,8 @@ defmodule Tracing.Monitor do def handle_info({:DOWN, _ref, :process, pid, :normal}, state) do :ets.take(__MODULE__, pid) |> Enum.each(fn {_pid, ctx} -> - _span_ctx = OpenTelemetry.Tracer.set_current_span(ctx) - _ = OpenTelemetry.Tracer.end_span() + _span_ctx = Tracing.set_current_span(ctx) + _ = Tracing.end_span() end) {:noreply, state} @@ -28,8 +28,8 @@ defmodule Tracing.Monitor do def handle_info({:DOWN, _ref, :process, pid, {:shutdown, _}}, state) do :ets.take(__MODULE__, pid) |> Enum.each(fn {_pid, ctx} -> - _span_ctx = OpenTelemetry.Tracer.set_current_span(ctx) - _ = OpenTelemetry.Tracer.end_span() + _span_ctx = Tracing.set_current_span(ctx) + _ = Tracing.end_span() end) {:noreply, state} @@ -38,9 +38,9 @@ defmodule Tracing.Monitor do def handle_info({:DOWN, _ref, :process, pid, reason}, state) do :ets.take(__MODULE__, pid) |> Enum.each(fn {_pid, ctx} -> - _span_ctx = OpenTelemetry.Tracer.set_current_span(ctx) - _ = OpenTelemetry.Tracer.add_event("Process died", [{"reason", inspect(reason)}]) - _ = OpenTelemetry.Tracer.end_span() + Tracing.set_current_span(ctx) + Tracing.add_event("Process died", [{"reason", inspect(reason)}]) + Tracing.end_span() end) {:noreply, state} diff --git a/lib/tracing/oban_telemetry.ex b/lib/tracing/oban_telemetry.ex index dbcf883..f682944 100644 --- a/lib/tracing/oban_telemetry.ex +++ b/lib/tracing/oban_telemetry.ex @@ -69,7 +69,7 @@ defmodule Tracing.ObanTelemetry do }) |> Span.set_attributes(attributes) - Tracing.Monitor.monitor(OpenTelemetry.Tracer.current_span_ctx()) + Tracing.monitor() end def handle_event([:oban, :job, :stop], _, meta, _) do @@ -82,7 +82,7 @@ defmodule Tracing.ObanTelemetry do %{kind: kind, reason: reason, stacktrace: stacktrace} = meta, _ ) do - case OpenTelemetry.Tracer.current_span_ctx() do + case Tracing.current_span() do :undefined -> nil