⚡️ Tracing with Recon
Notes
It’s mind blowing how powerful (and simple) tracing is in Elixir.
Here’s a quick intro into tracing in Elixir with Recon.
Tracing with :dbg
In Elixir and Erlang, we can trace with Erlang’s :dbg
module (not to be
confused with Elixir’s dbg
macro). But that can be a foot-gun since we can
accidentally overwhelm our running system with traces. 😵
Tracing with Recon
Recon is a library that makes tracing easier and safer.
There are several modules that we could use, so I encourage to look at the
docs. But for now, we’ll look at :recon_trace.calls/3
.
The basic way to use it is to give it a module, function, and arity, and the max number of traces we want (this prevents us from accidentally overwhelming our system 🎉).
From our iex
session:
:recon_trace.calls({Scout.Timeline, :get_post!, 1}, 1)
So, we’re tracing calls to Scout.Timelineg.get_post!/1
. And in this case, we
trace it one time before we trigger Recon’s automatic trigger to stop tracing.
Trace any arity
We can also trace get_post!
with any arity by providing a wildcard pattern
match spec:
:recon_trace.calls({Scout.Timeline, :get_post!, :_}, 1)
So, if we were to have a get_post!/1
and get_post!/2
, a call to either would
be traced.
Returning traces
The code above will only trace calls to the function. So, for example, we’ll be
able to see when Scout.Timeline.get_post!/1
gets called and with what
arguments (e.g. id 1
). But we don’t get the return value.
Thankfully, recon lets us request that very easily:
:recon_trace.calls({Scout.Timeline, :get_post!, :return_trace}, 2)
Now, we’ll be able to get the function invocation and its return value! (Note that we set the max number of traces to at least 2. Recon counts the function invocation as one trace and the return value as another trace – so we need at least 2).
More in the docs
That’s just the tip of the iceberg with Recon. If you want to learn more, check out out the docs, and let me know what else you’d like to learn! I could create a few more tips on using Recon.