You can enable Cloud Trace for Go applications
by using OpenCensus. OpenCensus is
a set of instrumentation libraries for collecting trace and metric data that
work with multiple backends. For the latest details about OpenCensus
for Go, along with additional documentation and examples, see
opencensus-go
.
Installing the package
Retrieve the OpenCensus trace package:
go get go.opencensus.io/trace
Configuring the Stackdriver exporter
In order to export the collected Trace data, use a Stackdriver exporter:
If you are running on Google Cloud infrastructure, then
you don't need to set the ProjectID
field to your Google Cloud
project ID because the client library for Go automatically gathers
this data from a Google Cloud metadata server.
If you aren't running on Google Cloud infrastructure, then you must
set the ProjectID
field to your Google Cloud project ID. In the
example code, an environment variable is used and the application has been
modified to read this variable.
Configuring integration with Cloud Logging
For information on how to send Cloud Trace data to Cloud Logging, see Integrating with Cloud Logging.
Configure your platform
You can use Cloud Trace on Google Cloud and other platforms.
Running on Google Cloud
When your application is running on Google Cloud, you don't need to provide authentication credentials in the form of a service account to the client library. However, you do need to ensure that your Google Cloud platform has the Cloud Trace API access scope enabled.
For a list of supported Google Cloud environments, see Environment support.
For the following configurations, the default access-scope settings enable the Cloud Trace API:
- App Engine flexible environment
App Engine standard environment
Google Kubernetes Engine (GKE)
Compute Engine
Cloud Run
If you use custom access scopes, then you must ensure that Cloud Trace API access scope is enabled:
For information about how to configure the access scopes for your environment by using the Google Cloud console, see Configuring your Google Cloud project.
For
gcloud
users, specify access scopes using the--scopes
flag and include thetrace.append
Cloud Trace API access scope. For example, to create a GKE cluster with only the Cloud Trace API enabled, do the following:gcloud container clusters create example-cluster-name --scopes=https://www.googleapis.com/auth/trace.append
Running locally and elsewhere
If your application is running outside of Google Cloud, then you must provide authentication credentials in the form of a service account to the client library. The service account must contain the Cloud Trace agent role. For instructions, see Creating a service account.
Google Cloud client libraries use Application Default Credentials (ADC) to find your application's credentials.
You can provide these credentials in one of three ways:
Run
gcloud auth application-default login
Place the service account in a default path for your operating system. The following lists the default paths for Windows and Linux:
Windows:
%APPDATA%/gcloud/application_default_credentials.json
Linux:
$HOME/.config/gcloud/application_default_credentials.json
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path to your service account:
Linux/macOS
export GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key
Windows
set GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key
PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="path-to-your-service-accounts-private-key"
Sample Trace application for Go
Viewing the traces
After deployment, you can view the traces in the Google Cloud console Trace Viewer.
Troubleshooting
For information on troubleshooting issues with Cloud Trace, go to the Troubleshooting page.
Upload of spans for the Go OpenCensus exporter fails
The following are examples of errors that occur when the bundler library used by the exporter is unable to manage the number of spans the application is generating and uploading:
OpenCensus Stackdriver exporter: failed to upload span: buffer full OpenCensus Stackdriver exporter: failed to upload 183 spans: buffer full
To resolve this situation, try one or more of the following:
In the
Options
struct passed to thetraceExporter
constructor, increase the value of theTraceSpansBufferMaxBytes
field. This optional field specifies the maximum number of bytes used for buffering spans. The default value is 8MB.In the
Options
object passed to thetraceExporter
constructor, decrease the values for the following fields:BundleDelayThreshold
: This optional field specifies the maximum time the exporter can wait before uploading trace spans. The default value of this field is two seconds.BundleCountThreshold
: This optional field specifies the number of spans that can be buffered. The default value of this field is 50.
Export fewer spans. To reduce the number of spans you export, configure a global sampler or pass a sampler to each
StartSpan
. For sample code and more information about these configurations, see OpenCensus Sampling.
Resources
- OpenCensus
- GitHub:
census-instrumentation/opencensus-go
- Trace API V1 package
- Trace API V2 package
- Source code
- GitHub issue tracker
- Stack Overflow