Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set current span and monitor functions #7

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
MvanDiemen committed Jul 12, 2024
commit f583165651982ea7d4af4665ec82bc66095d567d
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