Setting Up Cloud Debugger for Node.js

Overview

This page describes how to configure your environment and your Node.js application to use Cloud Debugger. For some environments, you must explicitly specify the access scope to let the Cloud Debugger agent send data. We recommend setting the broadest possible access scope and then using Identity and Access Management to restrict access. In keeping with this best practice, set the access scope to be all Cloud APIs with the option cloud-platform.

Language versions and compute environments

Cloud Debugger is available for Node.js 6 and higher on the following compute environments:

App Engine Standard environment App Engine Flexible environment Compute Engine Google Kubernetes Engine Cloud Run Cloud Run for Anthos on Google Cloud VMs and Containers running elsewhere Cloud Functions

Setting up Cloud Debugger

To set up Cloud Debugger, complete the following tasks:

  1. Verify the Cloud Debugger API is enabled for your project.

  2. Install and configure the Debugger on the compute environment you're using.

  3. Select your source code.

Verifying the Cloud Debugger API is enabled

To begin using Cloud Debugger, ensure that the Cloud Debugger API is enabled. Cloud Debugger is enabled by default for most projects.
Enable Cloud Debugger API

Canary snapshots and logpoints

The Debugger agent for Node.js can use canary snapshots and logpoints every time you set a snapshot or logpoint.

The Debugger agent canaries snapshots and logpoints to protect large jobs from any potential bug in the Debugger agent which can take the entire job down when a snapshot or a logpoint is applied.

To mitigate this, Debugger canaries snapshots and logpoints on a subset of your running instances each time they are set. After Debugger verifies the snapshot or logpoint does not adversely affect your running instances, Debugger then applies the snapshot or logpoint to all instances.

To learn how to use Debugger in canary mode, go to the the Debug snapshots and Debug logpoints pages.

Enabling canary snapshots and logpoints

When you install the latest version of the Debugger agent, you have the option to enable or disable canarying. Canarying is disabled by default.

When to enable canary snapshots and logpoints

To protect deployment and production-critical workloads, enable canarying when debugging these workloads.

If you have a single instance, you can still debug with canarying enabled, but your single instance runs without canarying the snapshot or logpoint.

When not to enable canary snapshots and logpoints

Don't enable canarying on workloads that have an execution time of less than 40 seconds, for instance, jobs using Cloud Functions.

Don't enable canarying if you want a faster snapshot-triggering cycle.

To configure the Debugger agent to not canary snapshots and logpoints, go to the installation instructions for the Google Cloud platform you're using.

App Engine

  1. Use npm to install the package:

    npm install --save @google-cloud/debug-agent
    
  2. Enable the agent at the top of your app's main script or entry point (but after @google/cloud-trace if you are also using it):

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({serviceContext: {enableCanary: true}});
    

    To debug with canarying not enabled, set enableCanary to false:

    require('@google-cloud/debug-agent').start({serviceContext: {enableCanary: false}});
    

The debugger is now ready for use with your app.

To have the Debug page in the Cloud Console automatically display source code matching the deployed app, see Selecting source code automatically.

Google Kubernetes Engine

GCLOUD

To enable Debugger using gcloud, complete the following steps:

  1. Create your cluster with one of the following access scopes:

    • https://www.googleapis.com/auth/cloud-platform grants your cluster access to all Google Cloud APIs.

    • https://www.googleapis.com/auth/cloud_debugger grants your cluster access to only the Debugger API. Use this access scope to harden your cluster's security.

    gcloud container clusters create example-cluster-name \
           --scopes=https://www.googleapis.com/auth/cloud_debugger
    
  2. Add the following line to your Dockerfile to add the Debugger agent:

    RUN  npm install --save @google-cloud/debug-agent
    
  3. Enable the agent at the top of your app's main script or entry point (but after @google/cloud-trace if you are also using it):

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({
      serviceContext: {
        service: 'SERVICE',
        version: 'VERSION'
        enableCanary: true,
      }
    });
    

    To debug with canarying not enabled, set enableCanary to false:

    enableCanary: false
    

    Where:

    • SERVICE is a name for your app, such as MyApp, Backend, or Frontend.
    • VERSION is a version, such as v1.0, build_147, or v20170714.

    We recommend setting these from environment variables so you do not need to change the source code with each deployment.

