Matthias Andreas Benkard | 82d7e44 | 2021-08-29 08:34:11 +0200 | [diff] [blame] | 1 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 2 | |
| 3 | /** |
| 4 | * A user-supplied provider for {@link StructuredParameter}s. |
| 5 | * |
Matthias Andreas Benkard | 2021024 | 2022-01-15 10:39:30 +0100 | [diff] [blame] | 6 | * <p>Instances of this interface that are registered with the {@link Formatter} are applied to each |
| 7 | * log entry that is logged. |
| 8 | * |
| 9 | * <p>If you are using the Quarkus extension, any CDI beans registered under this interface are |
| 10 | * registered automatically. |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 11 | * |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 12 | * <p><strong>Example:</strong> |
| 13 | * |
| 14 | * <pre>{@code |
| 15 | * @Singleton |
| 16 | * @Unremovable |
| 17 | * public final class TraceLogParameterProvider implements StructuredParameterProvider { |
| 18 | * |
| 19 | * @Override |
| 20 | * public StructuredParameter getParameter() { |
| 21 | * var b = Json.createObjectBuilder(); |
| 22 | * b.add("traceId", Span.current().getSpanContext().getTraceId()); |
| 23 | * b.add("spanId", Span.current().getSpanContext().getSpanId()); |
| 24 | * return () -> b; |
| 25 | * } |
| 26 | * } |
| 27 | * }</pre> |
| 28 | * |
| 29 | * Result: |
| 30 | * |
| 31 | * <pre>{@code |
| 32 | * { |
| 33 | * "jsonPayload": { |
| 34 | * "message": "Request rejected: unauthorized.", |
| 35 | * "traceId": "39f9a49a9567a8bd7087b708f8932550", |
| 36 | * "spanId": "c7431b14630b633d" |
| 37 | * } |
| 38 | * } |
| 39 | * }</pre> |
| 40 | * |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 41 | * @see LabelProvider |
Matthias Andreas Benkard | 82d7e44 | 2021-08-29 08:34:11 +0200 | [diff] [blame] | 42 | */ |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 43 | public interface StructuredParameterProvider { |
Matthias Andreas Benkard | 82d7e44 | 2021-08-29 08:34:11 +0200 | [diff] [blame] | 44 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Provides a {@link StructuredParameter} to add to each log entry that is logged. |
| 47 | * |
| 48 | * <p>It is often useful to return a custom {@link StructuredParameter} rather than a {@link |
| 49 | * KeyValueParameter} from this method. This way multiple key–value pairs can be generated by a |
| 50 | * single invocation. |
| 51 | * |
| 52 | * @return a {@link StructuredParameter} to add to each log entry that is logged. |
| 53 | */ |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 54 | StructuredParameter getParameter(); |
Matthias Andreas Benkard | 82d7e44 | 2021-08-29 08:34:11 +0200 | [diff] [blame] | 55 | } |