summaryrefslogtreecommitdiff
path: root/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.java
blob: 0298042e451d137cc4c9dd814ea0a652d328ea25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
//
// SPDX-License-Identifier: LGPL-3.0-or-later

package eu.mulk.quarkus.googlecloud.jsonlogging;

import java.util.Collection;

/**
 * A user-supplied provider for {@link Label}s.
 *
 * <p>Instances of this interface that are registered with the {@link Formatter} are applied to each
 * log entry that is logged.
 *
 * <p>If you are using the Quarkus extension, any CDI beans registered under this interface are
 * registered automatically.
 *
 * <p><strong>Example:</strong>
 *
 * {@snippet :
 * @Singleton
 * @Unremovable
 * public final class RequestIdLabelProvider implements LabelProvider {
 *
 *   @Override
 *   public Collection<Label> getLabels() {
 *     return List.of(Label.of("requestId", RequestContext.current().getRequestId()));
 *   }
 * }
 * }
 *
 * <p>Result:
 *
 * {@snippet lang="json" :
 * {
 *   "textPayload": "Request rejected: unauthorized.",
 *   "labels": {
 *     "requestId": "123"
 *   }
 * }
 * }
 *
 * @see StructuredParameterProvider
 */
public interface LabelProvider {

  /**
   * Provides a collection of {@link Label}s to add to each log entry that is logged.
   *
   * <p>If {@link #getLabels(Context)} is implemented, this method is ignored.
   *
   * @return a collection of {@link Label}s to add to each log entry that is logged.
   * @see #getLabels(Context)
   */
  default Collection<Label> getLabels() {
    return null;
  }

  /**
   * Provides a collection of {@link Label}s to add to each log entry that is logged.
   *
   * <p>Delegates to {@link #getLabels()} by default.
   *
   * @return a collection of {@link Label}s to add to each log entry that is logged.
   */
  default Collection<Label> getLabels(Context context) {
    return getLabels();
  }

  /** Contextual data available to {@link #getLabels(Context)}. */
  interface Context extends ProviderContext {}
}