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 --- .../jsonlogging/GoogleCloudLogEntry.java | 41 ++++++++ .../jsonlogging/GoogleCloudLoggingFormatter.java | 115 +++++++++++++++++++++ .../jsonlogging/GoogleCloudLoggingRecorder.java | 16 +++ .../googlecloud/jsonlogging/KeyValueParameter.java | 3 + .../googlecloud/jsonlogging/Label.java | 3 + 5 files changed, 178 insertions(+) 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 (limited to 'runtime/src/main/java/eu') 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) {} -- cgit v1.2.3