How-To: Debug Dapr applications with Visual Studio Code
When developing Dapr applications, you typically use the Dapr CLI to start your daprized service similar to this:
One approach to attaching the debugger to your service is to first run daprd with the correct arguments from the command line and then launch your code and attach the debugger. While this is a perfectly acceptable solution, it does require a few extra steps and some instruction to developers who might want to clone your repo and hit the “play” button to begin debugging.
If your application is a collection of microservices, each with a Dapr sidecar, it will be useful to debug them together in Visual Studio Code. This page will use the to showcase how to configure VSCode to debug multiple Dapr application using VSCode debugging.
Prerequisites
- Install the Dapr extension. You will be using the it offers later on.
- Optionally clone the hello world quickstart
The file contains for a VS Code debug run. This file defines what will launch and how it is configured when the user begins debugging. Configurations are available for each programming language in the Visual Studio Code marketplace.
Scaffold debugging configuration
The Dapr VSCode extension offers built-in scaffolding to generate launch.json
and tasks.json
for you.
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Nodeapp with Dapr",
"<node_internals>/**"
],
"program": "${workspaceFolder}/node/app.js",
"preLaunchTask": "daprd-debug-node",
"postDebugTask": "daprd-down-node"
},
{
"type": "python",
"request": "launch",
"name": "Pythonapp with Dapr",
"program": "${workspaceFolder}/python/app.py",
"console": "integratedTerminal",
"preLaunchTask": "daprd-debug-python",
"postDebugTask": "daprd-down-python"
}
]
}
If you’re using ports other than the default ports baked into the code, set the DAPR_HTTP_PORT
and DAPR_GRPC_PORT
environment variables in the launch.json
debug configuration. Match with the httpPort
and grpcPort
in the daprd tasks.json
. For example, launch.json
:
tasks.json
:
{
"httpPort": 3502,
"grpcPort": 50002
}
Each configuration requires a request
, type
and name
. These parameters help VSCode identify the task configurations in the .vscode/tasks.json
files.
type
defines the language used. Depending on the language, it might require an extension found in the marketplace, such as the Python Extension.name
is a unique name for the configuration. This is used for compound configurations when calling multiple configurations in your project.${workspaceFolder}
is a VS Code variable reference. This is the path to the workspace opened in VS Code.- The
preLaunchTask
andpostDebugTask
parameters refer to the program configurations run before and after launching the application. See step 2 on how to configure these.
For more information on VSCode debugging parameters see .
Step 2: Configure tasks.json
For each defined in .vscode/launch.json
, a corresponding task definition must exist in .vscode/tasks.json
.
For the quickstart, each service needs a task to launch a Dapr sidecar with the daprd
type, and a task to stop the sidecar with daprd-down
. The parameters appId
, httpPort
, metricsPort
, label
and type
are required. Additional optional parameters are available, see the reference table here.
For this example the compound configuration is:
{
"version": "2.0.0",
"configurations": [...],
"compounds": [
{
"name": "Node/Python Dapr",
"configurations": ["Nodeapp with Dapr","Pythonapp with Dapr"]
}
]
}
Step 4: Launch your debugging session
You can now run the applications in debug mode by finding the compound command name you have defined in the previous step in the VS Code debugger:
You are now debugging multiple applications with Dapr!
Below are the supported parameters for VS Code tasks. These parameters are equivalent to daprd
arguments as detailed in this reference: