dimas-b commented on code in PR #4061: URL: https://github.com/apache/polaris/pull/4061#discussion_r2991594595
########## runtime/service/src/main/java/org/apache/polaris/service/task/RequestIdPropagator.java: ########## @@ -0,0 +1,96 @@ +/* + * 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.polaris.service.task; + +import jakarta.annotation.Nullable; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.ContextNotActiveException; +import jakarta.inject.Inject; +import org.apache.polaris.service.context.catalog.RequestIdHolder; +import org.apache.polaris.service.tracing.RequestIdFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +/** + * Propagates the request ID across the async task boundary. + * + * <p>At capture time the request ID is read from {@link RequestIdHolder}, which is populated by + * {@code RequestIdFilter} on HTTP request threads and by this propagator's {@link #restore} path + * on task threads (enabling nested task submission). + * + * <p>At restore time the ID is written to both the {@link RequestIdHolder} (so that {@code + * RequestIdSupplier} works in task threads) and to the SLF4J MDC (so that log messages emitted by + * the task carry the originating request ID). + * + * <p>MDC cleanup is performed by the returned {@link AutoCloseable} so that thread-pool threads are + * left in a clean state after the task completes. + */ +@ApplicationScoped +public class RequestIdPropagator implements AsyncContextPropagator { + + private static final Logger LOGGER = LoggerFactory.getLogger(RequestIdPropagator.class); + + private final RequestIdHolder requestIdHolder; + + @SuppressWarnings("unused") // Required by CDI + protected RequestIdPropagator() { + this(null); + } + + @Inject + public RequestIdPropagator(RequestIdHolder requestIdHolder) { + this.requestIdHolder = requestIdHolder; + } + + @Nullable + @Override + public Object capture() { + String id = null; + try { + id = requestIdHolder.get(); + } catch (ContextNotActiveException e) { + // scope not active, return null + } + LOGGER.trace("capture requestId={}", id); + return id; + } + + @Override + public AutoCloseable restore(@Nullable Object capturedState) { + String requestId = (String) capturedState; + LOGGER.trace("restore requestId={}", requestId); + requestIdHolder.set(requestId); + + if (requestId == null) { + return () -> {}; + } + + String previous = MDC.get(RequestIdFilter.REQUEST_ID_KEY); Review Comment: I do not mind populating MDC with Request IDs. Yet, I think it is preferable to avoid MDC as a source of request IDs for context propagation purposes. -- 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]
