Edit on GitHub
Report issue
Page history

Using OpenCensus with Bigtable and Cloud Trace

Author(s): @googledrew ,   Published: 2019-05-17

Drew Stevens | Solutions Architect | Google

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
  • Bigtable
  • 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

  1. In the Cloud Console, go to the Manage resources page.
  2. Select a project, or click Create Project to create a new Google Cloud project.
  3. In the dialog, name your project. Make a note of your generated project ID.
  4. Click Create to create a new project.

Initialize the environment

  1. Start a Cloud Shell instance.

  2. 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 \

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 \

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 \

Deploying the Java application

  1. Log in to the VM

    gcloud compute ssh trace-client --zone=us-central1-c
  2. Run the following command to install Git, the Java 8 JDK, and Maven:

    sudo apt-get install git openjdk-8-jdk maven -y
  3. 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.

  1. Navigate to the folder containing the Java source:

    cd community/tutorials/bigtable-oc/java/
  2. Configure the environment variable for the application code to use the cbt-oc Bigtable instance:

    export INSTANCE_ID=cbt-oc
  3. 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

  1. Go to the Cloud Trace console

  2. 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

  1. Navigate to the Metrics Explorer.

  2. In the Find resource type and metric field, enter the following:

  3. Select this metric from the list.

  4. In the right pane, the distribution heatmap graph is shown.

Cleaning up

Since this tutorial uses multiple Google Cloud components, please be sure to delete the associated resources once you are done.

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.