How-To: Debug Dapr applications with Visual Studio Code

Learn how to configure VSCode to debug Dapr applications

Manual debugging

When developing Dapr applications, you typically use the Dapr CLI to start your daprized service similar to this:

  1. dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 app.js

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 hello world quickstart to showcase how to configure VSCode to debug multiple Dapr application using VSCode debugging.

Prerequisites

Step 1: Configure launch.json

The file .vscode/launch.json contains launch configurations 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.

Learn more

In the case of the hello world quickstart, two applications are launched, each with its own Dapr sidecar. One is written in Node.JS, and the other in Python. You’ll notice each configuration contains a daprd run preLaunchTask and a daprd stop postDebugTask.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "type": "pwa-node",
  6. "request": "launch",
  7. "name": "Nodeapp with Dapr",
  8. "skipFiles": [
  9. "<node_internals>/**"
  10. ],
  11. "program": "${workspaceFolder}/node/app.js",
  12. "preLaunchTask": "daprd-debug-node",
  13. "postDebugTask": "daprd-down-node"
  14. },
  15. {
  16. "type": "python",
  17. "request": "launch",
  18. "name": "Pythonapp with Dapr",
  19. "program": "${workspaceFolder}/python/app.py",
  20. "console": "integratedTerminal",
  21. "preLaunchTask": "daprd-debug-python",
  22. "postDebugTask": "daprd-down-python"
  23. }
  24. ]
  25. }

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:

  1. {
  2. // Set the non-default HTTP and gRPC ports
  3. "env": {
  4. "DAPR_HTTP_PORT": "3502",
  5. "DAPR_GRPC_PORT": "50002"
  6. },
  7. }

tasks.json:

  1. {
  2. // Match with ports set in launch.json
  3. "httpPort": 3502,
  4. "grpcPort": 50002
  5. }

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 and postDebugTask 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 VS Code launch attributes.

Step 2: Configure tasks.json

For each task 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.

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "daprd-debug-node",
  6. "type": "daprd",
  7. "appId": "nodeapp",
  8. "appPort": 3000,
  9. "httpPort": 3500,
  10. "metricsPort": 9090
  11. },
  12. {
  13. "label": "daprd-down-node",
  14. "type": "daprd-down",
  15. "appId": "nodeapp"
  16. },
  17. {
  18. "label": "daprd-debug-python",
  19. "type": "daprd",
  20. "appId": "pythonapp",
  21. "httpPort": 53109,
  22. "grpcPort": 53317,
  23. "metricsPort": 9091
  24. },
  25. {
  26. "label": "daprd-down-python",
  27. "type": "daprd-down",
  28. "appId": "pythonapp"
  29. }
  30. ]
  31. }

Step 3: Configure a compound launch in launch.json

A compound launch configuration can defined in .vscode/launch.json and is a set of two or more launch configurations that are launched in parallel. Optionally, a preLaunchTask can be specified and run before the individual debug sessions are started.

For this example the compound configuration is:

  1. {
  2. "version": "2.0.0",
  3. "configurations": [...],
  4. "compounds": [
  5. {
  6. "name": "Node/Python Dapr",
  7. "configurations": ["Nodeapp with Dapr","Pythonapp with Dapr"]
  8. }
  9. ]
  10. }

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:

How-To: Debug with VSCode - 图1

You are now debugging multiple applications with Dapr!

Daprd parameter table

Below are the supported parameters for VS Code tasks. These parameters are equivalent to daprd arguments as detailed in this reference:

ParameterDescriptionRequiredExample
allowedOriginsAllowed HTTP origins (default “”)No“allowedOrigins”: ““
appIdThe unique ID of the application. Used for service discovery, state encapsulation and the pub/sub consumer IDYes“appId”: “divideapp”
appMaxConcurrencyLimit the concurrency of your application. A valid value is any number larger than 0No“appMaxConcurrency”: -1
appPortThis parameter tells Dapr which port your application is listening onYes“appPort”: 4000
appProtocolTells Dapr which protocol your application is using. Valid options are http, grpc, https, grpcs, h2c. Default is http.No“appProtocol”: “http”
argsSets a list of arguments to pass on to the Dapr appNo“args”: []
componentsPathPath for components directory. If empty, components will not be loaded.No“componentsPath”: “./components”
configTells Dapr which Configuration resource to useNo“config”: “./config”
controlPlaneAddressAddress for a Dapr control planeNo“controlPlaneAddress”: “http://localhost:1366/
enableProfilingEnable profilingNo“enableProfiling”: false
enableMtlsEnables automatic mTLS for daprd to daprd communication channelsNo“enableMtls”: false
grpcPortgRPC port for the Dapr API to listen on (default “50001”)Yes, if multiple apps“grpcPort”: 50004
httpPortThe HTTP port for the Dapr APIYes“httpPort”: 3502
internalGrpcPortgRPC port for the Dapr Internal API to listen onNo“internalGrpcPort”: 50001
logAsJsonSetting this parameter to true outputs logs in JSON format. Default is falseNo“logAsJson”: false
logLevelSets the log level for the Dapr sidecar. Allowed values are debug, info, warn, error. Default is infoNo“logLevel”: “debug”
metricsPortSets the port for the sidecar metrics server. Default is 9090Yes, if multiple apps“metricsPort”: 9093
modeRuntime mode for Dapr (default “standalone”)No“mode”: “standalone”
placementHostAddressAddresses for Dapr Actor Placement serversNo“placementHostAddress”: “http://localhost:1313/
profilePortThe port for the profile server (default “7777”)No“profilePort”: 7777
sentryAddressAddress for the Sentry CA serviceNo“sentryAddress”: “http://localhost:1345/
typeTells VS Code it will be a daprd task typeYes“type”: “daprd”

Last modified October 11, 2024: Fixed typo (#4389) (fe17926)