nhpip

nhpip

:dbg and persistent_term or something else

Hi,

So I’m in the process of integrating :dbg , for tracing, into the product I’m working on. I’m using a GenServer to manage and analyze trace requests.

There are 2 types of processes to trace, long-running websocket processes and ephemeral processes handling REST requests. Each process is ‘owned’ by a user and the user_id is actually saved in the process dictionary of each process (looking forward to process labels in OTP 27). Typically a user has about 10 long-running process and maybe a new REST request every 5 seconds or so.

So what we want to do is say

trace bob@foobar.com for event X or for REST request {'GET', "/users/foo/baar"}"

Or we could create traces for the domain foobar.com

So the GenServer will authorize the request, create the match specs and identify the functions to apply the match specs to. A typical trace shall be a few seconds in duration, and tracing will be an infrequent occurrence. Only a single user / domain will be traced at a time.

How to identify the processes though? Basically we need add the pids to the tracer using :dbg.p(pid, [:c]) for all the processes owned by bob@foobar.com.

  1. Use persistent_term and have a function such as below that will be called on every in-bound request.
def maybe_start_trace(user_id) do
    trace_user = :persistent_term.get(:trace_user, nil)
    if trace_user && String.contains?(user_id, trace_user) do
      :dbg.p(self(), [:call])
    end
end

I worry about the cost of setting and clearing the persistent term.

  1. Dynamically compile a module such as:
defmodule TraceMatch do  
  # This line added when tracing is enabled
  def trace_me("bob@foobar.com"), do: :dbg.p(self(), [:call])
  # This line here all the time
  def trace_me(_), do: nil
end
  1. Hybrid-mode. For the running long-lived processes we can identify them from their process dictionary entry and use one of the mechanisms above for the ephemeral processes. This means we are potentially adding hundreds of idle processes to be traced that don’t need to be.

  2. Use the GenServer (or :ets) is not an option - see Amdahl`s law.

  3. A community answer? Underpants gnomes.

Thanks in advance…

Most Liked

rvirding

rvirding

Creator of Erlang

IIRC match specs are interpreted by a small VM and I am guessing that if you have excessive nesting then that VM can run out of stack. But I am guessing here.

Glad you like Erlang and then Elixir. I am not really a super static typing fan :smile: and I have done a lisp on top of Erlang/BEAM LFE (Lisp Flavoured Erlang)

dimitarvp

dimitarvp

Why is it not an option? ETS has a read concurrency mode when you do much more reading than writing.

I’d go for Registry though. While I am fond of dynamically compiling modules to do what we need right on the spot, the BEAM is not always handling hundreds of pattern-matching clauses well in terms of performance. Registry should allow your code to be faster and to use what’s been purposefully built for scenarios like yours.

Where Next?

Popular in Questions Top

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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement