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

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
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 record...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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