Using OpenCensus with Cloud Bigtable and Stackdriver Trace
This tutorial uses the following billable components of Google Cloud Platform (GCP):
- Google Compute Engine
- Cloud Bigtable
You can use the Pricing Calculator to generate a cost estimate based on your projected usage.
New GCP users might be eligible for a free trial.
We recommend that you deploy this tutorial into an ephemeral project, which can then be deleted once you’re done.
Before you begin
Create a new project
- In the GCP Console, go to the Manage resources page.
- Select a project, or click Create Project to create a new GCP project.
- In the dialog, name your project. Make a note of your generated project ID.
- Click Create to create a new project.
Initialize the environment
Start a Cloud Shell instance.
Enable the Google Compute Engine and Cloud Memorystore for Redis Admin APIs by running this command in Cloud Shell:
gcloud services enable compute.googleapis.com \ bigtable.googleapis.com \ bigtableadmin.googleapis.com \ logging.googleapis.com
Creating a Cloud Bigtable instance
In Cloud Shell, create a Cloud Bigtable development instance:
gcloud bigtable instances create cbt-oc \ --cluster=cbt-oc \ --cluster-zone=us-central1-c \ --display-name=cbt-oc \ --instance-type=DEVELOPMENT
Create and configure a Compute Engine VM
Create a Compute Engine VM with the necessary security scopes by running the following commands from Cloud Shell.
gcloud compute instances create trace-client --zone=us-central1-c \ --scopes="https://www.googleapis.com/auth/bigtable.admin.table,\ https://www.googleapis.com/auth/bigtable.data,\ https://www.googleapis.com/auth/logging.write,\ https://www.googleapis.com/auth/monitoring.write,\ https://www.googleapis.com/auth/trace.append"
Deploying the Java application
Log in to the VM
gcloud compute ssh trace-client --zone=us-central1-c
Run the following command to install Git, the Java 8 JDK, and Maven:
sudo apt-get install git openjdk-8-jdk maven -y
Clone the source repository for this tutorial:
git clone https://github.com/GoogleCloudPlatform/community.git
Update the Java application with some configuration specific to your project.
Navigate to the folder containing the Java source:
Configure the environment variable for the application code to use the
Run the following maven commands to build and run the program:
mvn package -DskipTests --quiet mvn exec:java -Dexec.mainClass=com.example.bigtable.App --quiet
Viewing traces with Stackdriver Trace
Go to the Stackdriver Trace console
Select Trace List on the left side to show a table similar to the following:
The tracing label opencensus.Bigtable.Tutorial in the Timeline is the name of the outermost tracing scope that is defined in the code snippet above.
If you select opencensus.Bigtable.Tutorial, you'll be taken to a view that shows more information about the call chain, along with other useful information such as lower-level tracing and operation-level call latencies.
For instance, each of the series of write and read rows are encapsulated by the lower level, user-defined WriteRows and ReadRows tracing spans respectively.
Below ReadRows, you can first see the get operation, followed by the table scan operations.
The other items included in the trace list, such as Operation.google.bigtable.admin.v2.BigtableTableAdmin.CreateTable,
occurred outside of the manually defined tracing scope, so these are included as separate operations in the list.
View the captured metrics with Stackdriver Metrics Explorer
Navigate to the Metrics Explorer.
In the Find resource type and metric field, enter the following:
Select this metric from the list.
In the right pane, the distribution heatmap graph is shown.
Since this tutorial uses multiple GCP components, please be sure to delete the associated resources once you are done.