Thoughts on replacing mocks with telemetry events
👋
I had an idea a few days ago, what if FlameOn starts and stops tracing on Telemetry events? That would potentially allow removing mocks.
Since telemetry runs in the caller process, we have access to the current pid, and we can use the new :trace module with very minimal effort for flamegraphs (+ with heap alloc stats). Flamecharts (which is what FlameOn is doing now) would require extra effort. With :trace we can also cross process boundary and start tracing the processes that the current process "calls", right now FlameOn just shows sleep when something like GenServer.call is encountered.
The API could be just a telemetry event for which to start tracing, and an optional event for when to stop tracing.
ref = make_ref()
# or something like FlameTrace.trace_once([:phoenix, :endpoint, :start])
:telemetry.attach("trace-phx-on", [:phoenix, :endpoint, :start], &FlameOn.start_tracing/4, sample: 0.01, ref: ref)
:telemetry.detach("trace-phx-on")
FlameOn.svg(FlameOn.report(ref))
Context:
- https://www.erlang.org/doc/apps/tools/tprof.html -- uses
trace, can be used as example to implement flamegraphs with heap alloc stats - https://www.erlang.org/doc/apps/kernel/trace.html