Instrumenting Erlang code

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Sun Jan 26 14:28:51 CET 2020


On Sun, Jan 26, 2020 at 1:58 PM Frank Muller <frank.muller.erl@REDACTED>
wrote:

> Hi everyone
>
> I would like to implement a custom instrumentation module for my Erlang
> code.
> Let assume this function:
>
>
If you have

f1() ->
  Exprs,
  ok.

You can write

f1() ->
  S = instrument:start(),
  try
    Exprs,
    ok
  finally
    instrument:end(S)
  end.

However, some times you want to track an error apart from a success value.
Positive and negative exit paths often require different instrumentation.
The success path is often interested in processing time (latency) in
addition to processing count. The error path often centers around the idea
of an error count, especially if the exit is fast. If you blend them, the
problem is that the fast errors muddles your view of the successes, as your
latency distribution becomes multi-modal. In turn, your mean and median
doesn't work like you think they do anymore.


> Other problem I can think of is when we have multiple return paths and/or
> recursive loop:
>
>
If you loop, you want to wrap:

f1(0) -> ok;
f1(N) ->
  do(),
  f(N-1).

f1_(N) ->
  S = instrument:start(),
  try
    f1(N)
  finally
    instrument:end(S)
  end.

This only pushes a single exception handler to the stack. An important
caveat is when Exprs or do() hibernates. Beware of that situation, since it
removes the stack and invokes a continuation.


> Is that doable?
> If yes, can I apply like this logic to all modules running in my Erlang
> node?
>
>
Yes, parse transforms.

But in my experience, applying something to all modules is usually the
wrong way to go about instrumentation. Maybe with a

-instrument([F, ...]).

to help the parsetransform along the way.

(

This is considerably easier in something like Elixir with its macro system,
or OCaml, because you have PPX extensions.

In OCaml, you would have

let f1() ->
    Expr.

And you could just annotate it

let%instrument f1() ->
  Expr.

And write a PPX rewriter for that.

)


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200126/b98eb9e7/attachment.htm>


More information about the erlang-questions mailing list