-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move OpenTelemetryMonitor to Tracing.Monitor
OpenTelemetryMonitor used to be a fork with a new module named OpenTelemetryMonitor. This module was proposed as a PR but was not merged. Dissolving the monitor into Tracing opens the way to be published.
- Loading branch information
1 parent
430d7af
commit 70fe943
Showing
7 changed files
with
65 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Tracing.Monitor do | ||
use GenServer | ||
|
||
def start_link(_arg) do | ||
GenServer.start_link(__MODULE__, nil, name: __MODULE__) | ||
end | ||
|
||
def init(nil) do | ||
_table_id = :ets.new(__MODULE__, [:bag, :public, {:write_concurrency, true}, :named_table]) | ||
{:ok, nil} | ||
end | ||
|
||
def handle_call({:monitor, pid}, _from, state) do | ||
Process.monitor(pid) | ||
{:reply, :ok, state} | ||
end | ||
|
||
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() | ||
end) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
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() | ||
end) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
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() | ||
end) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
def monitor(span_ctx) do | ||
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}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defmodule Tracing.MonitorTest do | ||
use ExUnit.Case | ||
doctest Tracing.Monitor | ||
end |