|
| 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 | +} |
0 commit comments