diff --git a/trace/pom.xml b/trace/pom.xml new file mode 100644 index 00000000000..3d951eabc3c --- /dev/null +++ b/trace/pom.xml @@ -0,0 +1,76 @@ + + + + 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 + + + + + + io.opencensus + opencensus-api + 0.12.2 + + + io.opencensus + opencensus-exporter-trace-stackdriver + 0.12.2 + + + io.opencensus + opencensus-impl + 0.12.2 + 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..767c41c41da --- /dev/null +++ b/trace/src/main/java/com/example/trace/TraceSample.java @@ -0,0 +1,103 @@ +/* + * 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(); + + GoogleCredentials credentials = + GoogleCredentials.create(new AccessToken(accessToken, expirationTime)); + StackdriverTraceExporter.createAndRegister( + StackdriverTraceConfiguration.builder() + .setProjectId("MyStackdriverProjectId") + .setCredentials(credentials) + .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..ae30f3ee083 --- /dev/null +++ b/trace/src/test/java/com/example/trace/TraceSampleIT.java @@ -0,0 +1,67 @@ +/* + * 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.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; + +/** + * Tests for stackdriver tracing sample. + */ +@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() { + 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(CLOUD_PROJECT_KEY)); + TraceSample.doWork(); + } +}