This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cc3bb758a6348ca3b1e94d07acfc4fbc52d5b8ed
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Wed Sep 24 11:25:19 2025 +0200

    feat(components): MDC custom headers and properties
---
 .../main/java/org/apache/camel/mdc/MDCService.java | 78 +++++++++++++++++++++-
 1 file changed, 76 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-mdc/src/main/java/org/apache/camel/mdc/MDCService.java 
b/components/camel-mdc/src/main/java/org/apache/camel/mdc/MDCService.java
index 098230b11a0..eaf890f93cf 100644
--- a/components/camel-mdc/src/main/java/org/apache/camel/mdc/MDCService.java
+++ b/components/camel-mdc/src/main/java/org/apache/camel/mdc/MDCService.java
@@ -18,6 +18,7 @@ package org.apache.camel.mdc;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.CamelLogger;
 import org.apache.camel.spi.CamelMDCService;
@@ -32,10 +33,20 @@ import org.slf4j.MDC;
 @JdkService("mdc-service")
 public class MDCService extends ServiceSupport implements CamelMDCService {
 
+    private String MDC_BREADCRUMB_ID = "camel.breadcrumbId";
+    private String MDC_EXCHANGE_ID = "camel.exchangeId";
+    private String MDC_MESSAGE_ID = "camel.messageId";
+    private String MDC_CORRELATION_ID = "camel.correlationId";
+    private String MDC_ROUTE_ID = "camel.routeId";
+    private String MDC_CAMEL_CONTEXT_ID = "camel.contextId";
+
     private static final Logger LOG = 
LoggerFactory.getLogger(MDCService.class);
 
     private CamelContext camelContext;
 
+    private String customHeaders;
+    private String customProperties;
+
     @Override
     public CamelContext getCamelContext() {
         return camelContext;
@@ -46,6 +57,22 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
         this.camelContext = camelContext;
     }
 
+    public String getCustomHeaders() {
+        return customHeaders;
+    }
+
+    public void setCustomHeaders(String customHeaders) {
+        this.customHeaders = customHeaders;
+    }
+
+    public String getCustomProperties() {
+        return customProperties;
+    }
+
+    public void setCustomProperties(String customProperties) {
+        this.customProperties = customProperties;
+    }
+
     /**
      * Registers this {@link MDCService} on the {@link CamelContext} if not 
already registered.
      */
@@ -71,8 +98,16 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
         @Override
         public String onLog(Exchange exchange, CamelLogger camelLogger, String 
message) {
             try {
-                // MDC setting here
-                MDC.put("exchangeID", exchange.getExchangeId());
+                // Default values
+                prepareMDC(exchange);
+                if (getCustomHeaders() != null) {
+                    // User headers values
+                    userSelectedHeadersMDC(exchange);
+                }
+                if (getCustomProperties() != null) {
+                    // User headers values
+                    userSelectedPropertiesMDC(exchange);
+                }
             } catch (Exception t) {
                 // This exception is ignored
                 LOG.warn("MDC: failed to store MDC data. This exception is 
ignored.", t);
@@ -84,6 +119,45 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
         public void afterLog(Exchange exchange, CamelLogger camelLogger, 
String message) {
             MDC.clear();
         }
+
+        // Default basic MDC properties to set
+        private void prepareMDC(Exchange exchange) {
+            MDC.put(MDC_EXCHANGE_ID, exchange.getExchangeId());
+            MDC.put(MDC_MESSAGE_ID, exchange.getMessage().getMessageId());
+            MDC.put(MDC_CAMEL_CONTEXT_ID, exchange.getContext().getName());
+            // Backward compatibility: this info may not be longer widely used
+            String corrId = 
exchange.getProperty(ExchangePropertyKey.CORRELATION_ID, String.class);
+            if (corrId != null) {
+                MDC.put(MDC_CORRELATION_ID, corrId);
+            }
+            // Backward compatibility: this info may not be longer widely used
+            String breadcrumbId = 
exchange.getIn().getHeader(Exchange.BREADCRUMB_ID, String.class);
+            if (breadcrumbId != null) {
+                MDC.put(MDC_BREADCRUMB_ID, breadcrumbId);
+            }
+            String routeId = exchange.getFromRouteId();
+            if (routeId != null) {
+                MDC.put(MDC_ROUTE_ID, routeId);
+            }
+        }
+
+        // Include those headers selected by the user
+        private void userSelectedHeadersMDC(Exchange exchange) {
+            for (String customHeader : getCustomHeaders().split(",")) {
+                if (exchange.getIn().getHeader(customHeader) != null) {
+                    MDC.put(customHeader, 
exchange.getIn().getHeader(customHeader, String.class));
+                }
+            }
+        }
+
+        // Include those properties selected by the user
+        private void userSelectedPropertiesMDC(Exchange exchange) {
+            for (String customProperty : getCustomProperties().split(",")) {
+                if (exchange.getProperty(customProperty) != null) {
+                    MDC.put(customProperty, 
exchange.getProperty(customProperty, String.class));
+                }
+            }
+        }
     }
 
 }

Reply via email to