Debug your backend in production
Debugging applications that run in a remote environment is so painful because setting breakpoints to these applications is impossible. We will talk about a next-generation debugging tool named Sidekick in this story. It is a remote and real-time application debugger that helps you solve problems where and when they occur without requiring you to stop or rebuild your application.
What is remote debugging
Remote debugging means having the ability to debug an application running on a machine that is not owned or controlled by yourself in software development literature. It may be needless in some scenarios where you run an application in your local environment. Still, if you could debug this application after deploying it to any remote environment, it starts to make sense.
The advantages of remote debugging
- Debug without interrupting the execution of an application
- Debug applications that are running in a remote environment
- Set a non-intrusive breakpoint to your code
- Collect actionable data from your remote application
Set Up Project
Create a Sample Application
Sidekick tested with lots of frameworks of NodeJS, Java, and Python. We will use the Express framework with NodeJS in this tutorial. You can use similar steps for other languages or other frameworks.
As an example project, I will use this tutorial repository. It is a basic express website. You can use any other project or your own project instead.
Integration sidekick is a piece of cake because it only required a few lines of code to be added. But first, we need to install the Sidekick agent with the command below:
After installing the agent, add these two lines to your root app.js file. We recommend using your API key as an environment variable to hold your Sidekick workspace safe. You can find the integrated project’s repository here.
Push to Version control
To be able to remote debug, your code must be readable for the Sidekick. We will push our code to Github in this step so the Sidekick can read the code from there then.
Be careful that your code will be running on your local environment is the same as the code you pushed version control in this step. Otherwise, errors will occur when debugging.
Sidekick support GitHub, GitLab, and BitBucket for now. You can publish your project on one of them you want. Here is the demo project I used for this project.
Get Your API-Key
After you sign up for Sidekick, a quick tutorial for a sample Java application will welcome you. You can skip it because we will examine the nodeJS equivalent in this article in detail.
All you need to do in this step is sign up for Sidekick and pass the basic steps that ask your name and etc. Then get your API key from the welcome page. We will come back to this page soon.
You can run the application on a docker container as well. No additional implementation on your code is required. Just follow the docs for this step.
Because we used our API key as an environment variable, we need to define it first. I will just export the API key like below:
Then run the application simply:
Debug your application remotely
Choose the repository, branch, and commit
Everything’s done now. Let’s go back to the Sidekick console and choose repository, branch, and commit. As we mentioned, it is essential to select the correct commit that your application is running on.
Choose the application that you want to debug
Then you will be able to see the applications running with the sidekick agent and your API key. Choose the application you want to debug:
Enjoy remote debugging!
Now set a breakpoint to one of your backend services. In this example, I set a breakpoint for the routers/speakers' service of my project.
After any trigger of the breakpoint line in your running application shown in the example, tracepoint’s event will appear on the sidekick console.