Skip to content

Commit

Permalink
Show worker/supervisor ids in app supervision graph (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiaanEngelbrecht authored Jun 24, 2024
1 parent 94db44d commit 467bb05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
14 changes: 11 additions & 3 deletions lib/kino/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ defmodule Kino.Process do
"#{idx}[(\"`#{module_or_atom_to_string(id)}\n**_#{protection}_**`\")]:::ets"
end

defp graph_node(%{idx: idx, pid: pid, type: type}) do
defp graph_node(%{idx: idx, id: id, pid: pid, type: type}) do
type =
if idx == 0 do
:root
Expand All @@ -763,8 +763,16 @@ defmodule Kino.Process do
case process_info(pid, :registered_name) do
{:registered_name, []} ->
case get_label(pid) do
:undefined -> inspect(pid)
process_label -> format_for_mermaid_graph_node(pid, process_label)
:undefined ->
if idx == 0 do
inspect(pid)
else
# Use worker/supervisor id as label
format_for_mermaid_graph_node(pid, id)
end

process_label ->
format_for_mermaid_graph_node(pid, process_label)
end

{:registered_name, name} ->
Expand Down
35 changes: 28 additions & 7 deletions test/kino/process_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ defmodule Kino.ProcessTest do
] = Supervisor.which_children(pid)

content = pid |> Kino.Process.sup_tree(render_ets_tables: true) |> mermaid()
agent_pid_text = :erlang.pid_to_list(agent_pid) |> List.to_string()

assert content =~ "0(supervisor_parent):::root ---> 1(ets_owner):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(ets_heir):::worker"
assert content =~ "0(supervisor_parent):::root ---> 3(#{inspect(agent_pid)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 3(\"Agent<br/>#{agent_pid_text}\"):::worker"

assert content =~
"1(ets_owner):::worker -- owner --> 4[(\"`test_ets_table\n**_protected_**`\")]:::ets"
Expand All @@ -33,7 +37,9 @@ defmodule Kino.ProcessTest do
content = :supervisor_parent |> Kino.Process.sup_tree(render_ets_tables: true) |> mermaid()
assert content =~ "0(supervisor_parent):::root ---> 1(ets_owner):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(ets_heir):::worker"
assert content =~ "0(supervisor_parent):::root ---> 3(#{inspect(agent_pid)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 3(\"Agent<br/>#{agent_pid_text}\"):::worker"

assert content =~
"1(ets_owner):::worker -- owner --> 4[(\"`test_ets_table\n**_protected_**`\")]:::ets"
Expand All @@ -52,15 +58,22 @@ defmodule Kino.ProcessTest do
] = Supervisor.which_children(pid)

content = pid |> Kino.Process.sup_tree() |> mermaid()
agent_pid_text = :erlang.pid_to_list(agent_pid) |> List.to_string()
assert content =~ "0(supervisor_parent):::root ---> 1(ets_owner):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(ets_heir):::worker"
assert content =~ "0(supervisor_parent):::root ---> 3(#{inspect(agent_pid)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 3(\"Agent<br/>#{agent_pid_text}\"):::worker"

refute content =~ ":::ets"

content = :supervisor_parent |> Kino.Process.sup_tree() |> mermaid()
assert content =~ "0(supervisor_parent):::root ---> 1(ets_owner):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(ets_heir):::worker"
assert content =~ "0(supervisor_parent):::root ---> 3(#{inspect(agent_pid)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 3(\"Agent<br/>#{agent_pid_text}\"):::worker"

refute content =~ ":::ets"
end

Expand All @@ -81,14 +94,19 @@ defmodule Kino.ProcessTest do
})

[_, {_, agent, _, _}] = Supervisor.which_children(pid)
agent_pid_text = :erlang.pid_to_list(agent) |> List.to_string()

content = Kino.Process.sup_tree(pid) |> mermaid()
assert content =~ "0(supervisor_parent):::root ---> 1(agent_child):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(#{inspect(agent)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 2(\"Agent<br/>#{agent_pid_text}\"):::worker"

content = Kino.Process.sup_tree(:supervisor_parent) |> mermaid()
assert content =~ "0(supervisor_parent):::root ---> 1(agent_child):::worker"
assert content =~ "0(supervisor_parent):::root ---> 2(#{inspect(agent)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 2(\"Agent<br/>#{agent_pid_text}\"):::worker"
end

test "shows supervision tree with children alongside non-started children" do
Expand All @@ -108,10 +126,13 @@ defmodule Kino.ProcessTest do
})

[{:not_started, :undefined, _, _}, {_, agent, _, _}] = Supervisor.which_children(pid)
agent_pid_text = :erlang.pid_to_list(agent) |> List.to_string()

content = Kino.Process.sup_tree(pid) |> mermaid()
assert content =~ "0(supervisor_parent):::root ---> 1(id: :not_started):::notstarted"
assert content =~ "0(supervisor_parent):::root ---> 2(#{inspect(agent)}):::worker"

assert content =~
"0(supervisor_parent):::root ---> 2(\"Agent<br/>#{agent_pid_text}\"):::worker"
end

# TODO: remove once we require Elixir v1.17.0
Expand Down

0 comments on commit 467bb05

Please sign in to comment.