Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Commit 88695dc

Browse files
authored
docs(sample): adding native-image-sample for java-trace (#732)
Importing google-cloud-trace's sample to this repository. I confirmed it works as `samples/native-image-sample/README.md`.
1 parent 306a4bf commit 88695dc

File tree

6 files changed

+396
-1
lines changed

6 files changed

+396
-1
lines changed

‎README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies:
4949
If you are using Gradle 5.x or later, add this to your dependencies
5050

5151
```Groovy
52-
implementation platform('com.google.cloud:libraries-bom:24.3.0')
52+
implementation platform('com.google.cloud:libraries-bom:24.4.0')
5353
5454
implementation 'com.google.cloud:google-cloud-trace'
5555
```
@@ -100,6 +100,15 @@ use this Stackdriver Trace Client Library.
100100

101101

102102

103+
## Samples
104+
105+
Samples are in the [`samples/`](https://github.com/googleapis/java-trace/tree/main/samples) directory.
106+
107+
| Sample | Source Code | Try it |
108+
| --------------------------- | --------------------------------- | ------ |
109+
| Trace Sample Application | [source code](https://github.com/googleapis/java-trace/blob/main/samples/native-image-sample/src/main/java/trace/TraceSampleApplication.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-trace&page=editor&open_in_editor=samples/native-image-sample/src/main/java/trace/TraceSampleApplication.java) |
110+
111+
103112

104113
## Troubleshooting
105114

‎samples/native-image-sample/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Cloud Trace Client Libraries with Native Image
2+
3+
This application uses the Google Cloud [Trace Client Libraries](https://github.com/googleapis/java-trace) and can be compiled with Native Image.
4+
5+
**Note:** In practice, the Trace Client Libraries are not used directly but through another tool, such as through the [OpenCensus Cloud Trace integration](https://cloud.google.com/trace/docs/setup/java) or through a framework like Spring via [Spring Cloud GCP Trace](https://github.com/spring-cloud/spring-cloud-gcp/blob/master/docs/src/main/asciidoc/trace.adoc).
6+
7+
## Setup Instructions
8+
9+
You will need to follow these prerequisite steps in order to run these samples:
10+
11+
1. If you have not already, [create a Google Cloud Platform Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
12+
13+
2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials.
14+
15+
Once installed, log in with Application Default Credentials using the following command:
16+
17+
```
18+
gcloud auth application-default login
19+
```
20+
21+
**Note:** Authenticating with Application Default Credentials is convenient to use during development, but we recommend [alternate methods of authentication](https://cloud.google.com/docs/authentication/production) during production use.
22+
23+
3. Install the GraalVM compiler.
24+
25+
You can follow the [official installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm) from the GraalVM website.
26+
After following the instructions, ensure that you install the Native Image extension installed by running:
27+
28+
```
29+
gu install native-image
30+
```
31+
32+
Once you finish following the instructions, verify that the default version of Java is set to the GraalVM version by running `java -version` in a terminal.
33+
34+
You will see something similar to the below output:
35+
36+
```
37+
$ java -version
38+
39+
openjdk version "11.0.7" 2020-04-14
40+
OpenJDK Runtime Environment GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02)
41+
OpenJDK 64-Bit Server VM GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02, mixed mode, sharing)
42+
```
43+
44+
4. Enable the [Cloud Trace APIs](https://console.cloud.google.com/apis/api/cloudtrace.googleapis.com/overview).
45+
46+
## Run with Native Image Compilation
47+
48+
Navigate to this directory in a new terminal.
49+
50+
1. Compile the application using the Native Image Compiler. This step may take a few minutes.
51+
52+
```
53+
$ mvn package -P native -DskipTests
54+
```
55+
56+
2. Run the application:
57+
58+
```
59+
$ ./target/native-image-sample
60+
```
61+
62+
3. The application will generate a new trace and send the data to Cloud Trace where it will be viewable in the [Google Cloud Console Traces Viewer](https://console.cloud.google.com/traces/traces).
63+
64+
```
65+
Wait some time for the Trace to be populated.
66+
Retrieved trace: 1be886734c6a4053adc4346b2b9040c5
67+
It has the following spans:
68+
Span: nativeimage-trace-sample-test
69+
```
70+
71+
4. Run the test in native-image mode:
72+
73+
```
74+
$ mvn test -P native
75+
```

‎samples/native-image-sample/pom.xml

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example.trace</groupId>
7+
<artifactId>native-image-sample</artifactId>
8+
<version>0.1.0</version>
9+
<name>Native Image Sample</name>
10+
11+
12+
<!--
13+
The parent pom defines common style checks and testing strategies for our samples.
14+
Removing or replacing it should not affect the execution of the samples in anyway.
15+
-->
16+
<parent>
17+
<groupId>com.google.cloud.samples</groupId>
18+
<artifactId>shared-configuration</artifactId>
19+
<version>1.2.0</version>
20+
</parent>
21+
22+
<properties>
23+
<!-- Java 8 because the Kokoro Sample test uses that Java version -->
24+
<maven.compiler.target>1.8</maven.compiler.target>
25+
<maven.compiler.source>1.8</maven.compiler.source>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
</properties>
28+
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.cloud</groupId>
33+
<artifactId>libraries-bom</artifactId>
34+
<version>24.3.0</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>google-cloud-core</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-trace</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<version>4.13.2</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.truth</groupId>
58+
<artifactId>truth</artifactId>
59+
<version>1.1.3</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<!-- These plugins enable building the application to a JAR *not* using Native Image -->
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-jar-plugin</artifactId>
70+
<version>3.2.2</version>
71+
<configuration>
72+
<archive>
73+
<manifest>
74+
<addClasspath>true</addClasspath>
75+
<classpathPrefix>dependency-jars/</classpathPrefix>
76+
<mainClass>trace.TraceSampleApplication</mainClass>
77+
</manifest>
78+
</archive>
79+
</configuration>
80+
</plugin>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-dependency-plugin</artifactId>
84+
<version>3.2.0</version>
85+
<executions>
86+
<execution>
87+
<id>copy-dependencies</id>
88+
<phase>package</phase>
89+
<goals>
90+
<goal>copy-dependencies</goal>
91+
</goals>
92+
<configuration>
93+
<outputDirectory>
94+
${project.build.directory}/dependency-jars/
95+
</outputDirectory>
96+
</configuration>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
103+
<profiles>
104+
<profile>
105+
<id>native</id>
106+
<dependencies>
107+
<dependency>
108+
<!-- TODO: remove this when it's no longer needed -->
109+
<groupId>com.google.cloud</groupId>
110+
<artifactId>native-image-support</artifactId>
111+
<version>0.12.0</version>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.junit.vintage</groupId>
115+
<artifactId>junit-vintage-engine</artifactId>
116+
<version>5.8.2</version>
117+
</dependency>
118+
<dependency>
119+
<groupId>org.graalvm.buildtools</groupId>
120+
<artifactId>junit-platform-native</artifactId>
121+
<version>0.9.9</version>
122+
<scope>test</scope>
123+
</dependency>
124+
</dependencies>
125+
<build>
126+
<plugins>
127+
<plugin>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-surefire-plugin</artifactId>
130+
<!-- Must use older version of surefire plugin for native-image testing. -->
131+
<version>2.22.2</version>
132+
<configuration>
133+
<includes>
134+
<include>**/IT*</include>
135+
</includes>
136+
</configuration>
137+
</plugin>
138+
<plugin>
139+
<groupId>org.graalvm.buildtools</groupId>
140+
<artifactId>native-maven-plugin</artifactId>
141+
<version>0.9.9</version>
142+
<extensions>true</extensions>
143+
<configuration>
144+
<mainClass>trace.TraceSampleApplication</mainClass>
145+
<buildArgs>
146+
<buildArg>--no-fallback</buildArg>
147+
<buildArg>--no-server</buildArg>
148+
<buildArg>--initialize-at-build-time</buildArg>
149+
</buildArgs>
150+
</configuration>
151+
<executions>
152+
<execution>
153+
<id>build-native</id>
154+
<goals>
155+
<goal>build</goal>
156+
<goal>test</goal>
157+
</goals>
158+
<phase>package</phase>
159+
</execution>
160+
<execution>
161+
<id>test-native</id>
162+
<goals>
163+
<goal>test</goal>
164+
</goals>
165+
<phase>test</phase>
166+
</execution>
167+
</executions>
168+
</plugin>
169+
</plugins>
170+
</build>
171+
</profile>
172+
</profiles>
173+
</project>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2020-2021 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+
* https://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 trace;
18+
19+
import com.google.api.gax.rpc.NotFoundException;
20+
import com.google.cloud.ServiceOptions;
21+
import com.google.cloud.trace.v1.TraceServiceClient;
22+
import com.google.devtools.cloudtrace.v1.GetTraceRequest;
23+
import com.google.devtools.cloudtrace.v1.PatchTracesRequest;
24+
import com.google.devtools.cloudtrace.v1.Trace;
25+
import com.google.devtools.cloudtrace.v1.TraceSpan;
26+
import com.google.devtools.cloudtrace.v1.Traces;
27+
import com.google.protobuf.Timestamp;
28+
import java.io.IOException;
29+
import java.time.Instant;
30+
import java.util.UUID;
31+
32+
/** Sample application demonstrating using the Google Trace client libraries. */
33+
public class TraceSampleApplication {
34+
35+
/** Runs basic methods in the Trace client libraries. */
36+
public static void main(String[] args) throws IOException, InterruptedException {
37+
String projectId = ServiceOptions.getDefaultProjectId();
38+
TraceServiceClient traceServiceClient = TraceServiceClient.create();
39+
40+
// Create a trace in the current project.
41+
String traceId = UUID.randomUUID().toString().replaceAll("-", "");
42+
PatchTracesRequest createRequest = createPatchTraceRequest(traceId, projectId);
43+
traceServiceClient.patchTraces(createRequest);
44+
45+
// Wait for the trace to be populated in Cloud Trace.
46+
System.out.println("Wait some time for the Trace to be populated.");
47+
Thread.sleep(3000);
48+
49+
try {
50+
// This checks Cloud trace for the new trace that was just created.
51+
GetTraceRequest getTraceRequest =
52+
GetTraceRequest.newBuilder().setProjectId(projectId).setTraceId(traceId).build();
53+
54+
Trace trace = traceServiceClient.getTrace(getTraceRequest);
55+
56+
System.out.println("Retrieved trace: " + trace.getTraceId());
57+
System.out.println("It has the following spans: ");
58+
for (TraceSpan span : trace.getSpansList()) {
59+
System.out.println("Span: " + span.getName());
60+
}
61+
} catch (NotFoundException e) {
62+
System.out.println(
63+
"We didn't find the trace: "
64+
+ traceId
65+
+ ". "
66+
+ "This is usually because we did not wait long enough. "
67+
+ "Please check https://console.cloud.google.com/traces/traces to "
68+
+ "find your trace in the traces viewer.");
69+
}
70+
}
71+
72+
private static PatchTracesRequest createPatchTraceRequest(String traceId, String projectId) {
73+
long currentTime = Instant.now().toEpochMilli() / 1000;
74+
75+
Trace trace =
76+
Trace.newBuilder()
77+
.setProjectId(projectId)
78+
.setTraceId(traceId)
79+
.addSpans(
80+
TraceSpan.newBuilder()
81+
.setSpanId(1)
82+
.setName("nativeimage-trace-sample-test")
83+
.setStartTime(Timestamp.newBuilder().setSeconds(currentTime - 5))
84+
.setEndTime(Timestamp.newBuilder().setSeconds(currentTime)))
85+
.build();
86+
87+
PatchTracesRequest request =
88+
PatchTracesRequest.newBuilder()
89+
.setProjectId(projectId)
90+
.setTraces(Traces.newBuilder().addTraces(trace))
91+
.build();
92+
93+
return request;
94+
}
95+
}

0 commit comments

Comments
 (0)