vrod

vrod

Monitoring messages in process mailboxes

I am wondering for advice on how to monitor an application with many genservers doing long-running work. Often we want to know “how many messages are in the queue?” The process has a mailbox not a queue but this is the same idea : how can we see what our app is doing? I have seen GenStage.estimate_buffered_count/2 and I am wondering if that is the best way. There is a mix of genserver and genstage, so I thought maybe Process.info/1 could help with genservers, but I noticed that this function seems to require a pid and it will not work with a process name. Also I do not see the messages in the process info? Only message_queue_len?
Another idea was because we are using pubsub, maybe we could add subscribers that would count messages but that feels maybe smelly.

Thank you for suggestions!

Marked As Solved

rvirding

rvirding

Creator of Erlang

A simple way of of finding the length of the message queue is Process.info(pid, :message_queue_len) which avoids copying the whole message queue. Only having :message)_queue_len in the default info is to NOT copy all the messages unless you explicitly request it.

12
Post #5

Also Liked

nathanl

nathanl

I haven’t used this yet, but as of OTP 27.0, you can subscribe to notifications about mailbox lengths.

:erlang.system_monitor(self(), [{:long_message_queue, {disable, enable}}])

disable and enable are integer thresholds for when you want receive a message. More explanation is in the docs.

rvirding

rvirding

Creator of Erlang

It is not really the handle_info callback which processes the actual message. The GenServer has a top-loop which sits and receives messages arriving at the process. When a message arrives it checks the message format and and calls the relevant callback to handle the message. When the callback returns it goes back into the top-loop and waits for and then processes the next message aand then waits for the next one and then … . In your case it calls the handle_info callback with the message.

This means that there is no real buffering of messages in the GenServer message queue so checking its length will generally return a small number. The only time the queue can become long is if the GenServer becomes overloaded.

RudManusachi

RudManusachi

To find a pid by process name we could check Process.whereis(:name)

And now we could see the messages via Process.info(pid, :messages), but only be careful, as this operation copies all messages to the process that called. So if mailbox is huge - the operation might be expensive.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement