Skip to content

Commit 1612612

Browse files
authored
Add Stackdriver Trace samples. (#1261)
* Add Stackdriver Trace samples. * Update trace/src/main/java/com/example/trace/TraceSample.java Co-Authored-By: dzlier-gcp <[email protected]> * Feedback updates to Trace sample.
1 parent 38e398e commit 1612612

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

trace/pom.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2018 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may 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 implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>jar</packaging>
20+
<groupId>com.example</groupId>
21+
<artifactId>trace-samples</artifactId>
22+
<version>1.0</version>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.10</version>
32+
<relativePath></relativePath>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<maven.compiler.target>1.8</maven.compiler.target>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
</properties>
40+
41+
<dependencies>
42+
<!-- [START trace_setup_java_maven ] -->
43+
<dependency>
44+
<groupId>io.opencensus</groupId>
45+
<artifactId>opencensus-api</artifactId>
46+
<version>0.12.2</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.opencensus</groupId>
50+
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
51+
<version>0.12.2</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.opencensus</groupId>
55+
<artifactId>opencensus-impl</artifactId>
56+
<version>0.12.2</version>
57+
<scope>runtime</scope>
58+
</dependency>
59+
<!-- [END trace_setup_java_maven ] -->
60+
61+
<!-- Test dependencies -->
62+
<dependency>
63+
<groupId>junit</groupId>
64+
<artifactId>junit</artifactId>
65+
<version>4.12</version>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.google.truth</groupId>
70+
<artifactId>truth</artifactId>
71+
<version>0.42</version>
72+
<scope>test</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.trace;
18+
19+
import com.google.auth.oauth2.AccessToken;
20+
import com.google.auth.oauth2.GoogleCredentials;
21+
22+
import io.opencensus.common.Scope;
23+
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
24+
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
25+
import io.opencensus.trace.Tracer;
26+
import io.opencensus.trace.Tracing;
27+
import io.opencensus.trace.samplers.Samplers;
28+
29+
import java.io.IOException;
30+
import java.util.Date;
31+
32+
import org.joda.time.DateTime;
33+
34+
public class TraceSample {
35+
36+
// [START trace_setup_java_custom_span]
37+
private static final Tracer tracer = Tracing.getTracer();
38+
39+
public static void doWork() {
40+
// Create a child Span of the current Span.
41+
try (Scope ss = tracer.spanBuilder("MyChildWorkSpan").startScopedSpan()) {
42+
doInitialWork();
43+
tracer.getCurrentSpan().addAnnotation("Finished initial work");
44+
doFinalWork();
45+
}
46+
}
47+
48+
private static void doInitialWork() {
49+
// ...
50+
tracer.getCurrentSpan().addAnnotation("Doing initial work");
51+
// ...
52+
}
53+
54+
private static void doFinalWork() {
55+
// ...
56+
tracer.getCurrentSpan().addAnnotation("Hello world!");
57+
// ...
58+
}
59+
// [END trace_setup_java_custom_span]
60+
61+
// [START trace_setup_java_full_sampling]
62+
public static void doWorkFullSampled() {
63+
try (
64+
Scope ss = tracer.spanBuilder("MyChildWorkSpan")
65+
.setSampler(Samplers.alwaysSample())
66+
.startScopedSpan()) {
67+
doInitialWork();
68+
tracer.getCurrentSpan().addAnnotation("Finished initial work");
69+
doFinalWork();
70+
}
71+
}
72+
// [END trace_setup_java_full_sampling]
73+
74+
// [START trace_setup_java_create_and_register]
75+
public static void createAndRegister() throws IOException {
76+
StackdriverTraceExporter.createAndRegister(
77+
StackdriverTraceConfiguration.builder().build());
78+
}
79+
// [END trace_setup_java_create_and_register]
80+
81+
// [START trace_setup_java_create_and_register_with_token]
82+
public static void createAndRegisterWithToken(String accessToken) throws IOException {
83+
Date expirationTime = DateTime.now().plusSeconds(60).toDate();
84+
85+
GoogleCredentials credentials =
86+
GoogleCredentials.create(new AccessToken(accessToken, expirationTime));
87+
StackdriverTraceExporter.createAndRegister(
88+
StackdriverTraceConfiguration.builder()
89+
.setProjectId("MyStackdriverProjectId")
90+
.setCredentials(credentials)
91+
.build());
92+
}
93+
// [END trace_setup_java_create_and_register_with_token]
94+
95+
// [START trace_setup_java_register_exporter]
96+
public static void createAndRegisterGoogleCloudPlatform(String projectId) throws IOException {
97+
StackdriverTraceExporter.createAndRegister(
98+
StackdriverTraceConfiguration.builder()
99+
.setProjectId(projectId)
100+
.build());
101+
}
102+
// [END trace_setup_java_register_exporter]
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.trace;
18+
19+
import com.google.common.base.Strings;
20+
21+
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
22+
23+
import java.io.IOException;
24+
25+
import org.junit.After;
26+
import org.junit.Assert;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/**
33+
* Tests for stackdriver tracing sample.
34+
*/
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class TraceSampleIT {
38+
private static final String CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
39+
40+
@BeforeClass
41+
public static void setup() {
42+
Assert.assertFalse(Strings.isNullOrEmpty(System.getenv(CLOUD_PROJECT_KEY)));
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
StackdriverTraceExporter.unregister();
48+
}
49+
50+
@Test
51+
public void testCreateAndRegister() throws IOException {
52+
TraceSample.createAndRegister();
53+
TraceSample.doWork();
54+
}
55+
56+
@Test
57+
public void testCreateAndRegisterFullSampled() throws IOException {
58+
TraceSample.createAndRegister();
59+
TraceSample.doWorkFullSampled();
60+
}
61+
62+
@Test
63+
public void testCreateAndRegisterGoogleCloudPlatform() throws IOException {
64+
TraceSample.createAndRegisterGoogleCloudPlatform(System.getenv(CLOUD_PROJECT_KEY));
65+
TraceSample.doWork();
66+
}
67+
}

0 commit comments

Comments
 (0)