dsmiley commented on code in PR #3985: URL: https://github.com/apache/solr/pull/3985#discussion_r2650121504
########## solr/core/src/java/org/apache/solr/servlet/MdcLoggingFilter.java: ########## @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.solr.servlet; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import org.apache.solr.common.util.SuppressForbidden; +import org.apache.solr.logging.MDCLoggingContext; +import org.apache.solr.logging.MDCSnapshot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Servlet Filter to set up and tear down MDC Logging context for each reqeust. */ +public class MdcLoggingFilter extends CoreContainerAwareHttpFilter { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Override + @SuppressForbidden( + reason = + "Set the thread contextClassLoader for all 3rd party dependencies that we cannot control") + protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) + throws IOException, ServletException { + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + // this autocloseable is here to invoke MDCSnapshot.close() which restores captured state + try (var mdcSnapshot = MDCSnapshot.create()) { + log.trace("MDC snapshot recorded {}", mdcSnapshot); // avoid both compiler and ide warning. + MDCLoggingContext.reset(); + MDCLoggingContext.setNode(getCores()); + // This doesn't belong here, but for the moment it is here to preserve it's relative + // timing of execution for now. Probably I will break this out in a subsequent change Review Comment: Hopefully not a future Filter for just one line. I sympathize with your characterization of SolrDispatchFilter as a kitchen sink but a few lines for certain topics at the critical Solr entrypoint is understandable IMO. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
