Skip to content

Commit 3385a98

Browse files
authored
Set some process labels (#764)
This makes it easier to understand the process tree in tools like Observer and Phoenix LiveDashboard and gives clearer errors for crashes.
1 parent e4f7942 commit 3385a98

5 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/postgrex/protocol.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ defmodule Postgrex.Protocol do
229229
with {:ok, database} <- fetch_database(opts),
230230
status = %{status | types_key: if(types_mod, do: {host, port, database})},
231231
{:ok, ret} <- connect_and_handshake(host, port, sock_opts, timeout, s, status) do
232+
Postgrex.Utils.set_label({Postgrex.Protocol, database})
232233
{:ok, ret}
233234
else
234235
{:error, err} ->
@@ -1907,7 +1908,12 @@ defmodule Postgrex.Protocol do
19071908
end)
19081909

19091910
ref = make_ref()
1910-
{_, mon} = spawn_monitor(fn -> reload_init(s, status, oids, ref, buffer) end)
1911+
1912+
{_, mon} =
1913+
spawn_monitor(fn ->
1914+
Postgrex.Utils.set_label({Postgrex.Protocol, :fetch_type_info})
1915+
reload_init(s, status, oids, ref, buffer)
1916+
end)
19111917

19121918
receive do
19131919
{:DOWN, ^mon, _, _, {^ref, s, buffer}} ->

lib/postgrex/replication_connection.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ defmodule Postgrex.ReplicationConnection do
466466
@doc false
467467
@impl :gen_statem
468468
def init({mod, arg, opts}) do
469+
Postgrex.Utils.set_label({Postgrex.ReplicationConnection, mod})
470+
469471
case mod.init(arg) do
470472
{:ok, mod_state} ->
471473
opts =

lib/postgrex/simple_connection.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ defmodule Postgrex.SimpleConnection do
299299
@doc false
300300
@impl :gen_statem
301301
def init({mod, args, opts}) do
302+
Postgrex.Utils.set_label({Postgrex.SimpleConnection, mod})
303+
302304
case mod.init(args) do
303305
{:ok, mod_state} ->
304306
idle_timeout = opts[:idle_timeout]

lib/postgrex/type_server.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ defmodule Postgrex.TypeServer do
5757
## Callbacks
5858

5959
def init({module, starter}) do
60+
Postgrex.Utils.set_label({Postgrex.TypeServer, module})
6061
_ = Process.flag(:trap_exit, true)
6162
Process.link(starter)
6263

lib/postgrex/utils.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ defmodule Postgrex.Utils do
170170

171171
## Helpers
172172

173+
@doc """
174+
Set a process label if `Process.set_label/1` is available.
175+
"""
176+
def set_label(label) do
177+
if function_exported?(Process, :set_label, 1) do
178+
apply(Process, :set_label, [label])
179+
else
180+
:ok
181+
end
182+
end
183+
173184
defp parse_version_bit(bit) do
174185
{int, _} = Integer.parse(bit)
175186
int

0 commit comments

Comments
 (0)