gabriel-farache commented on code in PR #4094:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4094#discussion_r2581457831


##########
api/kogito-services/src/main/java/org/kie/kogito/services/context/ProcessInstanceContext.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.kie.kogito.services.context;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+/**
+ * Utility class for managing process instance context in logging operations.
+ * This class uses SLF4J's Mapped Diagnostic Context (MDC) to ensure process 
instance IDs
+ * are automatically included in log messages.
+ *
+ * When no process instance is available, an empty string is used as the 
default value
+ * to provide cleaner formatting and easier searching in log aggregation 
systems.
+ *
+ * Thread Safety: This class is thread-safe and properly manages context 
isolation
+ * between different threads using MDC's inherent ThreadLocal-based storage.
+ */
+public final class ProcessInstanceContext {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ProcessInstanceContext.class);
+
+    private static final Map<String, ContextExtension> EXTENSIONS = new 
ConcurrentHashMap<>();
+
+    /**
+     * MDC key used to store the process instance ID.
+     * This key should be referenced in logging configurations.
+     */
+    public static final String MDC_PROCESS_INSTANCE_KEY = "processInstanceId";
+
+    /**
+     * Default value used when no process instance ID is available.
+     * Empty string for cleaner log formatting and easier searching.
+     */
+    public static final String GENERAL_CONTEXT = "";
+
+    // Private constructor to prevent instantiation
+    private ProcessInstanceContext() {
+        // Utility class
+    }
+
+    /**
+     * Sets the process instance ID for the current thread context.
+     * This method updates SLF4J MDC and only performs the update if the value 
changes.
+     *
+     * @param processInstanceId the process instance ID to set, or null to use 
general context
+     */
+    public static void setProcessInstanceId(String processInstanceId) {
+        String effectiveId = processInstanceId != null ? processInstanceId : 
GENERAL_CONTEXT;
+
+        // Only update MDC if the value is changing (optimization)
+        String currentId = MDC.get(MDC_PROCESS_INSTANCE_KEY);
+        if (!effectiveId.equals(currentId)) {
+            MDC.put(MDC_PROCESS_INSTANCE_KEY, effectiveId);
+        }
+    }
+
+    /**
+     * Gets the current process instance ID from MDC.
+     *
+     * @return the current process instance ID, or empty string if no context 
is set
+     */
+    public static String getProcessInstanceId() {
+        String id = MDC.get(MDC_PROCESS_INSTANCE_KEY);
+        return id != null ? id : GENERAL_CONTEXT;
+    }
+
+    /**
+     * Clears the process instance context for the current thread.
+     * This resets the MDC to the general context (empty string).
+     */
+    public static void clear() {
+        MDC.put(MDC_PROCESS_INSTANCE_KEY, GENERAL_CONTEXT);
+    }
+
+    /**
+     * Checks if a process instance context is currently set.
+     *
+     * @return true if a process instance context is set, false otherwise
+     */
+    public static boolean hasContext() {
+        String id = MDC.get(MDC_PROCESS_INSTANCE_KEY);
+        return id != null && !GENERAL_CONTEXT.equals(id);
+    }
+
+    /**
+     * Gets a copy of the current MDC context map for propagation to other 
threads.
+     * This is useful for async operations that need to maintain the same 
logging context.
+     *
+     * @return a copy of the current MDC context map, or null if no context is 
set
+     */
+    public static Map<String, String> copyContextForAsync() {
+        return MDC.getCopyOfContextMap();
+    }
+
+    /**
+     * Sets the MDC context map from a previously copied context.

Review Comment:
   I added in the comment:
   ```
        * Only the core keys and the registered extensions keys will be 
restored in the MDC
   ```



-- 
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]

Reply via email to