From 121a631215485e4f5ebb6e8eb0f81bdc7d8d4e64 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 12 May 2021 05:41:25 +0200 Subject: Downgrade to Java 11. Downgrades to Java 11 in order to support consumers that are not on Java 16 yet. Change-Id: Ia5930d1c40d0090ca145fd14c0b63a139e4ac970 --- pom.xml | 3 +- .../quarkus/googlecloud/jsonlogging/Formatter.java | 7 +- .../googlecloud/jsonlogging/KeyValueParameter.java | 37 ++++++++++- .../quarkus/googlecloud/jsonlogging/Label.java | 38 ++++++++++- .../quarkus/googlecloud/jsonlogging/LogEntry.java | 75 +++++++++++++++++----- 5 files changed, 138 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index f96e9d5..b2085ed 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ Quarkus Google Cloud JSON Logging Extension - Parent https://gerrit.benkard.de/plugins/gitiles/quarkus-googlecloud-jsonlogging + A Quarkus extension that logs to standard output in a JSON format compatible with Google Cloud Logging. @@ -46,7 +47,7 @@ true - 16 + 11 UTF-8 UTF-8 diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java index 0c691b7..d2e5562 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java @@ -36,9 +36,10 @@ public class Formatter extends ExtFormatter { Map labels = new HashMap<>(); if (logRecord.getParameters() != null) { for (var parameter : logRecord.getParameters()) { - if (parameter instanceof StructuredParameter sparam) { - parameters.add(sparam); - } else if (parameter instanceof Label label) { + if (parameter instanceof StructuredParameter) { + parameters.add((StructuredParameter) parameter); + } else if (parameter instanceof Label) { + var label = (Label) parameter; labels.put(label.key(), label.value()); } } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.java index 5f582c9..ae2e7e0 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.java @@ -2,11 +2,20 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Objects; import javax.json.Json; import javax.json.JsonObjectBuilder; import javax.json.JsonValue; -public record KeyValueParameter(String key, JsonValue value) implements StructuredParameter { +public final class KeyValueParameter implements StructuredParameter { + + private final String key; + private final JsonValue value; + + private KeyValueParameter(String key, JsonValue value) { + this.key = key; + this.value = value; + } public static KeyValueParameter of(String key, String value) { return new KeyValueParameter(key, Json.createValue(value)); @@ -40,4 +49,30 @@ public record KeyValueParameter(String key, JsonValue value) implements Structur public JsonObjectBuilder json() { return Json.createObjectBuilder().add(key, value); } + + public String key() { + return key; + } + + public JsonValue value() { + return value; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (KeyValueParameter) obj; + return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } + + @Override + public String toString() { + return "KeyValueParameter[" + "key=" + key + ", " + "value=" + value + ']'; + } } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Label.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Label.java index 02f7034..7c5d14d 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Label.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Label.java @@ -1,8 +1,44 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; -public record Label(String key, String value) { +import java.util.Objects; + +public final class Label { + + private final String key; + private final String value; + + private Label(String key, String value) { + this.key = key; + this.value = value; + } public static Label of(String key, String value) { return new Label(key, value); } + + public String key() { + return key; + } + + public String value() { + return value; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (Label) obj; + return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } + + @Override + public String toString() { + return "Label[" + "key=" + key + ", " + "value=" + value + ']'; + } } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java index 4394033..4c70e6f 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java @@ -18,21 +18,56 @@ import javax.json.JsonObjectBuilder; *

A few of the fields are treated specially by the fluentd instance running in Google Kubernetes * Engine. All other fields end up in the jsonPayload field on the Google Cloud Logging side. */ -record LogEntry( - String message, - String severity, - Timestamp timestamp, - @Nullable String trace, - @Nullable String spanId, - SourceLocation sourceLocation, - Map labels, - List parameters, - Map mappedDiagnosticContext, - @Nullable String nestedDiagnosticContext, - @Nullable String type) { - - static record SourceLocation( - @Nullable String file, @Nullable String line, @Nullable String function) { +final class LogEntry { + + private final String message; + private final String severity; + private final Timestamp timestamp; + @Nullable private final String trace; + @Nullable private final String spanId; + private final SourceLocation sourceLocation; + private final Map labels; + private final List parameters; + private final Map mappedDiagnosticContext; + @Nullable private final String nestedDiagnosticContext; + @Nullable private final String type; + + LogEntry( + String message, + String severity, + Timestamp timestamp, + @Nullable String trace, + @Nullable String spanId, + SourceLocation sourceLocation, + Map labels, + List parameters, + Map mappedDiagnosticContext, + @Nullable String nestedDiagnosticContext, + @Nullable String type) { + this.message = message; + this.severity = severity; + this.timestamp = timestamp; + this.trace = trace; + this.spanId = spanId; + this.sourceLocation = sourceLocation; + this.labels = labels; + this.parameters = parameters; + this.mappedDiagnosticContext = mappedDiagnosticContext; + this.nestedDiagnosticContext = nestedDiagnosticContext; + this.type = type; + } + + static final class SourceLocation { + + @Nullable private final String file; + @Nullable private final String line; + @Nullable private final String function; + + SourceLocation(@Nullable String file, @Nullable String line, @Nullable String function) { + this.file = file; + this.line = line; + this.function = function; + } JsonObject json() { return Json.createObjectBuilder() @@ -43,7 +78,15 @@ record LogEntry( } } - static record Timestamp(long seconds, int nanos) { + static final class Timestamp { + + private final long seconds; + private final int nanos; + + Timestamp(long seconds, int nanos) { + this.seconds = seconds; + this.nanos = nanos; + } Timestamp(Instant t) { this(t.getEpochSecond(), t.getNano()); -- cgit v1.2.3