forked from dbMundada/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logging): add ability to filter MDC labels (googleapis#6430)
Currently all MDC values are being written as labels in a LogEntry. This is not desirable for everyone. By adding MDCEventEnhancer a user now has the ability to easily filter which MDC entries get converted into labels. To maintain backwards compatability the default implementation is to write all entries as labels. If a user wants to filter these, they just need to extend MDCEventEnhancer, override the filtering, and add the new classpath to their logback.xml. Fixes googleapis#6320
- Loading branch information
Showing
4 changed files
with
142 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...loud-logging-logback/src/main/java/com/google/cloud/logging/logback/MDCEventEnhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.logging.logback; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import com.google.cloud.logging.LogEntry; | ||
import java.util.Map; | ||
|
||
/** | ||
* MDCEventEnhancer takes values found in the MDC property map and adds them as labels to the {@link | ||
* LogEntry}. This {@link LoggingEventEnhancer} is turned on by default. If you wish to filter which | ||
* MDC values get added as labels to your {@link LogEntry}, implement a {@link LoggingEventEnhancer} | ||
* and add its classpath to your {@code logback.xml}. If any {@link LoggingEventEnhancer} is added | ||
* this class is no longer registered. | ||
*/ | ||
final class MDCEventEnhancer implements LoggingEventEnhancer { | ||
|
||
@Override | ||
public final void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent e) { | ||
for (Map.Entry<String, String> entry : e.getMDCPropertyMap().entrySet()) { | ||
if (null != entry.getKey() && null != entry.getValue()) { | ||
builder.addLabel(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...-logging-logback/src/test/java/com/google/cloud/logging/logback/MDCEventEnhancerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.logging.logback; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import ch.qos.logback.classic.spi.LoggingEvent; | ||
import com.google.cloud.logging.LogEntry; | ||
import com.google.cloud.logging.Payload.StringPayload; | ||
import java.util.Collections; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class MDCEventEnhancerTest { | ||
private MDCEventEnhancer classUnderTest; | ||
|
||
@Before | ||
public void setUp() { | ||
classUnderTest = new MDCEventEnhancer(); | ||
} | ||
|
||
@Test | ||
public void testEnhanceLogEntry() { | ||
LoggingEvent loggingEvent = new LoggingEvent(); | ||
loggingEvent.setMessage("this is a test"); | ||
loggingEvent.setMDCPropertyMap(Collections.singletonMap("foo", "bar")); | ||
LogEntry.Builder builder = LogEntry.newBuilder(StringPayload.of("this is a test")); | ||
|
||
classUnderTest.enhanceLogEntry(builder, loggingEvent); | ||
LogEntry logEntry = builder.build(); | ||
|
||
assertThat(logEntry.getLabels().get("foo")).isEqualTo("bar"); | ||
} | ||
} |