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

Show worker/supervisor ids in app supervision graph #449

Merged
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
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
Loading