Edit on GitHub
Report issue
Page history

Using OpenCensus with Cloud Bigtable and Stackdriver Trace

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


This tutorial shows how to implement client-side tracing and metrics recording in your Cloud Bigtable workloads using OpenCensus and Stackdriver. While Cloud Bigtable surfaces a number of helpful server-side metrics via Stackdriver, 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.

Costs

This tutorial uses the following billable components of Google Cloud Platform (GCP):

  • Google Compute Engine
  • Cloud Bigtable
  • Stackdriver

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

  1. In the GCP Console, go to the Manage resources page.
  2. Select a project, or click Create Project to create a new GCP 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 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

  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 Stackdriver Trace

  1. Go to the Stackdriver 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 Stackdriver Metrics Explorer

  1. Navigate to the Metrics Explorer.

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

    opencensus/btappmetrics/write_latency
    
  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 GCP 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

GCP Tutorials

Tutorials published by GCP

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.