Skip to content

Commit 4f2b0aa

Browse files
dietervdw-spotifysduskis
authored andcommitted
---
yaml --- r: 18637 b: refs/heads/autosynth-monitoring c: e9ed7b0 h: refs/heads/master i: 18635: 150534f
1 parent b948db2 commit 4f2b0aa

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
@@ -114,7 +114,7 @@ refs/tags/v0.66.0: ed6a3f57cbdaa20339a1995f7d7d53b172a5b8ef
114114
refs/tags/v0.67.0: 30b56f02092efc6f3c3667650ea8b8825003e0b7
115115
refs/heads/autosynth-compute: afe19e1678334117f3daf13b968b667857f7c8ca
116116
refs/heads/autosynth-container: f6384095f50b31bfc8208542a49181e158d3681c
117-
refs/heads/autosynth-monitoring: 6268cd1224257a4b1c9e9113168f1873361e8ac3
117+
refs/heads/autosynth-monitoring: e9ed7b0fba36cbe562cba08500bb4ae4a451040b
118118
refs/heads/autosynth-pubsub: fd363d13793a853214eb8193c922b28c54e7a6b3
119119
refs/heads/autosynth-video-intelligence: e44c9746407fe00fac42e1bb10ac50f493dd37b0
120120
refs/heads/autosynth-vision: b8e47d76578b5f150ef530072ea7e485e2b02ca0

branches/autosynth-monitoring/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/autosynth-monitoring/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)