Skip to content

Commit

Permalink
Add set_current_span and monitor functions to Tracing
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
MvanDiemen committed Jul 12, 2024
1 parent 121eef4 commit 8f85e6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/tracing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) ->
Expand Down
9 changes: 7 additions & 2 deletions lib/tracing/monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8f85e6e

Please sign in to comment.