-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add composite MdcEntryWriter to filter MDC keys with regex (#974)
- Loading branch information
Showing
5 changed files
with
219 additions
and
2 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
87 changes: 87 additions & 0 deletions
87
...in/java/net/logstash/logback/composite/loggingevent/mdc/RegexFilteringMdcEntryWriter.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,87 @@ | ||
/* | ||
* Copyright 2013-2023 the original author or authors. | ||
* | ||
* 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 net.logstash.logback.composite.loggingevent.mdc; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
/** | ||
* Writes MDC entries by delegating to other instances of {@link MdcEntryWriter} if MDC key matches the given include | ||
* and exclude pattern. | ||
* | ||
* <ol> | ||
* <li>An MDC entry is written if the MDC key does match the {@link #includeMdcKeyPattern} AND does not match | ||
* the {@link #excludeMdcKeyPattern}.</li> | ||
* <li>Omitting a {@link #includeMdcKeyPattern} means to include all MDC keys.</li> | ||
* <li>Omitting a {@link #excludeMdcKeyPattern} means to exclude no MDC keys.</li> | ||
* </ol> | ||
*/ | ||
public class RegexFilteringMdcEntryWriter implements MdcEntryWriter { | ||
|
||
private Pattern includeMdcKeyPattern; | ||
private Pattern excludeMdcKeyPattern; | ||
private final List<MdcEntryWriter> mdcEntryWriters = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean writeMdcEntry(JsonGenerator generator, String fieldName, String mdcKey, String mdcValue) throws IOException { | ||
if (shouldWrite(mdcKey)) { | ||
for (MdcEntryWriter mdcEntryWriter : this.mdcEntryWriters) { | ||
if (mdcEntryWriter.writeMdcEntry(generator, fieldName, mdcKey, mdcValue)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public Pattern getIncludeMdcKeyPattern() { | ||
return includeMdcKeyPattern; | ||
} | ||
public void setIncludeMdcKeyPattern(String includeMdcKeyPattern) { | ||
this.includeMdcKeyPattern = Pattern.compile(includeMdcKeyPattern); | ||
} | ||
|
||
public Pattern getExcludeMdcKeyPattern() { | ||
return excludeMdcKeyPattern; | ||
} | ||
public void setExcludeMdcKeyPattern(String excludeMdcKeyPattern) { | ||
this.excludeMdcKeyPattern = Pattern.compile(excludeMdcKeyPattern); | ||
} | ||
|
||
public List<MdcEntryWriter> getMdcEntryWriters() { | ||
return Collections.unmodifiableList(mdcEntryWriters); | ||
} | ||
public void addMdcEntryWriter(MdcEntryWriter mdcEntryWriter) { | ||
this.mdcEntryWriters.add(mdcEntryWriter); | ||
} | ||
|
||
/** Returns true if passed MDC key should be written to the JSON output. */ | ||
private boolean shouldWrite(String key) { | ||
if (this.mdcEntryWriters.isEmpty()) { | ||
return false; | ||
} | ||
boolean includeKey = includeMdcKeyPattern == null || includeMdcKeyPattern.matcher(key).matches(); | ||
boolean excludeKey = excludeMdcKeyPattern != null && excludeMdcKeyPattern.matcher(key).matches(); | ||
return includeKey && !excludeKey; | ||
} | ||
|
||
} |
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
92 changes: 92 additions & 0 deletions
92
...ava/net/logstash/logback/composite/loggingevent/mdc/RegexFilteringMdcEntryWriterTest.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,92 @@ | ||
/* | ||
* Copyright 2013-2023 the original author or authors. | ||
* | ||
* 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 net.logstash.logback.composite.loggingevent.mdc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RegexFilteringMdcEntryWriterTest { | ||
|
||
private final RegexFilteringMdcEntryWriter mdcEntryWriter = new RegexFilteringMdcEntryWriter(); | ||
|
||
@Mock | ||
private JsonGenerator generator; | ||
@Mock | ||
private MdcEntryWriter mockedMdcEntryWriter; | ||
|
||
@Test | ||
void noIncludeAndNoExcludePattern() throws IOException { | ||
mockMdcEntryWriter(); | ||
|
||
boolean result = mdcEntryWriter.writeMdcEntry(generator, "field", "key", "value"); | ||
|
||
assertThat(result).isTrue(); | ||
verify(mockedMdcEntryWriter).writeMdcEntry(generator, "field", "key", "value"); | ||
verifyNoMoreInteractions(mockedMdcEntryWriter); | ||
} | ||
|
||
@Test | ||
void noMdcEntryWriter() throws IOException { | ||
boolean result = mdcEntryWriter.writeMdcEntry(generator, "field", "key", "value"); | ||
|
||
assertThat(result).isFalse(); | ||
verifyNoMoreInteractions(mockedMdcEntryWriter); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"include,excl.*,true", | ||
"include,.*lude,false", | ||
"exclude,excl.*,false", | ||
"other,excl.*,false" | ||
}) | ||
void includeAndExcludePattern(String mdcKey, String excludePattern, boolean entryWritten) throws IOException { | ||
if (entryWritten) { | ||
mockMdcEntryWriter(); | ||
} | ||
mdcEntryWriter.setIncludeMdcKeyPattern("incl.*"); | ||
mdcEntryWriter.setExcludeMdcKeyPattern(excludePattern); | ||
|
||
boolean result = mdcEntryWriter.writeMdcEntry(generator, "field", mdcKey, "value"); | ||
|
||
assertThat(result).isEqualTo(entryWritten); | ||
if (entryWritten) { | ||
verify(mockedMdcEntryWriter).writeMdcEntry(generator, "field", mdcKey, "value"); | ||
} | ||
verifyNoMoreInteractions(mockedMdcEntryWriter); | ||
} | ||
|
||
private void mockMdcEntryWriter() throws IOException { | ||
when(mockedMdcEntryWriter.writeMdcEntry(eq(generator), anyString(), anyString(), anyString())).thenReturn(true); | ||
mdcEntryWriter.addMdcEntryWriter(mockedMdcEntryWriter); | ||
} | ||
|
||
} |
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