Astarno

Astarno

Trace messages arriving in a GenServer's mailbox

Hi there,

Is there a way to trace, in any way, when a message arrives in the mailbox of a GenServer? Note, I am not refering to the moment when a message is actually handled (taken out of the mailbox). I am specifically refering to the moment when a message arrives in the mailbox (and might sit there for quite some time before it is actually handled).

I’ve looked at the dbg module, specifically into tracing receive in a specified Process. However, I am not sure this is the correct approach and I only end up with either very generic or totally unreadable trace messages.

For example:

{:"$gen_cast", {:push, :hello}}
{#Reference<0.4094062663.2986868739.152886>, {:ok, {1678, 384779, 246000}}}
{#Reference<0.4094062663.2986868740.153344>, {:ok, <<4, 0, 0, 0, 1, 127, 0, 0, 1, 0, 0, 0, 1, 65, 114, 110, 111, 0>>}}     
{[:alias | #Reference<0.4094062663.2986934276.153347>], {:ok, 52087622}}
{[:alias | #Reference<0.4094062663.2986934276.153348>], {:error, :no_socket, #PID<0.271.0>}}
{#Reference<0.4094062663.2986868740.153350>, {:ok, <<4, 0, 0, 0, 1, 127, 0, 0, 1, 0, 0, 0, 1, 65, 114, 110, 111, 0>>}}   

In the end, whenever a message arrives in the mailbox of a GenServer I would like to be able to log it somewhere, or even send an API call somewhere. As such, I would definetly need information about the message that arrived itself (i.e. content), as well as where it arrived (i.e. PID) and if possible also the type (i.e. cast/call).

It might be that this is impossible due to the nature of an actor (Process), in that case it would be nice to have this confirmed such that I do not search needlessly for a non-existent solution.

module

Most Liked

voltone

voltone

Those really are the messages as they arrive to the GenServer’s mailbox. Keep in mind that a GenServer has its own receive loop that will handle certain messages internally, and will interpret specific message patterns as call or cast. Only messages you handle in the handle_info/2 are going to appear exactly as-is in the mailbox.

Also, your GenServer may call other GenServers, in which case the response will be delivered to the processes mailbox while that call is blocking your process. It looks like just a function call from your code, but the underlying messaging will show up in the trace.

Something similar can happen from e.g. a library function that happens to send a message to another process and then blocks on a selective receive to get an answer.

TL;DR: the mailbox activity shows the raw messages as they arrive, which is going to include some unexpected messages or messages formatted differently from how the high-level GenServer API exposes them to your code. If you want to be notified of handle_call/handle_cast/handle_event invocations you can try function call tracing instead.

rvirding

rvirding

Creator of Erlang

Yes, e.g. the {:"$gen_cast", {:push, :hello}} is the format of the message GenServer.cast sends and the others are replies to GenServer.calls your process has sent out.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement