-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add Stackdriver Trace samples. #1261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?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> | ||
| <opencensus.version>0.12.2</opencensus.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- [START trace_setup_java_maven ] --> | ||
| <dependency> | ||
| <groupId>io.opencensus</groupId> | ||
| <artifactId>opencensus-api</artifactId> | ||
| <version>${opencensus.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opencensus</groupId> | ||
| <artifactId>opencensus-exporter-trace-stackdriver</artifactId> | ||
| <version>${opencensus.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opencensus</groupId> | ||
| <artifactId>opencensus-impl</artifactId> | ||
| <version>${opencensus.version}</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> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
dzlier-gcp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .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))) | ||
dzlier-gcp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .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] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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")); | ||
dzlier-gcp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| TraceSample.doWork(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.