-
|
Hello, I am working with a situation where I want to log a conditional MDC field if present, but also modify it preserving the "emptiness" if it doesn't exist. For example if the <omitEmptyFields>true</omitEmptyFields>
<pattern>
{
"trace.id": "%mdc{traceId}"
}
</pattern>Which works fine, but I also want to create another field which is the URL to navigate to the trace ID in Google's Trace Explorer. the URL would be provided as an environment variable and needs to be concatenated with the E.g. <omitEmptyFields>true</omitEmptyFields>
<pattern>
{
"logging.googleapis.com/trace": "projects/${PROJECT_NAME}/%mdc{traceId}"
}
</pattern>However this leads to I have created a modified version of the answer to this similar question: #762 However it seems a little clunky to have a special omission field for something that seems very nearly supported just via the xml: <jsonGeneratorDecorator class="StackDriverJsonDecorator" />
....further down
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{
"logging.googleapis.com/trace": "projects/${PROJECT_NAME}/%mdc{traceId:-OMIT_ME}"
}
</patternimport com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
import com.fasterxml.jackson.core.filter.TokenFilter;
import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
import net.logstash.logback.decorate.JsonGeneratorDecorator;
public class StackDriverJsonDecorator implements JsonGeneratorDecorator {
@Override
public JsonGenerator decorate(JsonGenerator generator) {
return new FilteringGeneratorDelegate(
generator,
OmitSpecialTokenFilter.INSTANCE,
Inclusion.INCLUDE_ALL_AND_PATH,
true /* multiple matches */);
}
private static class OmitSpecialTokenFilter extends TokenFilter {
private static final OmitSpecialTokenFilter INSTANCE = new OmitSpecialTokenFilter();
@Override
public boolean includeNull() {
return false;
}
@Override
public boolean includeString(String value) {
return !value.contains("OMIT_ME");
}
}
}Does this seem like the correct approach for this problem? Would it be easier to have some sort of log filter that extracts the traceId from the MDC and writes a new variable with the concatenation done already, then just reference that variable? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I'd recommend writing a custom |
Beta Was this translation helpful? Give feedback.
I'd recommend writing a custom
MdcEntryWriterfor this use case.