Skip to content

Commit 32302fe

Browse files
authored
[Cloud Run] Add Structured Logging Sample (GoogleCloudPlatform#1626)
* draft * Sample * fix formatting * Add tests * Add build tests for cloud run * Add to config * fix run command * fix build command * clean up * Fix relative path and make Cloud Run quiet. * remove comment * remove kokoro tests
1 parent 6d24381 commit 32302fe

File tree

9 files changed

+357
-11
lines changed

9 files changed

+357
-11
lines changed

run/hello-broken/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ RUN mvn compile assembly:single
3030
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
3131
FROM adoptopenjdk/openjdk11:alpine
3232

33-
# [START run_system_package_alpine]
34-
RUN apk --no-cache add graphviz ttf-ubuntu-font-family
35-
# [END run_system_package_alpine]
36-
3733
# Copy the jar to the production image from the builder stage.
3834
COPY --from=builder /app/target/hello-broken-*dependencies.jar /hello-broken.jar
3935

run/hello-broken/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ limitations under the License.
5151
<artifactId>slf4j-simple</artifactId>
5252
<version>1.6.4</version>
5353
</dependency>
54-
<dependency>
55-
<groupId>ch.qos.logback.contrib</groupId>
56-
<artifactId>logback-json-classic</artifactId>
57-
<version>0.1.5</version>
58-
</dependency>
5954
<dependency>
6055
<groupId>junit</groupId>
6156
<artifactId>junit</artifactId>

run/image-processing/pom.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!--Copyright 2019 Google LLCLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.-->
2+
<!--
3+
Copyright 2019 Google LLC
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
314
<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">
415
<modelVersion>4.0.0</modelVersion>
516
<groupId>com.example.cloudrun</groupId>
617
<artifactId>image-processing</artifactId>
718
<version>0.0.1-SNAPSHOT</version>
8-
<!-- 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. -->
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
923
<parent>
1024
<groupId>com.google.cloud.samples</groupId>
1125
<artifactId>shared-configuration</artifactId>

run/logging-manual/Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use the official maven/Java 11 image to create a build artifact.
16+
# https://hub.docker.com/_/maven
17+
FROM maven:3.6.2-jdk-11-slim as builder
18+
19+
# Copy local code to the container image.
20+
WORKDIR /app
21+
COPY pom.xml .
22+
COPY src ./src
23+
24+
# Build a release artifact.
25+
RUN mvn compile assembly:single
26+
27+
# Use the Official OpenJDK image for a lean production stage of our multi-stage build.
28+
# https://hub.docker.com/r/adoptopenjdk/openjdk11/
29+
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
30+
FROM adoptopenjdk/openjdk11:alpine
31+
32+
# Copy the jar to the production image from the builder stage.
33+
COPY --from=builder /app/target/logging-manual-*dependencies.jar /logging-manual.jar
34+
35+
# Run the web service on container startup.
36+
CMD ["java","-jar","/logging-manual.jar"]

run/logging-manual/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Cloud Run Manual Logging Sample
2+
3+
This sample shows how to send structured logs to Stackdriver Logging.
4+
5+
Read more about Cloud Run logging in the [Logging How-to Guide](http://cloud.google.com/run/docs/logging).
6+
7+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run).
8+
9+
[![Run in Google Cloud][run_img]][run_link]
10+
11+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
12+
[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/logging-manual
13+
14+
## Dependencies
15+
16+
* **Spark**: Web server framework.
17+
* **Junit**: [development] Test running framework.

run/logging-manual/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example.cloudrun</groupId>
17+
<artifactId>logging-manual</artifactId>
18+
<version>0.0.1-SNAPSHOT</version>
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
23+
<parent>
24+
<groupId>com.google.cloud.samples</groupId>
25+
<artifactId>shared-configuration</artifactId>
26+
<version>1.0.11</version>
27+
</parent>
28+
<properties>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<maven.compiler.source>11</maven.compiler.source>
31+
<maven.compiler.target>11</maven.compiler.target>
32+
</properties>
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.sparkjava</groupId>
36+
<artifactId>spark-core</artifactId>
37+
<version>2.8.0</version>
38+
</dependency>
39+
<!-- [START run_manual_logging_dep] -->
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>1.7.5</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>net.logstash.logback</groupId>
47+
<artifactId>logstash-logback-encoder</artifactId>
48+
<version>5.2</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>ch.qos.logback</groupId>
52+
<artifactId>logback-classic</artifactId>
53+
<version>1.2.3</version>
54+
</dependency>
55+
<!-- [END run_manual_logging_dep] -->
56+
<dependency>
57+
<groupId>com.squareup.okhttp3</groupId>
58+
<artifactId>okhttp</artifactId>
59+
<version>4.0.1</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>junit</groupId>
63+
<artifactId>junit</artifactId>
64+
<version>4.11</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>3.8.0</version>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-assembly-plugin</artifactId>
76+
<configuration>
77+
<archive>
78+
<manifest>
79+
<mainClass>com.example.cloudrun.App</mainClass>
80+
</manifest>
81+
</archive>
82+
<descriptorRefs>
83+
<descriptorRef>jar-with-dependencies</descriptorRef>
84+
</descriptorRefs>
85+
</configuration>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019 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+
* http://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.cloudrun;
18+
19+
import static net.logstash.logback.argument.StructuredArguments.kv;
20+
import static spark.Spark.get;
21+
import static spark.Spark.port;
22+
23+
import java.io.IOException;
24+
import java.util.concurrent.TimeUnit;
25+
import okhttp3.OkHttpClient;
26+
import okhttp3.Request;
27+
import okhttp3.Response;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
public class App {
32+
33+
private static final Logger logger = LoggerFactory.getLogger(App.class);
34+
private static final String project = getProjectId();
35+
36+
public static void main(String[] args) {
37+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
38+
port(port);
39+
40+
get(
41+
"/",
42+
(req, res) -> {
43+
// [START run_manual_logging]
44+
// Build structured log messages as an object.
45+
Object globalLogFields = null;
46+
// Add log correlation to nest all log messages beneath request log in Log Viewer.
47+
String traceHeader = req.headers("x-cloud-trace-context");
48+
if (traceHeader != null && project != null) {
49+
String trace = traceHeader.split("/")[0];
50+
globalLogFields =
51+
kv(
52+
"logging.googleapis.com/trace",
53+
String.format("projects/%s/traces/%s", project, trace));
54+
}
55+
// Create a structured log entry using key value pairs.
56+
logger.error(
57+
"This is the default display field.",
58+
kv("component", "arbitrary-property"),
59+
kv("severity", "NOTICE"),
60+
globalLogFields);
61+
// [END run_manual_logging]
62+
res.status(200);
63+
return "Hello Logger!";
64+
});
65+
}
66+
67+
// Load the project ID from GCP metadata server.
68+
public static String getProjectId() {
69+
OkHttpClient ok =
70+
new OkHttpClient.Builder()
71+
.readTimeout(500, TimeUnit.MILLISECONDS)
72+
.writeTimeout(500, TimeUnit.MILLISECONDS)
73+
.build();
74+
75+
String metadataUrl = "http://metadata.google.internal/computeMetadata/v1/project/project-id";
76+
Request request =
77+
new Request.Builder().url(metadataUrl).addHeader("Metadata-Flavor", "Google").get().build();
78+
79+
String project = null;
80+
try {
81+
Response response = ok.newCall(request).execute();
82+
project = response.body().string();
83+
} catch (IOException e) {
84+
logger.error("Error getting Project Id", e);
85+
}
86+
return project;
87+
}
88+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- [START run_manual_logging_logback] -->
3+
<configuration>
4+
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
6+
<!-- Ignore default logging fields -->
7+
<fieldNames>
8+
<timestamp>[ignore]</timestamp>
9+
<version>[ignore]</version>
10+
<logger>[ignore]</logger>
11+
<thread>[ignore]</thread>
12+
<level>[ignore]</level>
13+
<levelValue>[ignore]</levelValue>
14+
</fieldNames>
15+
</encoder>
16+
</appender>
17+
<root level="INFO">
18+
<appender-ref ref="jsonConsoleAppender"/>
19+
</root>
20+
</configuration>
21+
<!-- [END run_manual_logging_logback] -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2019 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+
* http://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.cloudrun;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
import static spark.Spark.awaitInitialization;
22+
import static spark.Spark.stop;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import java.net.HttpURLConnection;
28+
import java.net.URL;
29+
import org.junit.AfterClass;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import spark.utils.IOUtils;
34+
35+
public class AppTest {
36+
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
40+
@BeforeClass
41+
public static void beforeClass() {
42+
App app = new App();
43+
app.main(new String[] {});
44+
awaitInitialization();
45+
}
46+
47+
@AfterClass
48+
public static void afterClass() {
49+
stop();
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@Test
60+
public void shouldSucceed() throws IOException {
61+
TestResponse response = executeRequest("GET", "/");
62+
assertEquals(200, response.status);
63+
assertEquals("Hello Logger!", response.body);
64+
String output = bout.toString();
65+
assertTrue(output.toString().contains("This is the default display field."));
66+
assertTrue(output.toString().contains("NOTICE"));
67+
assertTrue(output.toString().contains("arbitrary-property"));
68+
}
69+
70+
private static TestResponse executeRequest(String method, String path) throws IOException {
71+
URL url = new URL("http://localhost:8080" + path);
72+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
73+
connection.setRequestMethod(method);
74+
connection.setDoOutput(true);
75+
connection.connect();
76+
String body = IOUtils.toString(connection.getInputStream());
77+
return new TestResponse(connection.getResponseCode(), body);
78+
}
79+
80+
public static class TestResponse {
81+
82+
public final String body;
83+
public final int status;
84+
85+
public TestResponse(int status, String body) {
86+
this.status = status;
87+
this.body = body;
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)