Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug an Elixir application in production?

This is not particularly about my current problem, but more like in general. Sometimes I have a problem that only happens in production configuration, and I'd like to debug it there. What is the best way to approach that in Elixir? Production runs without a graphical environment (docker).

In dev I can use IEX.pry, but since mix is unavailable in production, that does not seem to be an option.

For Erlang https://stackoverflow.com/a/21413344/1561489 mentions dbg and redbug, but even if they can be used, I would need help on applying them to Elixir code.

like image 892
raarts Avatar asked Oct 11 '17 08:10

raarts


People also ask

How do you debug an elixir in Phoenix?

Setting up the debugger for phoenix or elixir is done automatically for you by Visual Studio Code. When you open a phoenix or elixir project in Visual Studio Code, you should have a Run and Debugger option in your menu. If you don't see it, you can run a Show Run and Debug command with ctrl-shift-D .


1 Answers

First, start a local node running iex on your dev machine using iex -S mix. If you don't want the application that's running locally to cause breakpoints to be activated, you need to disable the app from starting locally. To do this, you can simply comment out the application function in mix.exs or run iex -S mix run --no-start.

Next, you need to connect to the remote node running on docker from iex on your dev node using Node.connect(:"remote@hostname"). In order to do this, you have to make sure both the epmd and the node ports on the remote machine are reachable from your local node.

Finally, once your nodes are connected, from the local iex, run :debugger.start() which opens the debugger with the GUI. Now in the local iex, run :int.ni(<Module you want to debug>) and it will make the module visible to the debugger and you can go ahead and add breakpoints and start debugging.

You can find a tutorial with steps and screenshots here.

like image 196
Emil Avatar answered Sep 28 '22 16:09

Emil