ppkarwasz commented on code in PR #2419: URL: https://github.com/apache/logging-log4j2/pull/2419#discussion_r1544808343
########## log4j-api/src/main/java/org/apache/logging/log4j/ScopedContext.java: ########## @@ -0,0 +1,558 @@ +/* + * 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.logging.log4j; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import org.apache.logging.log4j.internal.ScopedContextAnchor; +import org.apache.logging.log4j.status.StatusLogger; + +/** + * Context that can be used for data to be logged in a block of code. + * + * While this is influenced by ScopedValues from Java 21 it does not share the same API. While it can perform a + * similar function as a set of ScopedValues it is really meant to allow a block of code to include a set of keys and + * values in all the log events within that block. The underlying implementation must provide support for + * logging the ScopedContext for that to happen. + * + * The ScopedContext will not be bound to the current thread until either a run or call method is invoked. The + * contexts are nested so creating and running or calling via a second ScopedContext will result in the first + * ScopedContext being hidden until the call is returned. Thus the values from the first ScopedContext need to + * be added to the second to be included. + * + * The ScopedContext can be passed to child threads by including the ExecutorService to be used to manage the + * run or call methods. The caller should interact with the ExecutorService as if they were submitting their + * run or call methods directly to it. The ScopedContext performs no error handling other than to ensure the + * ThreadContext and ScopedContext are cleaned up from the executed Thread. + * + * @since 2.24.0 + */ +public class ScopedContext { Review Comment: @rgoers, > As you know, I am also against the API having dependencies on anything but the JDK. I am not inclined to have the API be dependent on someone elses context propagation API. I am not suggesting to add an external context propagation API dependency to `log4j-api`, but to delegate context propagation to higher levels of the stack. For example let's look at the first example of ["Observability with Spring Boot 3"](https://spring.io/blog/2022/10/12/observability-with-spring-boot-3#how-does-micrometer-observation-work): ```java // Create an Observation and observe your code! Observation.createNotStarted("user.name", registry) .contextualName("getting-user-name") .lowCardinalityKeyValue("userType", "userType1") // let's assume that you can have 3 user types .highCardinalityKeyValue("userId", "1234") // let's assume that this is an arbitrary number .observe(() -> log.info("Hello")); // this is a shortcut for starting an observation, opening a scope, ``` The `userId` value will be present in the spans and metrics, but will it be present in the context data of the log event? If it is not present, how can I add it? I don't think that wrapping the lambda using `ScopedContext` is a practical solution. > Looking at the `Context` object I see that it mostly adds different kinds of functions that can be called. I am not convinced that is necessary but if it is they can always be added later. The interesting part for me is not the amount of wrapper methods it provides, but the promise that it will propagate everywhere using any kind of technology (Netty, HTTP, Spring Reactor, …). They already integrate with Log4j Core (see [`log4j-context-data-2.17`](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure)), but even if they didn't, we could create a Maven module that integrates OpenTelemetry with Log4j API. -- 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]
