From 82d7e44344cf5598bd2e93ae846ade8b5e7248ad Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 29 Aug 2021 08:34:11 +0200 Subject: Add ParameterProvider. Change-Id: Iae30648eb41761831840de0ce5617072e69928a4 --- deployment/pom.xml | 4 ++++ runtime/pom.xml | 4 ++++ .../mulk/quarkus/googlecloud/jsonlogging/Formatter.java | 15 +++++++++++++++ .../jsonlogging/GoogleCloudJsonLoggingRecorder.java | 6 +++++- .../googlecloud/jsonlogging/ParameterProvider.java | 12 ++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java diff --git a/deployment/pom.xml b/deployment/pom.xml index 5634341..f15bbc7 100644 --- a/deployment/pom.xml +++ b/deployment/pom.xml @@ -14,6 +14,10 @@ Quarkus Google Cloud JSON Logging Extension - Deployment + + io.quarkus + quarkus-arc-deployment + io.quarkus quarkus-core-deployment diff --git a/runtime/pom.xml b/runtime/pom.xml index 9ab1549..7661699 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -14,6 +14,10 @@ Quarkus Google Cloud JSON Logging Extension - Runtime + + io.quarkus + quarkus-arc + io.quarkus quarkus-core 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 d2e5562..c6e177c 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 @@ -3,6 +3,7 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,12 +29,26 @@ public class Formatter extends ExtFormatter { private static final String ERROR_EVENT_TYPE = "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"; + private final List parameterProviders; + + public Formatter(Collection parameterProviders) { + this.parameterProviders = List.copyOf(parameterProviders); + } + @Override public String format(ExtLogRecord logRecord) { var message = formatMessageWithStackTrace(logRecord); List parameters = new ArrayList<>(); Map labels = new HashMap<>(); + + for (var parameterProvider : parameterProviders) { + var parameter = parameterProvider.get(); + if (parameter != null) { + parameters.add(parameter); + } + } + if (logRecord.getParameters() != null) { for (var parameter : logRecord.getParameters()) { if (parameter instanceof StructuredParameter) { diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java index db2c2e9..ace4a28 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java @@ -1,12 +1,16 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; +import io.quarkus.arc.Arc; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; import java.util.Optional; +import java.util.stream.Collectors; @Recorder public class GoogleCloudJsonLoggingRecorder { public RuntimeValue> initialize() { - return new RuntimeValue<>(Optional.of(new Formatter())); + var parameterProviders = + Arc.container().select(ParameterProvider.class).stream().collect(Collectors.toList()); + return new RuntimeValue<>(Optional.of(new Formatter(parameterProviders))); } } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java new file mode 100644 index 0000000..fb212a3 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java @@ -0,0 +1,12 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging; + +/** + * A user-supplied provider for {@link StructuredParameter}s. + * + *

Any CDI beans registered under this class are applied to each log entry that is logged. + */ +public interface ParameterProvider { + + /** Provides a {@link StructuredParameter} to add to each log entry that is logged. */ + StructuredParameter get(); +} -- cgit v1.2.3