Skip to content

Commit 0476e4d

Browse files
authored
Add tracing hello world (#270)
* Adding a Hello World Java with tracing. * Updating HelloWorld docs and readm * Addressing comments
1 parent 4fcd46c commit 0476e4d

File tree

4 files changed

+564
-0
lines changed

4 files changed

+564
-0
lines changed

java/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ limitations under the License.
3636
<module>dataproc-wordcount</module>
3737
<module>gae-flexible-helloworld</module>
3838
<module>hello-world</module>
39+
<module>tracing-hello-world</module>
3940
<module>metric-scaler</module>
4041
<module>simple-cli</module>
4142
<module>simple-performance-test</module>

java/tracing-hello-world/README.md

+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# Cloud Bigtable Hello World with Tracing
2+
3+
This application builds on top of the [basic Cloud Bigtable hello world example](../hello-world).
4+
This project adds automatic exports of [tracing to stackdriver](https://cloud.google.com/trace/) and
5+
[Z-Pages](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages) that
6+
show information about traces on the local machine.
7+
8+
See the [documentation for this
9+
sample](https://cloud.google.com/bigtable/docs/samples-java-hello) for a brief
10+
explanation of the hello world code.
11+
12+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
13+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
14+
15+
**Table of Contents**
16+
17+
- [Downloading the sample](#downloading-the-sample)
18+
- [Costs](#costs)
19+
- [Before you begin](#before-you-begin)
20+
- [Installing Maven](#installing-maven)
21+
- [Creating a Project in the Google Cloud Platform Console](#creating-a-project-in-the-google-cloud-platform-console)
22+
- [Enabling billing for your project.](#enabling-billing-for-your-project)
23+
- [Install the Google Cloud SDK.](#install-the-google-cloud-sdk)
24+
- [Setting Google Application Default Credentials](#setting-google-application-default-credentials)
25+
- [Provisioning an instance](#provisioning-an-instance)
26+
- [Running the application](#running-the-application)
27+
- [Cleaning up](#cleaning-up)
28+
29+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
30+
31+
32+
## Downloading the sample
33+
34+
Download the sample app and navigate into the app directory:
35+
36+
1. Clone the [Cloud Bigtable examples repository][github-repo], to your local
37+
machine:
38+
39+
git clone https://github.com/GoogleCloudPlatform/cloud-bigtable-examples.git
40+
41+
Alternatively, you can [download the sample][github-zip] as a zip file and
42+
extract it.
43+
44+
2. Change to the Hello World code sample directory.
45+
46+
cd cloud-bigtable-examples/java/tracing-hello-world
47+
48+
[github-repo]: https://github.com/GoogleCloudPlatform/cloud-bigtable-examples
49+
[github-zip]: https://github.com/GoogleCloudPlatform/cloud-bigtable-examples/archive/master.zip
50+
51+
52+
## Costs
53+
54+
This sample uses billable components of Cloud Platform, including:
55+
56+
+ Google Cloud Bigtable
57+
58+
Use the [Pricing Calculator][bigtable-pricing] to generate a cost estimate
59+
based on your projected usage. New Cloud Platform users might be eligible for
60+
a [free trial][free-trial].
61+
62+
[bigtable-pricing]: https://cloud.google.com/products/calculator/#id=1eb47664-13a2-4be1-9d16-6722902a7572
63+
[free-trial]: https://cloud.google.com/free-trial
64+
65+
66+
## Before you begin
67+
68+
This sample assumes you have [Java 8][java8] JDK installed.
69+
70+
[java8]: http://www.oracle.com/technetwork/java/javase/downloads/
71+
72+
### Installing Maven
73+
74+
These samples use the [Apache Maven][maven] build system. Before getting
75+
started, be sure to [download][maven-download] and [install][maven-install] it.
76+
When you use Maven as described here, it will automatically download the needed
77+
client libraries.
78+
79+
[maven]: https://maven.apache.org
80+
[maven-download]: https://maven.apache.org/download.cgi
81+
[maven-install]: https://maven.apache.org/install.html
82+
83+
### Creating a Project in the Google Cloud Platform Console
84+
85+
If you haven't already created a project, create one now. Projects enable you to
86+
manage all Google Cloud Platform resources for your app, including deployment,
87+
access control, billing, and services.
88+
89+
1. Open the [Cloud Platform Console][cloud-console].
90+
1. In the drop-down menu at the top, select **Create a project**.
91+
1. Give your project a name.
92+
1. Make a note of the project ID, which might be different from the project
93+
name. The project ID is used in commands and in configurations.
94+
95+
[cloud-console]: https://console.cloud.google.com/
96+
97+
### Enabling billing for your project.
98+
99+
If you haven't already enabled billing for your project, [enable
100+
billing][enable-billing] now. Enabling billing allows is required to use Cloud Bigtable
101+
and to create VM instances.
102+
103+
[enable-billing]: https://console.cloud.google.com/project/_/settings
104+
105+
### Install the Google Cloud SDK.
106+
107+
If you haven't already installed the Google Cloud SDK, [install the Google
108+
Cloud SDK][cloud-sdk] now. The SDK contains tools and libraries that enable you
109+
to create and manage resources on Google Cloud Platform.
110+
111+
[cloud-sdk]: https://cloud.google.com/sdk/
112+
113+
### Setting Google Application Default Credentials
114+
115+
Set your [Google Application Default
116+
Credentials][application-default-credentials] by [initializing the Google Cloud
117+
SDK][cloud-sdk-init] with the command:
118+
119+
gcloud init
120+
121+
Generate a credentials file by running the [application-default login](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login) command:
122+
123+
gcloud auth application-default login
124+
125+
[cloud-sdk-init]: https://cloud.google.com/sdk/docs/initializing
126+
[application-default-credentials]: https://developers.google.com/identity/protocols/application-default-credentials
127+
128+
129+
## Provisioning an instance
130+
131+
Follow the instructions in the [user
132+
documentation](https://cloud.google.com/bigtable/docs/creating-instance) to
133+
create a Google Cloud Platform project and Cloud Bigtable instance if necessary.
134+
You'll need to reference your project id and instance id to run the
135+
application.
136+
137+
138+
## Running the application
139+
140+
Build and run the sample using Maven.
141+
142+
mvn package
143+
mvn exec:java -Dbigtable.projectID=GCLOUDPROJECT -Dbigtable.instanceID=BIGTABLEINSTANCE
144+
145+
You will see output resembling the following, interspersed with informational logging
146+
from the underlying libraries:
147+
148+
HelloWorld: Create table Hello-Bigtable
149+
HelloWorld: Write some greetings to the table
150+
HelloWorld: Scan for all greetings:
151+
Hello World!
152+
Hello Cloud Bigtable!
153+
Hello HBase!
154+
HelloWorld: Delete the table
155+
156+
## View Z-Pages
157+
158+
Open [http://localhost:8080/tracez](http://localhost:8080/trace) to see a local rendering
159+
of trace information
160+
161+
## View Stackdriver tracing
162+
163+
Open [Stackriver traces](https://pantheon.corp.google.com/traces/traces) to see traces
164+
in the Google Cloud Console.
165+
166+
## Pom.xml
167+
168+
Setting up the [pom.xml](pom.xml) is as follows.
169+
170+
NOTE: cloud-bigtable-hbase-1.x-hadoop, cloud-bigtable-dataflow and cloud-bigtable-beam
171+
do not require this configuration, since they already include
172+
[shaded](https://maven.apache.org/plugins/maven-shade-plugin/) versions of tracing dependencies.
173+
174+
```xml
175+
176+
<!-- Opencensus dependencies. Pay close attention to the exclusions. -->
177+
178+
<!-- OpenCensus Java implementation -->
179+
<dependency>
180+
<groupId>io.opencensus</groupId>
181+
<artifactId>opencensus-impl</artifactId>
182+
<version>${opencensus.version}</version>
183+
<exclusions>
184+
<exclusion>
185+
<groupId>io.grpc</groupId>
186+
<artifactId>*</artifactId>
187+
</exclusion>
188+
</exclusions>
189+
</dependency>
190+
191+
<!-- Dependency for Z-Pages -->
192+
<dependency>
193+
<groupId>io.opencensus</groupId>
194+
<artifactId>opencensus-contrib-zpages</artifactId>
195+
<version>${opencensus.version}</version>
196+
<exclusions>
197+
<exclusion>
198+
<groupId>io.grpc</groupId>
199+
<artifactId>*</artifactId>
200+
</exclusion>
201+
</exclusions>
202+
</dependency>
203+
204+
<!-- Dependency to export traces to stackdriver -->
205+
<dependency>
206+
<groupId>io.opencensus</groupId>
207+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
208+
<version>${opencensus.version}</version>
209+
<exclusions>
210+
<exclusion>
211+
<groupId>io.grpc</groupId>
212+
<artifactId>*</artifactId>
213+
</exclusion>
214+
<exclusion>
215+
<groupId>io.netty</groupId>
216+
<artifactId>*</artifactId>
217+
</exclusion>
218+
</exclusions>
219+
</dependency>
220+
```
221+
222+
## Java code
223+
224+
Here is the code from [HelloWorld](src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java)
225+
that sets up tracing
226+
227+
```java
228+
229+
// Force tracing for every request for demo purposes.
230+
Tracing.getTraceConfig().updateActiveTraceParams(
231+
TraceParams.DEFAULT.toBuilder().setSampler(Samplers.probabilitySampler(1)).build());
232+
233+
// HBase Bigtable specific setup for zpages
234+
HBaseTracingUtilities.setupTracingConfig();
235+
236+
StackdriverExporter.createAndRegisterWithProjectId(projectId);
237+
238+
// Start a web server on port 8080 for tracing data
239+
ZPageHandlers.startHttpServerAndRegisterAll(8080);
240+
241+
doHelloWorld(projectId, instanceId);
242+
243+
System.out.println("Sleeping for 1 minute so that you can view http://localhost:8080/tracez");
244+
// Sleep for 1 minute.
245+
Thread.sleep(TimeUnit.MINUTES.toSeconds(1));
246+
247+
```
248+
249+
## Cleaning up
250+
251+
To avoid incurring extra charges to your Google Cloud Platform account, remove
252+
the resources created for this sample.
253+
254+
1. Go to the Clusters page in the [Cloud
255+
Console](https://console.cloud.google.com).
256+
257+
[Go to the Clusters page](https://console.cloud.google.com/project/_/bigtable/clusters)
258+
259+
1. Click the cluster name.
260+
261+
1. Click **Delete**.
262+
263+
![Delete](https://cloud.google.com/bigtable/img/delete-quickstart-cluster.png)
264+
265+
1. Type the cluster ID, then click **Delete** to delete the cluster.
266+

java/tracing-hello-world/pom.xml

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2017 Google Inc. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License"); you
6+
~ may not use this file except in compliance with the License. You may
7+
~ obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
~ implied. See the License for the specific language governing
15+
~ permissions and limitations under the License.
16+
-->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.bigtable.helloworld</groupId>
20+
<artifactId>cloud-bigtable-tracing-hello-world</artifactId>
21+
22+
<packaging>jar</packaging>
23+
<version>1.0-SNAPSHOT</version>
24+
<name>cloud-bigtable-tracing-hello-world</name>
25+
<url>http://maven.apache.org</url>
26+
27+
<parent>
28+
<groupId>com.google.cloud</groupId>
29+
<artifactId>bigtable-samples</artifactId>
30+
<version>1.0.0</version>
31+
<relativePath>..</relativePath>
32+
</parent>
33+
34+
<properties>
35+
<bigtable.version>1.0.0</bigtable.version>
36+
<opencensus.version>0.7.0</opencensus.version>
37+
<maven.compiler.target>1.8</maven.compiler.target>
38+
<maven.compiler.source>1.8</maven.compiler.source>
39+
</properties>
40+
41+
<repositories>
42+
<repository>
43+
<id>snapshots-repo</id>
44+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
45+
<releases><enabled>false</enabled></releases>
46+
<snapshots><enabled>true</enabled></snapshots>
47+
</repository>
48+
</repositories>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>com.google.cloud.bigtable</groupId>
53+
<artifactId>bigtable-hbase-1.x</artifactId>
54+
<version>${bigtable.version}</version>
55+
</dependency>
56+
57+
<!-- Opencensus dependencies. Pay close attention to the exclusions. -->
58+
59+
<!-- OpenCensus Java implementation -->
60+
<dependency>
61+
<groupId>io.opencensus</groupId>
62+
<artifactId>opencensus-impl</artifactId>
63+
<version>${opencensus.version}</version>
64+
<exclusions>
65+
<exclusion>
66+
<groupId>io.grpc</groupId>
67+
<artifactId>*</artifactId>
68+
</exclusion>
69+
</exclusions>
70+
</dependency>
71+
72+
<!-- Dependency for Z-Pages -->
73+
<dependency>
74+
<groupId>io.opencensus</groupId>
75+
<artifactId>opencensus-contrib-zpages</artifactId>
76+
<version>${opencensus.version}</version>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>io.grpc</groupId>
80+
<artifactId>*</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
</dependency>
84+
85+
<!-- Dependency to export traces to stackdriver -->
86+
<dependency>
87+
<groupId>io.opencensus</groupId>
88+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
89+
<version>${opencensus.version}</version>
90+
<exclusions>
91+
<exclusion>
92+
<groupId>io.grpc</groupId>
93+
<artifactId>*</artifactId>
94+
</exclusion>
95+
<exclusion>
96+
<groupId>io.netty</groupId>
97+
<artifactId>*</artifactId>
98+
</exclusion>
99+
</exclusions>
100+
</dependency>
101+
102+
</dependencies>
103+
104+
<build>
105+
<plugins>
106+
<plugin>
107+
<groupId>org.codehaus.mojo</groupId>
108+
<artifactId>exec-maven-plugin</artifactId>
109+
<version>1.5.0</version>
110+
<configuration>
111+
<mainClass>com.example.cloud.bigtable.helloworld.HelloWorld</mainClass>
112+
</configuration>
113+
</plugin>
114+
</plugins>
115+
</build>
116+
</project>

0 commit comments

Comments
 (0)