From c8144a9c60279f8dc81b2506794acd24df31b9a3 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Mon, 3 May 2021 08:04:53 +0200 Subject: Initial checkin: Quarkus Google Cloud JSON Logging. Change-Id: I264211f56c2bed4002ecdb6ead8a5321ada855fd --- runtime/pom.xml | 59 +++++++++++ .../jsonlogging/GoogleCloudLogEntry.java | 41 ++++++++ .../jsonlogging/GoogleCloudLoggingFormatter.java | 115 +++++++++++++++++++++ .../jsonlogging/GoogleCloudLoggingRecorder.java | 16 +++ .../googlecloud/jsonlogging/KeyValueParameter.java | 3 + .../googlecloud/jsonlogging/Label.java | 3 + .../main/resources/META-INF/quarkus-extension.yaml | 9 ++ 7 files changed, 246 insertions(+) create mode 100644 runtime/pom.xml create mode 100644 runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLogEntry.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingFormatter.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingRecorder.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/KeyValueParameter.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/Label.java create mode 100644 runtime/src/main/resources/META-INF/quarkus-extension.yaml (limited to 'runtime') diff --git a/runtime/pom.xml b/runtime/pom.xml new file mode 100644 index 0000000..e8254f9 --- /dev/null +++ b/runtime/pom.xml @@ -0,0 +1,59 @@ + + + + 4.0.0 + + + eu.mulk.quarkus-observability + quarkus-googlecloud-jsonlogging-parent + 1.0.0-SNAPSHOT + + quarkus-googlecloud-jsonlogging + Quarkus Google Cloud JSON Logging Extension - Runtime + + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-jsonb + + + + + + + io.quarkus + quarkus-bootstrap-maven-plugin + ${quarkus.version} + + + compile + + extension-descriptor + + + ${project.groupId}:${project.artifactId}-deployment:${project.version} + + + + + + maven-compiler-plugin + + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + + diff --git a/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLogEntry.java b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLogEntry.java new file mode 100644 index 0000000..3f5a836 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLogEntry.java @@ -0,0 +1,41 @@ +package eu.mulk.quarkus.observability.googlecloud.jsonlogging; + +import io.smallrye.common.constraint.Nullable; +import java.time.Instant; +import java.util.Map; +import javax.json.bind.annotation.JsonbProperty; + +/** + * A JSON log entry compatible with Google Cloud Logging. + * + *

Roughly (but not quite) corresponds to Google Cloud Logging's + * LogEntry + * structure. + */ +public record GoogleCloudLogEntry( + String getMessage, + String getSeverity, + Timestamp getTimestamp, + @Nullable String getTrace, + @Nullable String getSpanId, + @Nullable SourceLocation getSourceLocation, + @Nullable Map getLabels, + @Nullable Map getParameters, + @Nullable Map getMappedDiagnosticContext, + @Nullable String getNestedDiagnosticContext, + @Nullable @JsonbProperty("@type") String getType) { + + static public record SourceLocation( + @Nullable String getFile, + @Nullable String getLine, + @Nullable String getFunction) {} + + static public record Timestamp( + long getSeconds, + int getNanos) { + + public Timestamp(Instant t) { + this(t.getEpochSecond(), t.getNano()); + } + } +} diff --git a/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingFormatter.java b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingFormatter.java new file mode 100644 index 0000000..a44e8b5 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingFormatter.java @@ -0,0 +1,115 @@ +package eu.mulk.quarkus.observability.googlecloud.jsonlogging; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.logging.Level; +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbException; +import org.jboss.logmanager.ExtFormatter; +import org.jboss.logmanager.ExtLogRecord; + +/** + * Formats log records as JSON for consumption by Google Cloud Logging. + * + *

Meant to be used in containers running on Google Kubernetes Engine (GKE). + * + * @see GoogleCloudLogEntry + */ +class GoogleCloudLoggingFormatter extends ExtFormatter { + + private static final String TRACE_LEVEL = "TRACE"; + private static final String DEBUG_LEVEL = "DEBUG"; + private static final String INFO_LEVEL = "INFO"; + private static final String WARNING_LEVEL = "WARNING"; + private static final String ERROR_LEVEL = "ERROR"; + + private static final String ERROR_EVENT_TYPE = + "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"; + + private final Jsonb jsonb; + + GoogleCloudLoggingFormatter(Jsonb jsonb) { + this.jsonb = jsonb; + } + + @Override + public String format(ExtLogRecord logRecord) { + var message = formatMessageWithStackTrace(logRecord); + + var parameters = new HashMap(); + var labels = new HashMap(); + if (logRecord.getParameters() != null) { + for (var parameter : logRecord.getParameters()) { + if (parameter instanceof KeyValueParameter kvparam) { + parameters.put(kvparam.key(), kvparam.value()); + } else if (parameter instanceof Label label) { + labels.put(label.key(), label.value()); + } + } + } + + var mdc = logRecord.getMdcCopy(); + var ndc = logRecord.getNdc(); + + var sourceLocation = + new GoogleCloudLogEntry.SourceLocation( + logRecord.getSourceFileName(), String.valueOf(logRecord.getSourceLineNumber()), String.format("%s.%s", logRecord.getSourceClassName(), logRecord.getSourceMethodName())); + + var entry = + new GoogleCloudLogEntry( + message, + severityOf(logRecord.getLevel()), + new GoogleCloudLogEntry.Timestamp(logRecord.getInstant()), + null, + null, + sourceLocation, + labels.isEmpty() ? null : labels, + parameters.isEmpty() ? null : parameters, + mdc.isEmpty() ? null : mdc, + ndc.isEmpty() ? null : ndc, + logRecord.getLevel().intValue() >= 1000 ? ERROR_EVENT_TYPE : null); + + try { + return jsonb.toJson(entry) + "\n"; + } catch (JsonbException e) { + e.printStackTrace(); + return message + "\n"; + } + } + + /** + * Formats the log message corresponding to {@code logRecord} including a stack trace of the + * {@link ExtLogRecord#getThrown()} exception if any. + */ + private String formatMessageWithStackTrace(ExtLogRecord logRecord) { + var messageStringWriter = new StringWriter(); + var messagePrintWriter = new PrintWriter(messageStringWriter); + messagePrintWriter.append(this.formatMessage(logRecord)); + + if (logRecord.getThrown() != null) { + messagePrintWriter.println(); + logRecord.getThrown().printStackTrace(messagePrintWriter); + } + + messagePrintWriter.close(); + return messageStringWriter.toString(); + } + + /** + * Computes the Google Cloud Logging severity corresponding to a given {@link Level}. + */ + private static String severityOf(Level level) { + if (level.intValue() < 500) { + return TRACE_LEVEL; + } else if (level.intValue() < 700) { + return DEBUG_LEVEL; + } else if (level.intValue() < 900) { + return INFO_LEVEL; + } else if (level.intValue() < 1000) { + return WARNING_LEVEL; + } else { + return ERROR_LEVEL; + } + } +} diff --git a/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingRecorder.java b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingRecorder.java new file mode 100644 index 0000000..9ae3ae1 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/GoogleCloudLoggingRecorder.java @@ -0,0 +1,16 @@ +package eu.mulk.quarkus.observability.googlecloud.jsonlogging; + +import io.quarkus.runtime.RuntimeValue; +import io.quarkus.runtime.annotations.Recorder; +import java.util.Optional; +import java.util.logging.Formatter; +import javax.json.bind.spi.JsonbProvider; + +@Recorder +public class GoogleCloudLoggingRecorder { + + public RuntimeValue> initialize() { + var jsonb = JsonbProvider.provider().create().build(); + return new RuntimeValue<>(Optional.of(new GoogleCloudLoggingFormatter(jsonb))); + } +} diff --git a/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/KeyValueParameter.java b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/KeyValueParameter.java new file mode 100644 index 0000000..358e470 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/KeyValueParameter.java @@ -0,0 +1,3 @@ +package eu.mulk.quarkus.observability.googlecloud.jsonlogging; + +public record KeyValueParameter(String key, Object value) {} diff --git a/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/Label.java b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/Label.java new file mode 100644 index 0000000..0c13739 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/observability/googlecloud/jsonlogging/Label.java @@ -0,0 +1,3 @@ +package eu.mulk.quarkus.observability.googlecloud.jsonlogging; + +public record Label(String key, String value) {} diff --git a/runtime/src/main/resources/META-INF/quarkus-extension.yaml b/runtime/src/main/resources/META-INF/quarkus-extension.yaml new file mode 100644 index 0000000..7aa7a2a --- /dev/null +++ b/runtime/src/main/resources/META-INF/quarkus-extension.yaml @@ -0,0 +1,9 @@ +name: Google Cloud JSON Logging +description: Logs to standard output in Google-Cloud-compatible JSON format. +metadata: + keywords: + - logging + categories: + - "miscellaneous" + status: "preview" + # guide: ... -- cgit v1.2.3