From 692f48d300e84128ab16e4d2eababb0d7dc7b111 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 31 Aug 2021 21:06:50 +0200 Subject: Add LabelProvider, rename {=> Structured}ParameterProvider. Change-Id: Ib4902a35e22d023fc7e8dda4ca73ec2aab50695d --- .../quarkus/googlecloud/jsonlogging/Formatter.java | 19 +++++++++++++--- .../GoogleCloudJsonLoggingRecorder.java | 11 ++++++++-- .../googlecloud/jsonlogging/KeyValueParameter.java | 16 ++++++++++++++ .../quarkus/googlecloud/jsonlogging/Label.java | 15 +++++++++++++ .../googlecloud/jsonlogging/LabelProvider.java | 16 ++++++++++++++ .../googlecloud/jsonlogging/ParameterProvider.java | 12 ----------- .../jsonlogging/StructuredParameter.java | 25 ++++++++++++++++++++++ .../jsonlogging/StructuredParameterProvider.java | 14 ++++++++++++ .../googlecloud/jsonlogging/package-info.java | 5 +++++ 9 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.java delete mode 100644 runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.java create mode 100644 runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java (limited to 'runtime/src') 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 c6e177c..c8bb310 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 @@ -29,10 +29,14 @@ public class Formatter extends ExtFormatter { private static final String ERROR_EVENT_TYPE = "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"; - private final List parameterProviders; + private final List parameterProviders; + private final List labelProviders; - public Formatter(Collection parameterProviders) { + public Formatter( + Collection parameterProviders, + Collection labelProviders) { this.parameterProviders = List.copyOf(parameterProviders); + this.labelProviders = List.copyOf(labelProviders); } @Override @@ -43,12 +47,21 @@ public class Formatter extends ExtFormatter { Map labels = new HashMap<>(); for (var parameterProvider : parameterProviders) { - var parameter = parameterProvider.get(); + var parameter = parameterProvider.getParameter(); if (parameter != null) { parameters.add(parameter); } } + for (var labelProvider : labelProviders) { + var providedLabels = labelProvider.getLabels(); + if (providedLabels != null) { + for (var label : providedLabels) { + labels.put(label.key(), label.value()); + } + } + } + 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 ace4a28..a26f4da 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 @@ -6,11 +6,18 @@ import io.quarkus.runtime.annotations.Recorder; import java.util.Optional; import java.util.stream.Collectors; +/** A Quarkus recorder that registers {@link Formatter} as a log formatter for the application. */ @Recorder public class GoogleCloudJsonLoggingRecorder { public RuntimeValue> initialize() { + var parameterProviders = - Arc.container().select(ParameterProvider.class).stream().collect(Collectors.toList()); - return new RuntimeValue<>(Optional.of(new Formatter(parameterProviders))); + Arc.container().select(StructuredParameterProvider.class).stream() + .collect(Collectors.toList()); + + var labelProviders = + Arc.container().select(LabelProvider.class).stream().collect(Collectors.toList()); + + return new RuntimeValue<>(Optional.of(new Formatter(parameterProviders, labelProviders))); } } 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 ae2e7e0..173d9e2 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 @@ -7,6 +7,22 @@ import javax.json.Json; import javax.json.JsonObjectBuilder; import javax.json.JsonValue; +/** + * A simple single key--value pair forming a {@link StructuredParameter}. + * + *

This class is suitable for the common case of logging a key—value pair as parameter to the + * {@code *f} family of logging functions on {@link org.jboss.logging.Logger}. For advanced use + * cases, provide your own implementation of {@link StructuredParameter}. + * + *

Example: + * + *

{@code
+ * logger.infof("Application starting.", StructuredParameter.of("version", "1.0"));
+ * }
+ * + * @see Label + * @see StructuredParameter + */ public final class KeyValueParameter implements StructuredParameter { private final String key; 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 7c5d14d..72929f1 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 @@ -2,6 +2,21 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; import java.util.Objects; +/** + * A label usable to tag a log message. + * + *

Instances of {@link Label} can be passed as log parameters to the {@code *f} family of logging + * functions on {@link org.jboss.logging.Logger}. + * + *

Example: + * + *

{@code
+ * logger.logf("Request rejected: unauthorized.", Label.of("requestId", "123"));
+ * }
+ * + * @see KeyValueParameter + * @see StructuredParameter + */ public final class Label { private final String key; diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.java new file mode 100644 index 0000000..ef31bcc --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.java @@ -0,0 +1,16 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging; + +import java.util.Collection; + +/** + * A user-supplied provider for {@link Label}s. + * + *

Any CDI beans registered under this class are applied to each log entry that is logged. + * + * @see StructuredParameterProvider + */ +public interface LabelProvider { + + /** Provides a collection of {@link Label}s to add to each log entry that is logged. */ + Collection

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(); -} diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.java index 0b4a36e..fa326d5 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.java @@ -2,6 +2,31 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; import javax.json.JsonObjectBuilder; +/** + * A structured parameter usable as logging payload. + * + *

Any instance of {@link StructuredParameter} can be passed as a log parameter to the {@code *f} + * family of logging functions on {@link org.jboss.logging.Logger}. + * + *

Example: + * + *

{@code
+ * StructuredParameter p1 = ...;
+ * StructuredParameter p2 = ...;
+ *
+ * logger.logf("Something interesting happened.", p1, p2);
+ * }
+ * + * @see KeyValueParameter + * @see Label + */ public interface StructuredParameter { + + /** + * The JSON to be embedded in the log entry. + * + *

May contain multiple keys and values as well as nested objects. Each top-level entry of the + * returned object is embedded as a top-level entry in the log entry. + */ JsonObjectBuilder json(); } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.java new file mode 100644 index 0000000..d8ab39b --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.java @@ -0,0 +1,14 @@ +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. + * + * @see LabelProvider + */ +public interface StructuredParameterProvider { + + /** Provides a {@link StructuredParameter} to add to each log entry that is logged. */ + StructuredParameter getParameter(); +} diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java new file mode 100644 index 0000000..cbb6e16 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java @@ -0,0 +1,5 @@ +/** + * Provides structured logging to standard output according to the Google Cloud Logging + * specification. + */ +package eu.mulk.quarkus.googlecloud.jsonlogging; -- cgit v1.2.3