From da265f76b23362ff84ffe6155f016d499e415e36 Mon Sep 17 00:00:00 2001 From: Dane Liergaard Date: Thu, 15 Nov 2018 13:49:07 -0800 Subject: [PATCH 1/3] Add Stackdriver Trace samples. --- trace/pom.xml | 77 ++++++++++++++ .../java/com/example/trace/TraceSample.java | 100 ++++++++++++++++++ .../java/com/example/trace/TraceSampleIT.java | 57 ++++++++++ 3 files changed, 234 insertions(+) create mode 100644 trace/pom.xml create mode 100644 trace/src/main/java/com/example/trace/TraceSample.java create mode 100644 trace/src/test/java/com/example/trace/TraceSampleIT.java diff --git a/trace/pom.xml b/trace/pom.xml new file mode 100644 index 00000000000..010a56ee13a --- /dev/null +++ b/trace/pom.xml @@ -0,0 +1,77 @@ + + + + 4.0.0 + jar + com.example + trace-samples + 1.0 + + + + com.google.cloud.samples + shared-configuration + 1.0.10 + + + + + 1.8 + 1.8 + UTF-8 + 0.12.2 + + + + + + io.opencensus + opencensus-api + ${opencensus.version} + + + io.opencensus + opencensus-exporter-trace-stackdriver + ${opencensus.version} + + + io.opencensus + opencensus-impl + ${opencensus.version} + runtime + + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.42 + test + + + + diff --git a/trace/src/main/java/com/example/trace/TraceSample.java b/trace/src/main/java/com/example/trace/TraceSample.java new file mode 100644 index 00000000000..718c378f112 --- /dev/null +++ b/trace/src/main/java/com/example/trace/TraceSample.java @@ -0,0 +1,100 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.trace; + +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; + +import io.opencensus.common.Scope; +import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration; +import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; +import io.opencensus.trace.Tracer; +import io.opencensus.trace.Tracing; +import io.opencensus.trace.samplers.Samplers; + +import java.io.IOException; +import java.util.Date; + +import org.joda.time.DateTime; + +public class TraceSample { + + // [START trace_setup_java_custom_span] + private static final Tracer tracer = Tracing.getTracer(); + + public static void doWork() { + // Create a child Span of the current Span. + try (Scope ss = tracer.spanBuilder("MyChildWorkSpan").startScopedSpan()) { + doInitialWork(); + tracer.getCurrentSpan().addAnnotation("Finished initial work"); + doFinalWork(); + } + } + + private static void doInitialWork() { + // ... + tracer.getCurrentSpan().addAnnotation("Doing initial work"); + // ... + } + + private static void doFinalWork() { + // ... + tracer.getCurrentSpan().addAnnotation("Hello world!"); + // ... + } + // [END trace_setup_java_custom_span] + + // [START trace_setup_java_full_sampling] + public static void doWorkFullSampled() { + try (Scope ss = tracer.spanBuilder("MyChildWorkSpan") + .setSampler(Samplers.alwaysSample()) + .startScopedSpan()) { + doInitialWork(); + tracer.getCurrentSpan().addAnnotation("Finished initial work"); + doFinalWork(); + } + } + // [END trace_setup_java_full_sampling] + + // [START trace_setup_java_create_and_register] + public static void createAndRegister() throws IOException { + StackdriverTraceExporter.createAndRegister( + StackdriverTraceConfiguration.builder().build()); + } + // [END trace_setup_java_create_and_register] + + // [START trace_setup_java_create_and_register_with_token] + public static void createAndRegisterWithToken(String accessToken) throws IOException { + Date expirationTime = DateTime.now().plusSeconds(60).toDate(); + + StackdriverTraceExporter.createAndRegister( + StackdriverTraceConfiguration.builder() + .setProjectId("MyStackdriverProjectId") + .setCredentials(GoogleCredentials.create(new AccessToken(accessToken, expirationTime))) + .build()); + } + // [END trace_setup_java_create_and_register_with_token] + + // [START trace_setup_java_register_exporter] + public static void createAndRegisterGoogleCloudPlatform(String projectId) throws IOException { + StackdriverTraceExporter.createAndRegister( + StackdriverTraceConfiguration.builder() + .setProjectId(projectId) + .build()); + } + // [END trace_setup_java_register_exporter] +} diff --git a/trace/src/test/java/com/example/trace/TraceSampleIT.java b/trace/src/test/java/com/example/trace/TraceSampleIT.java new file mode 100644 index 00000000000..08c803d7147 --- /dev/null +++ b/trace/src/test/java/com/example/trace/TraceSampleIT.java @@ -0,0 +1,57 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.trace; + +import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for stackdriver tracing sample. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TraceSampleIT { + + @After + public void tearDown() { + StackdriverTraceExporter.unregister(); + } + + @Test + public void testCreateAndRegister() throws IOException { + TraceSample.createAndRegister(); + TraceSample.doWork(); + } + + @Test + public void testCreateAndRegisterFullSampled() throws IOException { + TraceSample.createAndRegister(); + TraceSample.doWorkFullSampled(); + } + + @Test + public void testCreateAndRegisterGoogleCloudPlatform() throws IOException { + TraceSample.createAndRegisterGoogleCloudPlatform(System.getenv("GOOGLE_CLOUD_PROJECT")); + TraceSample.doWork(); + } +} From fe711ff58ce7ef2f86e84da2fa9df1ba23811566 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> Date: Mon, 26 Nov 2018 10:37:48 -0800 Subject: [PATCH 2/3] Update trace/src/main/java/com/example/trace/TraceSample.java Co-Authored-By: dzlier-gcp --- trace/src/main/java/com/example/trace/TraceSample.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/trace/src/main/java/com/example/trace/TraceSample.java b/trace/src/main/java/com/example/trace/TraceSample.java index 718c378f112..d5f8497c1ef 100644 --- a/trace/src/main/java/com/example/trace/TraceSample.java +++ b/trace/src/main/java/com/example/trace/TraceSample.java @@ -60,7 +60,11 @@ private static void doFinalWork() { // [START trace_setup_java_full_sampling] public static void doWorkFullSampled() { - try (Scope ss = tracer.spanBuilder("MyChildWorkSpan") + try ( + Scope ss = tracer.spanBuilder("MyChildWorkSpan") + .setSampler(Samplers.alwaysSample()) + .startScopedSpan(); + ) { .setSampler(Samplers.alwaysSample()) .startScopedSpan()) { doInitialWork(); From 0ec4f482268fc1eb293dc3e5d9598c2910878052 Mon Sep 17 00:00:00 2001 From: Dane Liergaard Date: Mon, 26 Nov 2018 10:41:46 -0800 Subject: [PATCH 3/3] Feedback updates to Trace sample. --- trace/pom.xml | 7 +++---- .../src/main/java/com/example/trace/TraceSample.java | 9 ++++----- .../test/java/com/example/trace/TraceSampleIT.java | 12 +++++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/trace/pom.xml b/trace/pom.xml index 010a56ee13a..3d951eabc3c 100644 --- a/trace/pom.xml +++ b/trace/pom.xml @@ -36,7 +36,6 @@ 1.8 1.8 UTF-8 - 0.12.2 @@ -44,17 +43,17 @@ io.opencensus opencensus-api - ${opencensus.version} + 0.12.2 io.opencensus opencensus-exporter-trace-stackdriver - ${opencensus.version} + 0.12.2 io.opencensus opencensus-impl - ${opencensus.version} + 0.12.2 runtime diff --git a/trace/src/main/java/com/example/trace/TraceSample.java b/trace/src/main/java/com/example/trace/TraceSample.java index d5f8497c1ef..767c41c41da 100644 --- a/trace/src/main/java/com/example/trace/TraceSample.java +++ b/trace/src/main/java/com/example/trace/TraceSample.java @@ -63,10 +63,7 @@ public static void doWorkFullSampled() { try ( Scope ss = tracer.spanBuilder("MyChildWorkSpan") .setSampler(Samplers.alwaysSample()) - .startScopedSpan(); - ) { - .setSampler(Samplers.alwaysSample()) - .startScopedSpan()) { + .startScopedSpan()) { doInitialWork(); tracer.getCurrentSpan().addAnnotation("Finished initial work"); doFinalWork(); @@ -85,10 +82,12 @@ public static void createAndRegister() throws IOException { public static void createAndRegisterWithToken(String accessToken) throws IOException { Date expirationTime = DateTime.now().plusSeconds(60).toDate(); + GoogleCredentials credentials = + GoogleCredentials.create(new AccessToken(accessToken, expirationTime)); StackdriverTraceExporter.createAndRegister( StackdriverTraceConfiguration.builder() .setProjectId("MyStackdriverProjectId") - .setCredentials(GoogleCredentials.create(new AccessToken(accessToken, expirationTime))) + .setCredentials(credentials) .build()); } // [END trace_setup_java_create_and_register_with_token] diff --git a/trace/src/test/java/com/example/trace/TraceSampleIT.java b/trace/src/test/java/com/example/trace/TraceSampleIT.java index 08c803d7147..ae30f3ee083 100644 --- a/trace/src/test/java/com/example/trace/TraceSampleIT.java +++ b/trace/src/test/java/com/example/trace/TraceSampleIT.java @@ -16,11 +16,15 @@ package com.example.trace; +import com.google.common.base.Strings; + import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; import java.io.IOException; import org.junit.After; +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,6 +35,12 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class TraceSampleIT { + private static final String CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT"; + + @BeforeClass + public static void setup() { + Assert.assertFalse(Strings.isNullOrEmpty(System.getenv(CLOUD_PROJECT_KEY))); + } @After public void tearDown() { @@ -51,7 +61,7 @@ public void testCreateAndRegisterFullSampled() throws IOException { @Test public void testCreateAndRegisterGoogleCloudPlatform() throws IOException { - TraceSample.createAndRegisterGoogleCloudPlatform(System.getenv("GOOGLE_CLOUD_PROJECT")); + TraceSample.createAndRegisterGoogleCloudPlatform(System.getenv(CLOUD_PROJECT_KEY)); TraceSample.doWork(); } }