Profiling Node.js code

This page describes setting up Stackdriver Profiler for profiling Node.js code. For Node.js, Profiler offers wall-time and heap profiling; see Profiling concepts for more information.

The following versions of Node.js are supported:

  • 6.12.3 or higher on the 6.x version branch.
  • 8.9.4 or higher on the 8.x version branch.
  • 10.4.1 or higher on the 10.x version branch..

You can use the profiling agent on Linux in the following environments:

  • Compute Engine
  • GKE
  • App Engine.

You can also profile Java code on non-Google Cloud Platform systems. See Profiling code outside Google Cloud Platform for more information.

Enabling the Profiler API

Before you use the profiling agent, ensure that the underlying Profiler API is enabled. You can check the status of the API and enable it if necessary by using either the Cloud SDK gcloud command-line tool or the Cloud Console:

Cloud SDK

  1. If you have not already installed the Cloud SDK on your workstation, see Google Cloud SDK.

  2. Run the following command:

    gcloud services enable

For more information, see gcloud services.

Cloud Console

  1. Go to the APIs & Services dashboard:

    Go to APIs & services

  2. Select the project you will use to access the API.

  3. Click the Add APIs and Services button.

    Add APIs and Services

  4. Search for Profiler API.

  5. In the search results, select Stackdriver Profiler API.

  6. If API enabled is displayed, then the API is already enabled. If not, click the Enable button.

Using Stackdriver Profiler

In all of the supported environments, you use the Profiler by installing the package, requiring the package in your application, and deploying the code.

Use npm install to install the latest version of Stackdriver Profiler:

 npm install --save @google-cloud/profiler

If you are also using the Trace agent, when you modify your code, import the Profiler after the Trace agent (@google-cloud/trace-agent). Your environment-specific require statements are discussed in the following sections.

After modifying your code, deploy it as usual to your environment.

Compute Engine and Google Kubernetes Engine

For Compute Engine and GKE, the require code looks like this:

  serviceContext: {
      service: 'your-service',
      version: '1.0.0'

In both the Compute Engine and GKE environments, create a serviceContext object that includes two parameters:

  • service: A name for the service being profiled
  • version: (optional) The version of the service being profiled

See Service name and version arguments for more information on these configuration options.

App Engine

For App Engine, the require code looks like this:


In App Engine, the service and version parameters are derived from the environment, so you don't have to specify them. Therefore, you don't need to create a serviceContext object.

Analyzing data

After Profiler has collected data, you can view and analyze this data using the Profiler interface. To get started using this interface, see Opening the Profiler interface.

Service name and version arguments

When you load the Profiler agent, you specify a service-name argument and an optional service-version argument to configure it.

The service name lets Profiler collect profiling data for all replicas of that service. The profiler service ensures a collection rate of one profile per minute, on average, for each service name across each combination service versions and zones.

For example, if you have a service with two versions running across replicas in three zones, the profiler will create an average of 6 profiles per minute for that service.

If you use different service names for your replicas, then your service will be profiled more often than necessary, with a correspondingly higher overhead.

When selecting a service name:

  • Choose a name that clearly represents the service in your application architecture. The choice of service name is less important if you only run a single service or application. It is more important if your application runs as a set of micro-services, for example.

  • Make sure to not use any process-specific values, like a process ID, in the service-name string.

  • The service-name string must match this regular expression:


A good guideline is to use a static string like imageproc-service as the service name.

The service version is optional. If you specify the service version, Profiler can aggregate profiling information from multiple instances and display it correctly. It can be used to mark different versions of your services as they get deployed. The Profiler UI lets you filter the data by service version; this way, you can compare the performance of older and newer versions of the code.

The value of the service-version argument is a free-form string, but values for this argument typically look like version numbers, for example, 1.0.0 or 2.1.2.

Agent logging

The profiling agent can report logging information. To enable logging, set the logLevel option when starting the agent. The supported logLevel values are:

  • 0: disables all agent logging (default).
  • 1: enables error logging.
  • 2: enables warning logging.
  • 3: enables info logging.
  • 4: enables debug logging.

Set the logLevel value in the same object that provides the service context:

    serviceContext: { ... }
    logLevel:       3

Known issues

The following are known issues in the beta release of Stackdriver Profiler for Node.js:

  • The profiling agent for Node.js interferes with the normal exit of the program; it can take up to an hour for the program to terminate after all the tasks in the program have completed. Forcibly exiting the program, for example, by using ctrl-C, causes the program to terminate immediately.

What's next

See Using the Stackdriver Profiler Interface for detailed information on different features of Profiler:

  • Learn about the Profiler controls.
  • Learn how to zoom in on specific frames.
  • Learn how to filter and focus the graph.
  • Learn how to compare profiles.
Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Profiler