Setting Up Stackdriver Trace for Go

Stackdriver Trace can be used by Go applications using the OpenCensus package for Go.

Stackdriver Trace's Go support is provided by OpenCensus, a set of tracing and application metrics instrumentation libraries that work with multiple backends. The latest details about OpenCensus for Go, along with additional documentation and examples, can be found on its GitHub page.

Installing the package

To use the OpenCensus package, go get it by running the following command:

go get -u go.opencensus.io

Configuring the Stackdriver Trace exporter

In order to export the collected data, use a Stackdriver Trace exporter. Exporter uses the Application Default Credentials for authentication. You can omit the project ID if your code is running on Google Cloud Platform.

import (
  "contrib.go.opencensus.io/exporter/stackdriver"
  "go.opencensus.io/trace"
)

exporter, err := stackdriver.NewExporter(stackdriver.Options{
  ProjectID: "google-cloud-project-id",
})
if err != nil {
  log.Fatal(err)
}
trace.RegisterExporter(exporter)

Running on Google Cloud Platform

The OpenCensus client library should work without the need to manually provide authentication credentials for VM instances running on Google Cloud Platform, as long as the Stackdriver Trace API access scope is enabled on that instance.

App Engine

On Google App Engine flexible environment, the Stackdriver Trace API access scope is enabled by default, and the OpenCensus client library can be used without needing to provide credentials or a project ID.

Kubernetes Engine

On Google Kubernetes Engine, add the trace.append OAuth access scope when creating the cluster:

gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/trace.append

Compute Engine

For Google Compute Engine instances, you need to explicitly enable the trace.append Stackdriver Trace API access scope for each instance. When creating a new instance through the Google Cloud Platform Console, you can do this under Identity and API access: Use the Compute Engine default service account and select "Allow full access to all Cloud APIs" under Access scopes.

To use something other than the Compute Engine default service account see the docs for Creating and Enabling Service Accounts for Instances and the Running elsewhere section below. The important thing is that the service account you use has the Cloud Trace Agent role.

Sample Stackdriver Trace application for Go

// Sample trace_quickstart creates traces incoming and outgoing requests.
package main

import (
	"log"
	"net/http"

	"contrib.go.opencensus.io/exporter/stackdriver"
	"contrib.go.opencensus.io/exporter/stackdriver/propagation"
	"go.opencensus.io/plugin/ochttp"
	"go.opencensus.io/trace"
)

func main() {
	// Create an register a OpenCensus
	// Stackdriver Trace exporter.
	exporter, err := stackdriver.NewExporter(stackdriver.Options{
		ProjectID: "YOUR_PROJECT_ID",
	})
	if err != nil {
		log.Fatal(err)
	}
	trace.RegisterExporter(exporter)

	client := &http.Client{
		Transport: &ochttp.Transport{
			// Use Google Cloud propagation format.
			Propagation: &propagation.HTTPFormat{},
		},
	}

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		req, _ := http.NewRequest("GET", "https://metadata/users", nil)

		// The trace ID from the incoming request will be
		// propagated to the outgoing request.
		req = req.WithContext(r.Context())

		// The outgoing request will be traced with r's trace ID.
		if _, err := client.Do(req); err != nil {
			log.Fatal(err)
		}
	})
	http.Handle("/foo", handler)
	log.Fatal(http.ListenAndServe(":6060", &ochttp.Handler{}))
}

Viewing the traces

After deployment, you can view the traces in the GCP Console Trace Viewer.

Go to the Trace Viewer page

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Trace