blob: d78f0d831b0b42a5866492acb0abe2a3629a0ec8 [file] [log] [blame]
Matthias Andreas Benkard80909242022-02-03 20:47:47 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkard82d7e442021-08-29 08:34:11 +02005package eu.mulk.quarkus.googlecloud.jsonlogging;
6
7/**
8 * A user-supplied provider for {@link StructuredParameter}s.
9 *
Matthias Andreas Benkard20210242022-01-15 10:39:30 +010010 * <p>Instances of this interface that are registered with the {@link Formatter} are applied to each
11 * log entry that is logged.
12 *
13 * <p>If you are using the Quarkus extension, any CDI beans registered under this interface are
14 * registered automatically.
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020015 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020016 * <p><strong>Example:</strong>
17 *
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020018 * {@snippet :
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020019 * @Singleton
20 * @Unremovable
21 * public final class TraceLogParameterProvider implements StructuredParameterProvider {
22 *
23 * @Override
24 * public StructuredParameter getParameter() {
25 * var b = Json.createObjectBuilder();
26 * b.add("traceId", Span.current().getSpanContext().getTraceId());
27 * b.add("spanId", Span.current().getSpanContext().getSpanId());
28 * return () -> b;
29 * }
30 * }
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020031 * }
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020032 *
33 * Result:
34 *
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020035 * {@snippet lang="json" :
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020036 * {
37 * "jsonPayload": {
38 * "message": "Request rejected: unauthorized.",
39 * "traceId": "39f9a49a9567a8bd7087b708f8932550",
40 * "spanId": "c7431b14630b633d"
41 * }
42 * }
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020043 * }
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020044 *
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020045 * @see LabelProvider
Matthias Andreas Benkard82d7e442021-08-29 08:34:11 +020046 */
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020047public interface StructuredParameterProvider {
Matthias Andreas Benkard82d7e442021-08-29 08:34:11 +020048
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020049 /**
50 * Provides a {@link StructuredParameter} to add to each log entry that is logged.
51 *
52 * <p>It is often useful to return a custom {@link StructuredParameter} rather than a {@link
53 * KeyValueParameter} from this method. This way multiple key–value pairs can be generated by a
54 * single invocation.
55 *
Matthias Andreas Benkard2cc18b32022-09-03 10:03:30 +020056 * <p>If {@link #getParameter(Context)} is implemented, this method is ignored.
57 *
58 * @return a {@link StructuredParameter} to add to each log entry that is logged.
59 * @see #getParameter(Context)
60 */
61 default StructuredParameter getParameter() {
62 return null;
63 }
64
65 /**
66 * Provides a {@link StructuredParameter} to add to each log entry that is logged.
67 *
68 * <p>It is often useful to return a custom {@link StructuredParameter} rather than a {@link
69 * KeyValueParameter} from this method. This way multiple key–value pairs can be generated by a
70 * single invocation.
71 *
72 * <p>Delegates to {@link #getParameter()} by default.
73 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020074 * @return a {@link StructuredParameter} to add to each log entry that is logged.
75 */
Matthias Andreas Benkard2cc18b32022-09-03 10:03:30 +020076 default StructuredParameter getParameter(Context context) {
77 return getParameter();
78 }
79
80 /** Contextual data available to {@link #getParameter(Context)}. */
81 interface Context extends ProviderContext {}
Matthias Andreas Benkard82d7e442021-08-29 08:34:11 +020082}