Skip to content

Commit 4411a9f

Browse files
dietervdw-spotifysduskis
authored andcommitted
---
yaml --- r: 26055 b: refs/heads/pubsub-ordering-keys c: e9ed7b0 h: refs/heads/master i: 26053: 1c8f802 26051: 762a685 26047: 8cf7d3e
1 parent 61178bf commit 4411a9f

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ refs/tags/v0.72.0: a7703f2593ba312c0b2dde6fdfd4f5c764bb55ac
155155
refs/tags/v0.73.0: 21241ea8be9439cc5764c4944cdce21d34ce4f9e
156156
refs/tags/v0.74.0: 9d1f733dbbf790de7b494418523b69c4a9a57638
157157
refs/heads/ignoretest: 23c412ae07af3d0ab1caa2d44d5bc5c0ccb8b31d
158-
refs/heads/pubsub-ordering-keys: 6268cd1224257a4b1c9e9113168f1873361e8ac3
158+
refs/heads/pubsub-ordering-keys: e9ed7b0fba36cbe562cba08500bb4ae4a451040b
159159
"refs/heads/update_mvn_badge": ae2d773814db0f71197ccf5a8612ee1d8056f8de
160160
refs/tags/v0.75.0: c3673089ae09a897c1b4cf7dfe167fe4f8ab32fb
161161
refs/tags/v0.76.0: 395b016826d3ddf9cb8b34919636df15a4dbd032

branches/pubsub-ordering-keys/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/logback/LoggingAppender.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Collections;
3737
import java.util.HashSet;
3838
import java.util.List;
39+
import java.util.Map;
3940
import java.util.Set;
4041

4142
/**
@@ -232,6 +233,10 @@ private LogEntry logEntryFor(ILoggingEvent e) {
232233
.addLabel(LEVEL_NAME_KEY, level.toString())
233234
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()));
234235

236+
for (Map.Entry<String, String> entry : e.getMDCPropertyMap().entrySet()) {
237+
builder.addLabel(entry.getKey(), entry.getValue());
238+
}
239+
235240
if (loggingEnhancers != null) {
236241
for (LoggingEnhancer enhancer : loggingEnhancers) {
237242
enhancer.enhanceLogEntry(builder);

branches/pubsub-ordering-keys/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/logback/LoggingAppenderTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,36 @@ public void testDefaultWriteOptionsHasExpectedDefaults() {
181181
// assertThat(resourceArg.getValue()).isEqualTo(defaultWriteOptions[1]);
182182
}
183183

184+
@Test
185+
public void testMdcValuesAreConvertedToLabels() {
186+
LogEntry logEntry =
187+
LogEntry.newBuilder(StringPayload.of("this is a test"))
188+
.setTimestamp(100000L)
189+
.setSeverity(Severity.INFO)
190+
.setLabels(
191+
new ImmutableMap.Builder<String, String>()
192+
.put("levelName", "INFO")
193+
.put("levelValue", String.valueOf(20000L))
194+
.put("mdc1", "value1")
195+
.put("mdc2", "value2")
196+
.build())
197+
.build();
198+
logging.setFlushSeverity(Severity.ERROR);
199+
Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
200+
logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
201+
expectLastCall().once();
202+
replay(logging);
203+
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
204+
LoggingEvent loggingEvent = createLoggingEvent(Level.INFO, timestamp.getSeconds());
205+
loggingEvent.setMDCPropertyMap(ImmutableMap.of("mdc1", "value1", "mdc2", "value2"));
206+
loggingAppender.start();
207+
// info event does not get logged
208+
loggingAppender.doAppend(loggingEvent);
209+
verify(logging);
210+
assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue();
211+
assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry);
212+
}
213+
184214
private LoggingEvent createLoggingEvent(Level level, long timestamp) {
185215
LoggingEvent loggingEvent = new LoggingEvent();
186216
loggingEvent.setMessage("this is a test");

0 commit comments

Comments
 (0)