Using OpenCensus with Bigtable and Cloud Trace
Contributed by Google employees.
This tutorial shows how to implement client-side tracing and metrics recording in your Bigtable workloads using OpenCensus and Cloud Trace. While Bigtable surfaces a number of helpful server-side metrics through the Google Cloud operations suite, applications can realize added benefits by implementing client-side tracing and application defined metrics. For example, server-side metrics do not give you a window into the round-trip latency of calls made to your Bigtable endpoint and can only be surfaced using client-side tracing.
This tutorial uses the following billable components of Google Cloud:
- Compute Engine
- Cloud Trace
You can use the Pricing Calculator to generate a cost estimate based on your projected usage.
New Google Cloud 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 Cloud Console, go to the Manage resources page.
- Select a project, or click Create Project to create a new Google Cloud 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 Compute Engine and 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 Bigtable instance
In Cloud Shell, create a 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 Cloud Trace
Go to the Cloud 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 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 Google Cloud components, please be sure to delete the associated resources once you are done.