Matthias Andreas Benkard | 8090924 | 2022-02-03 20:47:47 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 5 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 6 | |
| 7 | import java.util.Collection; |
| 8 | |
| 9 | /** |
| 10 | * A user-supplied provider for {@link Label}s. |
| 11 | * |
Matthias Andreas Benkard | 2021024 | 2022-01-15 10:39:30 +0100 | [diff] [blame] | 12 | * <p>Instances of this interface that are registered with the {@link Formatter} are applied to each |
| 13 | * log entry that is logged. |
| 14 | * |
| 15 | * <p>If you are using the Quarkus extension, any CDI beans registered under this interface are |
| 16 | * registered automatically. |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 17 | * |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 18 | * <p><strong>Example:</strong> |
| 19 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 20 | * {@snippet : |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 21 | * @Singleton |
| 22 | * @Unremovable |
| 23 | * public final class RequestIdLabelProvider implements LabelProvider { |
| 24 | * |
| 25 | * @Override |
| 26 | * public Collection<Label> getLabels() { |
| 27 | * return List.of(Label.of("requestId", RequestContext.current().getRequestId())); |
| 28 | * } |
| 29 | * } |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 30 | * } |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 31 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 32 | * <p>Result: |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 33 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 34 | * {@snippet lang="json" : |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 35 | * { |
| 36 | * "textPayload": "Request rejected: unauthorized.", |
| 37 | * "labels": { |
| 38 | * "requestId": "123" |
| 39 | * } |
| 40 | * } |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 41 | * } |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 42 | * |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 43 | * @see StructuredParameterProvider |
| 44 | */ |
| 45 | public interface LabelProvider { |
| 46 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 47 | /** |
| 48 | * Provides a collection of {@link Label}s to add to each log entry that is logged. |
| 49 | * |
Matthias Andreas Benkard | 2cc18b3 | 2022-09-03 10:03:30 +0200 | [diff] [blame] | 50 | * <p>If {@link #getLabels(Context)} is implemented, this method is ignored. |
| 51 | * |
| 52 | * @return a collection of {@link Label}s to add to each log entry that is logged. |
| 53 | * @see #getLabels(Context) |
| 54 | */ |
| 55 | default Collection<Label> getLabels() { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Provides a collection of {@link Label}s to add to each log entry that is logged. |
| 61 | * |
| 62 | * <p>Delegates to {@link #getLabels()} by default. |
| 63 | * |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 64 | * @return a collection of {@link Label}s to add to each log entry that is logged. |
| 65 | */ |
Matthias Andreas Benkard | 2cc18b3 | 2022-09-03 10:03:30 +0200 | [diff] [blame] | 66 | default Collection<Label> getLabels(Context context) { |
| 67 | return getLabels(); |
| 68 | } |
| 69 | |
| 70 | /** Contextual data available to {@link #getLabels(Context)}. */ |
| 71 | interface Context extends ProviderContext {} |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 72 | } |