Is it okay to cast messages to a process that may or may not be running?

I have a monitoring solution that uses genservers that we can turn on and off when needed (We only want to monitor for debugging purposes). However, I have integrated code that will always cast messages to those processes regardless of if they are running. While this seems to work just fine and appears to be a no-op, something does not feel right about it. Is there a better way to do this, or is this an acceptable solution?

The Logger-way is probably the one to go here…

Use a macro which replaces itself with a proper function call in :dev, while it replaces itself with just :ok when in :prod.

Or if you want to avoid writing macros, you can embrace the ideas of “backends”. You would start your monitoring solution in each environment, and have a process that you send the messages to in each case. But then, depending on configuration specified in dev.exs or production.exs, it uses different backend. The production backend does the real job, but “NullBackend” just dumps all the messages.

The advantage of the approach is that you limit the difference in code being executed on production and on dev to only the part that syncs the monitoring messages, so you can possibly catch some bugs in your monitoring tool itself while you work on it. If you go with Logger-like approach, you are effectively disabling bigger chunk of code.

1 Like

The NullBackend is a possible solution, but as it will ultimatively call at least one function maybe even two or three to dispatch to the correct backend, it will affect the reduction count of the calling process and therefore might affect performance.

If this effect is noticeable at all depends on some things, overall application architecture is one of these things.