Skip to content

Node.js library for Stackdriver Debugger: investigate your code’s behavior in production

License

Notifications You must be signed in to change notification settings

a2call/cloud-debug-nodejs

 
 

Repository files navigation

Stackdriver Debugger agent for Node.js

NPM Version Build Status Test Coverage Dependency Status devDependency Status

This module is experimental, and should be used by early adopters. This module uses APIs that may be undocumented and subject to change without notice.

This module provides Stackdriver Debugger support for Node.js applications. Stackdriver Debugger is a feature of Google Cloud Platform that lets you debug your applications in production without stopping or pausing your application. Here's an introductory video:

Cloud Debugger Intro

Prerequisites

  • Stackdriver Debugger is comptible with Node.js version 0.12 or greater. Node.js v5+ is recommended.

Quick Start

# Install with `npm` or add to your `package.json`.
npm install --save @google/cloud-debug

# Require the agent at the top of your main script (but after '@google/cloud-trace' if you are also using it).
require('@google/cloud-debug');

Deploy your application, and navigate to the Strackdriver Debug view within the Google Cloud Console to set snapshots and start debugging.

Running on Google Cloud Platform

There are three different services that can host Node.js application to Google Cloud Platform.

Google App Engine Flexible Environment

If you are using Google App Engine Flexible Environment , you do not have to do any additional configuration.

Google Compute Engine

Your VM instances need to be created with cloud-platform scope if created via gcloud or the 'Allow API access' checkbox selected if created via the console (see screenshot).

GCE API

If you already have VMs that were created without API access and do not wish to recreate it, you can follow the instructions for using a service account under running elsewhere.

Google Container Engine

Container Engine nodes need to also be created with the cloud-platform scope, which is configurable during cluster creation. Alternatively, you can follow the instructions for using a service account under running elsewhere. It's recommended that you store the service account credentials as Kubernetes Secret.

Running elsewhere

If your application is running outside of Google Cloud Platform, such as locally, on-premise, or on another cloud provider, you can still use Stackdriver Debugger.

  1. You will need to specify your project name. Your project name is visible in the Google Cloud Console, it may be something like particular-future-12345. If your application is running on Google Cloud Platform, you don't need to specify the project name.

     export GCLOUD_PROJECT=<project name>
    
  2. You need to provide service account credentials to your application. The recommended way is via Application Default Credentials.

  3. Create a new JSON service account key.

  4. Copy the key somewhere your application can access it. Be sure not to expose the key publicly.

  5. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the full path to the key. The debug agent will automatically look for this environment variable.

  6. Alternatively, if you are running your application on a machine where your are using the gcloud command line tools, and are logged using gcloud auth login, you already have sufficient credentials, and a service account key is not required.

  7. Generate a source-context.json file which contains information about the version of the source code used to build the application. This file should be located in the root directory of your application. When you open the Stackdriver Debugger in the Cloud Platform Console, it uses the information in this file to display the correct version of the source.

     gcloud app gen-repo-info-file
    

Using the Debugger

Once your application is running (deployed, or elsewhere), you should be able to use the Debug UI in your Cloud developer console. You can find the Debug UI in the 'STACKDRIVER -> Debug' section in the navigation panel, or by simply searching for 'Debug' in the cloud console.

Debug UI

If your source is hosted in a cloud source repository, Stackdriver Debugger will display the source code of your application automatically. Alternatively, you can also point the debugger to local files, a GitHub or Bitbucket repository, through a Source Capture, or you can simply type in a filename and line number. More details are on source options are available here.

If you have the source available, you can set a snapshot by clicking in the gutter (line number area). Once you set a snapshot, the debug agent will insert a momentary breakpoint at the code location in the running instances of the application.

Breakpoint Set

As soon as that line of code is reached in any of the running instances of your application, the stack traces, local variables, and watch expressions are captured, and your application continues.

Breakpoint Hit

Limitations and Requirements

  • The root directory of your application needs to contain a package.json file.
  • You can set snapshot conditions and watch expressions to be evaluated in the context of your application. This leads to some issues you should be aware of
    • You may be able to view sensitive data of your own users by looking at the values of the variables.
    • The debug agent tries to ensure that all conditions and watchpoints you add are read-only and have no side effects. It catches, and disallows, all expressions that may have static side effects to prevent accidental state change. However, it presently does not catch expressions that have dynamic side-effects. For example, o.f looks like a property access, but dynamically, it may end up calling a getter function. We presently do NOT detect such dynamic-side effects.
  • With Node.js 4.x and older, your application may experience a performance impact when there are snapshots active. There should be no impact to performance when no snapshots are active. Node.js v5.x does not have this issue.
  • Node.js v0.10.x or older are not supported as they lack some necessary APIs to avoid a permanent (life of the application) performance hit.

About

Node.js library for Stackdriver Debugger: investigate your code’s behavior in production

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.2%
  • Shell 0.8%