The debugger is now ready for use when you deploy your containerized app.

To have the Debug page in the Cloud Console automatically display source code matching the deployed app, see Selecting source code automatically.

CONSOLE

To enable Debugger using the console, complete the following steps:

  1. After selecting your cluster type, click More options from the Node pools pane:

    Node pools options

  2. Select one of the following from the Security pane:

    • Allow full access to all Cloud APIs.

    • Allow access for each API and then select Enabled for Cloud Debugger.

  3. Add the following line to your Dockerfile to add the Debugger agent:

        RUN  npm install --save @google-cloud/debug-agent
    
  4. Enable the agent at the top of your app's main script or entry point (but after @google/cloud-trace if you are also using it):

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({
      serviceContext: {
        service: 'SERVICE',
        version: 'VERSION'
        enableCanary: true,
      }
    });
    

    To debug with canarying not enabled, set enableCanary to false:

    enableCanary: false
    

    Where:

    • SERVICE is a name for your app, such as MyApp, Backend, or Frontend.
    • VERSION is a version, such as v1.0, build_147, or v20170714.

    We recommend setting these from environment variables so you do not need to change the source code with each deployment.

Compute Engine

  1. Make sure your Compute Engine instances are created with the access scope option Allow full access to all Cloud APIs, or have one of the following access scopes:

    • https://www.googleapis.com/auth/cloud-platform
    • https://www.googleapis.com/auth/cloud_debugger
  2. Use npm to install the package:

    npm install --save @google-cloud/debug-agent
    
  3. Enable the agent at the top of your app's main script or entry point (but after @google/cloud-trace if you are also using it):

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({
      serviceContext: {
        service: 'SERVICE',
        version: 'VERSION',
        enableCanary: true,
      }
    });
    

    To debug with canarying not enabled, set enableCanary to false:

    enableCanary: false
    

    Where:

    • SERVICE is a name for your app, such as MyApp, Backend, or Frontend.
    • VERSION is a version, such as v1.0, build_147, or v20170714.

    We recommend setting these from environment variables so you do not need to change the source code with each deployment.

The debugger is now ready for use with your app.

To have the Debug page in the Cloud Console automatically display source code matching the deployed app, see Selecting Source Code Automatically.

Cloud Run and Cloud Run for Anthos on Google Cloud

  1. Use npm to install the package:

    npm install --save @google-cloud/debug-agent
    
  2. Enable the agent at the top of your app's main script or entry point (but after @google/cloud-trace if you are also using it):

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({serviceContext: {enableCanary: true}});
    

    To debug with canarying not enabled, set enableCanary to false:

    enableCanary: false
    

    The debugger is now ready for use when you deploy your app.

Local and elsewhere

  1. Use npm to install the package:

    npm install --save @google-cloud/debug-agent
    
  2. Download service account credentials.

    To use the Cloud Debugger agent for Node.js on machines not hosted by Google Cloud, the agent must use a Google Cloud service account credentials to authenticate with the Cloud Debugger Service.

    Use the Cloud Console Service Accounts page to create a credentials file for an existing or new service account. The service account must have at least the Cloud Debugger Agent role.

  3. Configure and enable the agent with the downloaded credentials:

    To debug with canarying enabled:

    require('@google-cloud/debug-agent').start({
      projectId: 'your-project-id',
      keyFilename: '/path/to/key.json',
      serviceContext: {
        service: 'SERVICE',
        version: 'VERSION',
        enableCanary: true,
      }
    });
    

    To debug with canarying not enabled, set enableCanary to false:

    enableCanary: false
    

    Where:

    • SERVICE is a name for your app, such as MyApp, Backend, or Frontend.
    • VERSION is a version, such as v1.0, build_147, or v20170714.

    We recommend setting these from environment variables so you do not need to change the source code with each deployment.

The debugger is now ready for use with your app.

The Debug page in the Cloud Console can display local source files, without upload, for local development. See Selecting Source Code Manually.