Skip to content

Commit 7e59bf3

Browse files
authored
docs(sample): Add sample for Native Image support in Logging (#880)
* docs(sample): Add sample for Native Image support
1 parent cebc54e commit 7e59bf3

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-logging/tree/
302302

303303
| Sample | Source Code | Try it |
304304
| --------------------------- | --------------------------------- | ------ |
305+
| Native Image Logging Sample | [source code](https://github.com/googleapis/java-logging/blob/main/samples/native-image-sample/src/main/java/com/example/logging/NativeImageLoggingSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/logging/NativeImageLoggingSample.java) |
305306
| Get Sink Metadata | [source code](https://github.com/googleapis/java-logging/blob/main/samples/snippets/src/main/java/com/example/logging/GetSinkMetadata.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/logging/GetSinkMetadata.java) |
306307
| List Log Entries | [source code](https://github.com/googleapis/java-logging/blob/main/samples/snippets/src/main/java/com/example/logging/ListLogEntries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/logging/ListLogEntries.java) |
307308
| List Logs | [source code](https://github.com/googleapis/java-logging/blob/main/samples/snippets/src/main/java/com/example/logging/ListLogs.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-logging&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/logging/ListLogs.java) |

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Cloud Logging Client Libraries with Native Image
2+
3+
This application uses the Google Cloud [Logging Client Libraries](https://github.com/googleapis/java-logging) and can be compiled with Native Image Native Image.
4+
5+
## Setup Instructions
6+
7+
You will need to follow these prerequisite steps in order to run the samples:
8+
9+
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).
10+
11+
2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials.
12+
13+
Once installed, log in with Application Default Credentials using the following command:
14+
15+
```
16+
gcloud auth application-default login
17+
```
18+
19+
**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.
20+
21+
3. Install the GraalVM compiler.
22+
23+
You can follow the [official installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm) from the GraalVM website.
24+
After following the instructions, ensure that you install the native image extension installed by running:
25+
26+
```
27+
gu install native-image
28+
```
29+
30+
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.
31+
32+
You will see something similar to the below output:
33+
34+
```
35+
$ java -version
36+
37+
openjdk version "11.0.7" 2020-04-14
38+
OpenJDK Runtime Environment GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02)
39+
OpenJDK 64-Bit Server VM GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02, mixed mode, sharing)
40+
```
41+
42+
4. Enable the [Logging APIs](https://console.cloud.google.com/flows/enableapi?apiid=logging.googleapis.com).
43+
44+
## Sample
45+
46+
Navigate to this directory in a new terminal.
47+
48+
1. Compile the application using the Native Image Compiler. This step may take a few minutes.
49+
50+
```
51+
mvn package -P native -DskipTests
52+
```
53+
54+
2. Run the application:
55+
56+
```
57+
./target/native-image-sample
58+
```
59+
60+
3. The application will log a message to your local terminal and to Google Cloud Console.
61+
62+
Navigate to the [Cloud Console Logs Viewer](https://console.cloud.google.com/logs/viewer) to view you logs and find the newly generated log entry in Cloud Console:
63+
```
64+
This is a log produced by Native Image.
65+
```
66+
### Sample Integration test with Native Image Support
67+
In order to run the sample integration test as a native image, call the following command:
68+
69+
```
70+
mvn test -Pnative
71+
```

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

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.example.logging</groupId>
6+
<artifactId>native-image-sample</artifactId>
7+
<name>Native Image Sample</name>
8+
<url>https://github.com/googleapis/java-logging</url>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not affect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.2.0</version>
18+
</parent>
19+
20+
<properties>
21+
<!-- Java 8 because the Kokoro Sample test uses that Java version -->
22+
<maven.compiler.target>1.8</maven.compiler.target>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.google.cloud</groupId>
31+
<artifactId>libraries-bom</artifactId>
32+
<version>24.3.0</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-logging</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.13.2</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.google.truth</groupId>
53+
<artifactId>truth</artifactId>
54+
<version>1.1.3</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<!-- This plugin enables building the application to a JAR *not* using Native Image -->
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-jar-plugin</artifactId>
65+
<version>3.2.2</version>
66+
<configuration>
67+
<archive>
68+
<manifest>
69+
<addClasspath>true</addClasspath>
70+
<classpathPrefix>dependency-jars/</classpathPrefix>
71+
<mainClass>com.example.logging.NativeImageLoggingSample</mainClass>
72+
</manifest>
73+
</archive>
74+
</configuration>
75+
</plugin>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-dependency-plugin</artifactId>
79+
<version>3.2.0</version>
80+
<executions>
81+
<execution>
82+
<id>copy-dependencies</id>
83+
<phase>package</phase>
84+
<goals>
85+
<goal>copy-dependencies</goal>
86+
</goals>
87+
<configuration>
88+
<outputDirectory>
89+
${project.build.directory}/dependency-jars/
90+
</outputDirectory>
91+
</configuration>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
98+
<!-- Native Profile-->
99+
<profiles>
100+
<profile>
101+
<id>native</id>
102+
103+
<dependencies>
104+
<dependency>
105+
<groupId>com.google.cloud</groupId>
106+
<artifactId>native-image-support</artifactId>
107+
<version>0.12.0</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>org.junit.vintage</groupId>
111+
<artifactId>junit-vintage-engine</artifactId>
112+
<version>5.8.2</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.graalvm.buildtools</groupId>
117+
<artifactId>junit-platform-native</artifactId>
118+
<version>0.9.9</version>
119+
<scope>test</scope>
120+
</dependency>
121+
</dependencies>
122+
123+
<build>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-surefire-plugin
128+
</artifactId> <!-- Must use older version of surefire plugin for native-image testing. -->
129+
<version>2.22.2</version>
130+
<configuration>
131+
<includes>
132+
<include>**/IT*</include>
133+
</includes>
134+
</configuration>
135+
</plugin>
136+
<plugin>
137+
<groupId>org.graalvm.buildtools</groupId>
138+
<artifactId>native-maven-plugin</artifactId>
139+
<version>0.9.9</version>
140+
<extensions>true</extensions>
141+
<configuration>
142+
<mainClass>com.example.logging.NativeImageLoggingSample</mainClass>
143+
<buildArgs>
144+
<buildArg>--no-fallback</buildArg>
145+
<buildArg>--no-server</buildArg>
146+
</buildArgs>
147+
</configuration>
148+
<executions>
149+
<execution>
150+
<id>build-native</id>
151+
<goals>
152+
<goal>build</goal>
153+
<goal>test</goal>
154+
</goals>
155+
<phase>package</phase>
156+
</execution>
157+
<execution>
158+
<id>test-native</id>
159+
<goals>
160+
<goal>test</goal>
161+
</goals>
162+
<phase>test</phase>
163+
</execution>
164+
</executions>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
</profile>
169+
</profiles>
170+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2022 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 com.example.logging;
18+
19+
import com.google.cloud.MonitoredResource;
20+
import com.google.cloud.logging.LogEntry;
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.LoggingOptions;
23+
import com.google.cloud.logging.Payload.StringPayload;
24+
import com.google.cloud.logging.Severity;
25+
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.util.Collections;
28+
29+
/** Basic sample application which writes a log message to Cloud Logging. */
30+
public class NativeImageLoggingSample {
31+
32+
/** Runs the Logging Sample Application. */
33+
public static void main(String[] args) throws Exception {
34+
Instant startTime = Instant.now();
35+
// Instantiates a client
36+
Logging client = LoggingOptions.getDefaultInstance().getService();
37+
38+
// The name of the log to write to
39+
String logName = "nativeimage_logging_sample.log";
40+
41+
// The data to write to the log
42+
String text = "This is a log produced by Native Image.";
43+
44+
LogEntry entry =
45+
LogEntry.newBuilder(StringPayload.of(text))
46+
.setSeverity(Severity.INFO)
47+
.setLogName(logName)
48+
.setResource(MonitoredResource.newBuilder("global").build())
49+
.build();
50+
51+
client.write(Collections.singleton(entry));
52+
client.flush();
53+
client.close();
54+
55+
System.out.println(String.format("Logged: %s", text));
56+
System.out.println("Log message written to Cloud Logging.");
57+
System.out.println(
58+
"See your logs in the Cloud Console: https://console.cloud.google.com/logs/viewer "
59+
+ "(Might take a few seconds to load.)");
60+
Instant endTime = Instant.now();
61+
Duration duration = Duration.between(startTime, endTime);
62+
System.out.println("Duration: " + duration.toString());
63+
}
64+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 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 com.example.logging;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.testing.junit4.StdOutCaptureRule;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
25+
public class ITNativeImageLoggingSample {
26+
27+
@Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule();
28+
29+
@Test
30+
public void testLogging() throws Exception {
31+
NativeImageLoggingSample.main(new String[] {});
32+
assertThat(stdOut.getCapturedOutputAsUtf8String())
33+
.contains("Logged: This is a log produced by Native Image.");
34+
}
35+
}

‎samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<module>snapshot</module>
3232
<module>install-without-bom</module>
3333
<module>snippets</module>
34+
<module>native-image-sample</module>
3435
</modules>
3536

3637
<build>

0 commit comments

Comments
 (0)