Elixir Tips

Elixir Tips

  • Alexander Koutmos

    akoutmos

    akoutmos

    Tracing GenServer execution

    The BEAM has some of the best observability tools built right into the runtime. Right down to tracing individual GenServer process execution flow!
    iex> {:ok, agent} = Agent.start_link fn -> [] end
    {:ok, #PID<0.112.0>}
    
    iex> :sys.trace(agent, true)
    :ok
    
    iex> Agent.get_and_update(agent, fn state -> {state, [1 | state]} end)
    *DBG* <0.112.0> got call {get_and_update, #Fun<erl_eval.44.12345123} from <0.110.0>
    *DBG* <0.112.0> sent [] to <0.110.0>, new state [1]
    []
    
    iex> Agent.get(agent, fn state -> state end)
    *DBG* <0.112.0> got call {get, #Fun<erl_eval.44.12345124} from <0.110.0>
    *DBG* <0.112.0> sent [1] to <0.110.0>, new state [1]
    [1]
    
    iex> :sys.trace(agent, false)
    :ok
    
    iex> Agent.get(agent, fn state -> state end)
    [1]
    
    130 upvotes