Skip to content

Commit

Permalink
[Cloud Run] Add Structured Logging Sample (#1626)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
averikitsch authored Oct 22, 2019
1 parent 6d24381 commit 32302fe
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 11 deletions.
4 changes: 0 additions & 4 deletions run/hello-broken/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ RUN mvn compile assembly:single
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine

# [START run_system_package_alpine]
RUN apk --no-cache add graphviz ttf-ubuntu-font-family
# [END run_system_package_alpine]

# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/hello-broken-*dependencies.jar /hello-broken.jar

Expand Down
5 changes: 0 additions & 5 deletions run/hello-broken/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ limitations under the License.
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
18 changes: 16 additions & 2 deletions run/image-processing/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--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.-->
<!--
Copyright 2019 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 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cloudrun</groupId>
<artifactId>image-processing</artifactId>
<version>0.0.1-SNAPSHOT</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. -->
<!--
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>
Expand Down
36 changes: 36 additions & 0 deletions run/logging-manual/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 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
#
# https://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.

# Use the official maven/Java 11 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.6.2-jdk-11-slim as builder

# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src

# Build a release artifact.
RUN mvn compile assembly:single

# Use the Official OpenJDK image for a lean production stage of our multi-stage build.
# https://hub.docker.com/r/adoptopenjdk/openjdk11/
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine

# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/logging-manual-*dependencies.jar /logging-manual.jar

# Run the web service on container startup.
CMD ["java","-jar","/logging-manual.jar"]
17 changes: 17 additions & 0 deletions run/logging-manual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Cloud Run Manual Logging Sample

This sample shows how to send structured logs to Stackdriver Logging.

Read more about Cloud Run logging in the [Logging How-to Guide](http://cloud.google.com/run/docs/logging).

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).

[![Run in Google Cloud][run_img]][run_link]

[run_img]: https://storage.googleapis.com/cloudrun/button.svg
[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/logging-manual

## Dependencies

* **Spark**: Web server framework.
* **Junit**: [development] Test running framework.
89 changes: 89 additions & 0 deletions run/logging-manual/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 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 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cloudrun</groupId>
<artifactId>logging-manual</artifactId>
<version>0.0.1-SNAPSHOT</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.11</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- [START run_manual_logging_dep] -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- [END run_manual_logging_dep] -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.cloudrun.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
88 changes: 88 additions & 0 deletions run/logging-manual/src/main/java/com/example/cloudrun/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2019 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.cloudrun;

import static net.logstash.logback.argument.StructuredArguments.kv;
import static spark.Spark.get;
import static spark.Spark.port;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {

private static final Logger logger = LoggerFactory.getLogger(App.class);
private static final String project = getProjectId();

public static void main(String[] args) {
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
port(port);

get(
"/",
(req, res) -> {
// [START run_manual_logging]
// Build structured log messages as an object.
Object globalLogFields = null;
// Add log correlation to nest all log messages beneath request log in Log Viewer.
String traceHeader = req.headers("x-cloud-trace-context");
if (traceHeader != null && project != null) {
String trace = traceHeader.split("/")[0];
globalLogFields =
kv(
"logging.googleapis.com/trace",
String.format("projects/%s/traces/%s", project, trace));
}
// Create a structured log entry using key value pairs.
logger.error(
"This is the default display field.",
kv("component", "arbitrary-property"),
kv("severity", "NOTICE"),
globalLogFields);
// [END run_manual_logging]
res.status(200);
return "Hello Logger!";
});
}

// Load the project ID from GCP metadata server.
public static String getProjectId() {
OkHttpClient ok =
new OkHttpClient.Builder()
.readTimeout(500, TimeUnit.MILLISECONDS)
.writeTimeout(500, TimeUnit.MILLISECONDS)
.build();

String metadataUrl = "http://metadata.google.internal/computeMetadata/v1/project/project-id";
Request request =
new Request.Builder().url(metadataUrl).addHeader("Metadata-Flavor", "Google").get().build();

String project = null;
try {
Response response = ok.newCall(request).execute();
project = response.body().string();
} catch (IOException e) {
logger.error("Error getting Project Id", e);
}
return project;
}
}
21 changes: 21 additions & 0 deletions run/logging-manual/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- [START run_manual_logging_logback] -->
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<!-- Ignore default logging fields -->
<fieldNames>
<timestamp>[ignore]</timestamp>
<version>[ignore]</version>
<logger>[ignore]</logger>
<thread>[ignore]</thread>
<level>[ignore]</level>
<levelValue>[ignore]</levelValue>
</fieldNames>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</configuration>
<!-- [END run_manual_logging_logback] -->
90 changes: 90 additions & 0 deletions run/logging-manual/src/test/java/com/example/cloudrun/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2019 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.cloudrun;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static spark.Spark.awaitInitialization;
import static spark.Spark.stop;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import spark.utils.IOUtils;

public class AppTest {

private ByteArrayOutputStream bout;
private PrintStream out;

@BeforeClass
public static void beforeClass() {
App app = new App();
app.main(new String[] {});
awaitInitialization();
}

@AfterClass
public static void afterClass() {
stop();
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@Test
public void shouldSucceed() throws IOException {
TestResponse response = executeRequest("GET", "/");
assertEquals(200, response.status);
assertEquals("Hello Logger!", response.body);
String output = bout.toString();
assertTrue(output.toString().contains("This is the default display field."));
assertTrue(output.toString().contains("NOTICE"));
assertTrue(output.toString().contains("arbitrary-property"));
}

private static TestResponse executeRequest(String method, String path) throws IOException {
URL url = new URL("http://localhost:8080" + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
String body = IOUtils.toString(connection.getInputStream());
return new TestResponse(connection.getResponseCode(), body);
}

public static class TestResponse {

public final String body;
public final int status;

public TestResponse(int status, String body) {
this.status = status;
this.body = body;
}
}
}

0 comments on commit 32302fe

Please sign in to comment.