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 all commits
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
37 changes: 26 additions & 11 deletions lib/tracing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Tracing do
defmacro __using__(_) do
quote do
require Tracing
require OpenTelemetry.Tracer
use Tracing.Decorator
end
end
Expand All @@ -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"

Expand All @@ -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`
"""
Expand All @@ -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
)
Expand All @@ -97,20 +106,26 @@ defmodule Tracing do
end

@doc """
Helper function for propogating opentelemetry
Simple wrapper around `OpenTelemetry.Tracer.end_span/0`
"""
def inject(keywords \\ []) do
keywords
|> :otel_propagator_text_map.inject()
def end_span() do
OpenTelemetry.Tracer.end_span()
end

@doc """
Helper function for propogating opentelemetry
"""
def extract(keywords) do
keywords
|> :otel_propagator_text_map.extract()
end
def inject(keywords \\ []), do: :otel_propagator_text_map.inject(keywords)

@doc """
Helper function for propogating opentelemetry
"""
def extract(keywords), do: :otel_propagator_text_map.extract(keywords)

@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_.
Expand Down Expand Up @@ -154,7 +169,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
23 changes: 14 additions & 9 deletions lib/tracing/monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -38,19 +38,24 @@ 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}
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
4 changes: 2 additions & 2 deletions lib/tracing/oban_telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Loading