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 | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 5 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 6 | |
| 7 | import io.smallrye.common.constraint.Nullable; |
siddik.acil | c59bf52 | 2023-03-23 18:26:51 +0100 | [diff] [blame] | 8 | import jakarta.json.JsonObject; |
| 9 | import jakarta.json.JsonObjectBuilder; |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 10 | import jakarta.json.spi.JsonProvider; |
Matthias Andreas Benkard | 7fec6de | 2024-06-23 16:43:29 +0200 | [diff] [blame] | 11 | import java.time.Instant; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * A JSON log entry compatible with Google Cloud Logging. |
| 17 | * |
| 18 | * <p>Roughly (but not quite) corresponds to Google Cloud Logging's <a |
| 19 | * href="https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry">LogEntry</a> |
| 20 | * structure. |
| 21 | * |
Matthias Andreas Benkard | 596486a | 2021-08-30 12:14:43 +0200 | [diff] [blame] | 22 | * <p>A few of the fields are <a href="https://cloud.google.com/logging/docs/structured-logging"> |
| 23 | * treated specially</a> by the fluentd instance running in Google Kubernetes Engine. All other |
| 24 | * fields end up in the jsonPayload field on the Google Cloud Logging side. |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 25 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 26 | final class LogEntry { |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 27 | |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 28 | private final String message; |
| 29 | private final String severity; |
| 30 | private final Timestamp timestamp; |
| 31 | @Nullable private final String trace; |
| 32 | @Nullable private final String spanId; |
Matthias Andreas Benkard | 3af29c3 | 2024-06-25 22:31:04 +0200 | [diff] [blame^] | 33 | @Nullable private final SourceLocation sourceLocation; |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 34 | private final Map<String, String> labels; |
| 35 | private final List<StructuredParameter> parameters; |
| 36 | private final Map<String, String> mappedDiagnosticContext; |
| 37 | @Nullable private final String nestedDiagnosticContext; |
| 38 | @Nullable private final String type; |
| 39 | |
| 40 | LogEntry( |
| 41 | String message, |
| 42 | String severity, |
| 43 | Timestamp timestamp, |
| 44 | @Nullable String trace, |
| 45 | @Nullable String spanId, |
Matthias Andreas Benkard | 3af29c3 | 2024-06-25 22:31:04 +0200 | [diff] [blame^] | 46 | @Nullable SourceLocation sourceLocation, |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 47 | Map<String, String> labels, |
| 48 | List<StructuredParameter> parameters, |
| 49 | Map<String, String> mappedDiagnosticContext, |
| 50 | @Nullable String nestedDiagnosticContext, |
| 51 | @Nullable String type) { |
| 52 | this.message = message; |
| 53 | this.severity = severity; |
| 54 | this.timestamp = timestamp; |
| 55 | this.trace = trace; |
| 56 | this.spanId = spanId; |
| 57 | this.sourceLocation = sourceLocation; |
| 58 | this.labels = labels; |
| 59 | this.parameters = parameters; |
| 60 | this.mappedDiagnosticContext = mappedDiagnosticContext; |
| 61 | this.nestedDiagnosticContext = nestedDiagnosticContext; |
| 62 | this.type = type; |
| 63 | } |
| 64 | |
| 65 | static final class SourceLocation { |
| 66 | |
| 67 | @Nullable private final String file; |
| 68 | @Nullable private final String line; |
| 69 | @Nullable private final String function; |
| 70 | |
| 71 | SourceLocation(@Nullable String file, @Nullable String line, @Nullable String function) { |
| 72 | this.file = file; |
| 73 | this.line = line; |
| 74 | this.function = function; |
| 75 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 76 | |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 77 | JsonObject json(JsonProvider json) { |
| 78 | var b = json.createObjectBuilder(); |
Matthias Andreas Benkard | a78c957 | 2021-05-18 21:43:36 +0200 | [diff] [blame] | 79 | |
| 80 | if (file != null) { |
| 81 | b.add("file", file); |
| 82 | } |
| 83 | |
| 84 | if (line != null) { |
| 85 | b.add("line", line); |
| 86 | } |
| 87 | |
| 88 | if (function != null) { |
| 89 | b.add("function", function); |
| 90 | } |
| 91 | |
| 92 | return b.build(); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 96 | static final class Timestamp { |
| 97 | |
| 98 | private final long seconds; |
| 99 | private final int nanos; |
| 100 | |
| 101 | Timestamp(long seconds, int nanos) { |
| 102 | this.seconds = seconds; |
| 103 | this.nanos = nanos; |
| 104 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 105 | |
| 106 | Timestamp(Instant t) { |
| 107 | this(t.getEpochSecond(), t.getNano()); |
| 108 | } |
| 109 | |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 110 | JsonObject json(JsonProvider json) { |
| 111 | return json.createObjectBuilder().add("seconds", seconds).add("nanos", nanos).build(); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 115 | JsonObjectBuilder json(JsonProvider json) { |
| 116 | var b = json.createObjectBuilder(); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 117 | |
| 118 | if (trace != null) { |
Matthias Andreas Benkard | 596486a | 2021-08-30 12:14:43 +0200 | [diff] [blame] | 119 | b.add("logging.googleapis.com/trace", trace); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (spanId != null) { |
Matthias Andreas Benkard | 596486a | 2021-08-30 12:14:43 +0200 | [diff] [blame] | 123 | b.add("logging.googleapis.com/spanId", spanId); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (nestedDiagnosticContext != null && !nestedDiagnosticContext.isEmpty()) { |
| 127 | b.add("nestedDiagnosticContext", nestedDiagnosticContext); |
| 128 | } |
| 129 | |
| 130 | if (!labels.isEmpty()) { |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 131 | b.add("logging.googleapis.com/labels", jsonOfStringMap(json, labels)); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (type != null) { |
| 135 | b.add("@type", type); |
| 136 | } |
| 137 | |
Matthias Andreas Benkard | 3af29c3 | 2024-06-25 22:31:04 +0200 | [diff] [blame^] | 138 | b.add("message", message).add("severity", severity).add("timestamp", timestamp.json(json)); |
| 139 | |
| 140 | if (sourceLocation != null) { |
| 141 | b.add("logging.googleapis.com/sourceLocation", sourceLocation.json(json)); |
| 142 | } |
| 143 | |
| 144 | return b.addAll(jsonOfStringMap(json, mappedDiagnosticContext)) |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 145 | .addAll(jsonOfParameterMap(json, parameters)); |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 148 | private JsonObjectBuilder jsonOfStringMap(JsonProvider json, Map<String, String> stringMap) { |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 149 | return stringMap.entrySet().stream() |
| 150 | .reduce( |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 151 | json.createObjectBuilder(), |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 152 | (acc, x) -> acc.add(x.getKey(), x.getValue()), |
| 153 | JsonObjectBuilder::addAll); |
| 154 | } |
| 155 | |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 156 | private JsonObjectBuilder jsonOfParameterMap( |
| 157 | JsonProvider json, List<StructuredParameter> parameters) { |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 158 | return parameters.stream() |
| 159 | .reduce( |
Matthias Andreas Benkard | d2280ad | 2024-06-23 17:08:58 +0200 | [diff] [blame] | 160 | json.createObjectBuilder(), |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 161 | (acc, p) -> acc.addAll(p.json()), |
| 162 | JsonObjectBuilder::addAll); |
| 163 | } |
| 164 | } |