Setting Up Stackdriver Trace

App Engine standard environment

Stackdriver Trace is automatically enabled for all applications running on App Engine standard environment. App Engine standard environment applications do not require any additional instrumentation libraries or SDKs.

VMs or containers (Compute Engine, Container Engine, App Engine flexible, non-Google services)

Applications running on VMs or containers (including Google Compute Engine, App Engine flexible environment, and Google Container Engine, as well as non-Google services) are not automatically traced. Use the SDKs listed below, or the Stackdriver Trace API, to send traces to the service.

Node.js

To enable Stackdriver Trace for Node.js applications running on App Engine flexible environment, import the Node.js Stackdriver Trace agent at the start of your application.

Stackdriver Trace is compatible with Node.js version 0.12 or greater. Node.js version 5+ is recommended.

Installation

  1. Install with npm or add to your package.json:

    npm install --save @google/cloud-trace

  2. Import the agent at the top of your main script:

    require('@google/cloud-trace').start();
    

Sample application

if (process.env.NODE_ENV === 'production') {
  require('@google-cloud/trace-agent').start();
}

const express = require('express');
const got = require('got');

const app = express();
const DISCOVERY_URL = 'https://www.googleapis.com/discovery/v1/apis';

// This incoming HTTP request should be captured by Trace
app.get('/', (req, res) => {
  // This outgoing HTTP request should be captured by Trace
  got(DISCOVERY_URL, { json: true })
    .then((response) => {
      const names = response.body.items.map((item) => item.name);

      res
        .status(200)
        .send(names.join('\n'))
        .end();
    })
    .catch((err) => {
      console.error(err);
      res
        .status(500)
        .end();
    });
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

Java

The Google Cloud Trace SDK for Java is available on GitHub:

https://github.com/GoogleCloudPlatform/cloud-trace-java

Go

The Stackdriver Trace SDK for Go is available on GitHub. Usage instructions are included in the code comments.

https://github.com/GoogleCloudPlatform/google-cloud-go/tree/master/trace

Ruby

The Stackdriver Trace SDK for Ruby is available on GitHub.

https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/master/google-cloud-trace

Viewing traces

After deployment, you can view recorded traces and inspect the RPCs in your application.

Send feedback about...

Stackdriver Trace Documentation