Skip to content

Commit

Permalink
Merge branch 'master' into pdt-search-ga
Browse files Browse the repository at this point in the history
  • Loading branch information
nirupa-kumar authored Nov 26, 2018
2 parents 7bc1cdd + 1612612 commit 1368cd2
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 0 deletions.
76 changes: 76 additions & 0 deletions trace/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<groupId>com.example</groupId>
<artifactId>trace-samples</artifactId>
<version>1.0</version>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.10</version>
<relativePath></relativePath>
</parent>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- [START trace_setup_java_maven ] -->
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>0.12.2</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>0.12.2</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>0.12.2</version>
<scope>runtime</scope>
</dependency>
<!-- [END trace_setup_java_maven ] -->

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.42</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
103 changes: 103 additions & 0 deletions trace/src/main/java/com/example/trace/TraceSample.java
Original file line number Diff line number Diff line change
@@ -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]
}
67 changes: 67 additions & 0 deletions trace/src/test/java/com/example/trace/TraceSampleIT.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 1368cd2

Please sign in to comment.