Skip to content

Commit

Permalink
fix tracking spawned processes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaszk committed Jan 2, 2024
1 parent 536be83 commit 518644f
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/new_relic/transaction/erlang_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ defmodule NewRelic.Transaction.ErlangTrace do

import NewRelic.ConditionalCompile

# Before elixir 1.15, tasks were spawned with :proc_lib,
# but starting with elixir 1.15 tasks are spawned with Task.Supervised.
# See https://github.com/elixir-lang/elixir/commit/ecdf68438160928f01769b3ed76e184ad451c9fe
before_elixir_version("1.15.0", @spawn_module(:proc_lib))
after_elixir_version("1.15.0", @spawn_module(Task.Supervised))

def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
Expand Down Expand Up @@ -55,18 +49,20 @@ defmodule NewRelic.Transaction.ErlangTrace do
# Trace messages

def handle_info(
{:trace_ts, source, :return_from, {@spawn_module, :spawn_link, _}, pid, timestamp},
{:trace_ts, source, :return_from, {module, :spawn_link, _}, pid, timestamp},
state
) do
)
when module in [:proc_lib, Task.Supervised] do
Transaction.Reporter.track_spawn(source, pid, NewRelic.Util.time_to_ms(timestamp))
overload_protection(state.overload)
{:noreply, state}
end

def handle_info(
{:trace_ts, source, :return_from, {@spawn_module, :start_link, _}, {:ok, pid}, timestamp},
{:trace_ts, source, :return_from, {module, :start_link, _}, {:ok, pid}, timestamp},
state
) do
)
when module in [:proc_lib, Task.Supervised] do
Transaction.Reporter.track_spawn(source, pid, NewRelic.Util.time_to_ms(timestamp))
overload_protection(state.overload)
{:noreply, state}
Expand Down Expand Up @@ -98,8 +94,20 @@ defmodule NewRelic.Transaction.ErlangTrace do
end

defp trace_proc_lib_spawn_link do
:erlang.trace_pattern({@spawn_module, :spawn_link, :_}, [{:_, [], [{:return_trace}]}], [])
:erlang.trace_pattern({@spawn_module, :start_link, :_}, [{:_, [], [{:return_trace}]}], [])
:erlang.trace_pattern({:proc_lib, :spawn_link, :_}, [{:_, [], [{:return_trace}]}], [])
:erlang.trace_pattern({:proc_lib, :start_link, :_}, [{:_, [], [{:return_trace}]}], [])

# Starting with elixir 1.15 tasks are spawned with Task.Supervised.
# See https://github.com/elixir-lang/elixir/commit/ecdf68438160928f01769b3ed76e184ad451c9fe
after_elixir_version(
"1.15.0",
:erlang.trace_pattern({Task.Supervised, :spawn_link, :_}, [{:_, [], [{:return_trace}]}], [])
)

after_elixir_version(
"1.15.0",
:erlang.trace_pattern({Task.Supervised, :start_link, :_}, [{:_, [], [{:return_trace}]}], [])
)
end

defp overload_protection(%{backoff: backoff} = overload) do
Expand Down

0 comments on commit 518644f

Please sign in to comment.