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


The following commit(s) were added to refs/heads/main by this push:
     new 1c0c7940452f feat(components): mdc wildcard configuration
1c0c7940452f is described below

commit 1c0c7940452fafc62de78ff669a1863f578049ee
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Mar 6 12:05:26 2026 +0100

    feat(components): mdc wildcard configuration
    
    Closes CAMEL-23133
---
 components/camel-mdc/src/main/docs/mdc.adoc        |  4 +-
 .../main/java/org/apache/camel/mdc/MDCService.java | 65 +++++++++++++---
 .../mdc/MDCSelectedHeadersWithWildcardTest.java    | 85 +++++++++++++++++++++
 .../mdc/MDCSelectedPropertiesWithWildcardTest.java | 89 ++++++++++++++++++++++
 4 files changed, 231 insertions(+), 12 deletions(-)

diff --git a/components/camel-mdc/src/main/docs/mdc.adoc 
b/components/camel-mdc/src/main/docs/mdc.adoc
index 6811ab56e23e..51de38ccbe79 100644
--- a/components/camel-mdc/src/main/docs/mdc.adoc
+++ b/components/camel-mdc/src/main/docs/mdc.adoc
@@ -70,6 +70,6 @@ The configuration properties for the MDC component are:
 |=======================================================================
 |Option |Default |Description
 |`camel.mdc.enabled`| false | Enable the MDC logging.
-|`camel.mdc.customHeaders` |  | Provide the headers you would like to use in 
the logging. Use `*` value to include all available headers.
-|`camel.mdc.customProperties` |  | Provide the properties you would like to 
use in the logging. Use `*` value to include all available properties.
+|`camel.mdc.customHeaders` |  | Provide the exchange headers you would like to 
trace in MDC. Use `*` value as a wildcard to include more exchange headers at 
once, for example `CAMEL_HTTP_*` or only the `*` to include all available 
exchange headers.
+|`camel.mdc.customProperties` |  | Provide the exchange properties you would 
like to trace in MDC. Use `*` value as a wildcard to include more exchange 
properties at once, for example `property_*` or only the `*` to include all 
available exchange properties.
 |=======================================================================
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 9e52ac66bd9e..87934751c628 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
@@ -16,7 +16,10 @@
  */
 package org.apache.camel.mdc;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
@@ -203,12 +206,52 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
     // Set/unset those headers selected by the user.
     private void userSelectedHeadersMDC(Exchange exchange, boolean push) {
         for (String customHeader : getCustomHeaders().split(",")) {
-            if (exchange.getIn().getHeader(customHeader) != null) {
-                if (push) {
-                    MDC.put(customHeader, 
exchange.getIn().getHeader(customHeader, String.class));
-                } else {
-                    MDC.remove(customHeader);
+            if (customHeader.contains("*")) {
+                for (String filteredHeader : 
filter(exchange.getIn().getHeaders().keySet(), customHeader)) {
+                    String value = exchange.getIn().getHeader(filteredHeader, 
String.class);
+                    mdcAction(exchange, filteredHeader, value, push);
                 }
+            } else {
+                String value = exchange.getIn().getHeader(customHeader, 
String.class);
+                mdcAction(exchange, customHeader, value, push);
+            }
+        }
+    }
+
+    // filter returns a list of values matching the condition expressed in the 
filter.
+    private List<String> filter(Set<String> all, String filter) {
+        List<String> filteredHeaders = new ArrayList<>();
+        for (String f : all) {
+            if (matches(filter, f)) {
+                filteredHeaders.add(f);
+            }
+        }
+
+        return filteredHeaders;
+    }
+
+    // NOTE: we avoid using regex on purpose in order to avoid potential 
vulnerabilities and better performances.
+    private boolean matches(String filter, String value) {
+        String[] parts = filter.split("\\*", -1);
+
+        int pos = 0;
+        for (String part : parts) {
+            int idx = value.indexOf(part, pos);
+            if (idx == -1) {
+                return false;
+            }
+            pos = idx + part.length();
+        }
+
+        return true;
+    }
+
+    private void mdcAction(Exchange exchange, String key, String value, 
boolean push) {
+        if (value != null) {
+            if (push) {
+                MDC.put(key, value);
+            } else {
+                MDC.remove(key);
             }
         }
     }
@@ -229,12 +272,14 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
     // Set/unset those properties selected by the user.
     private void userSelectedPropertiesMDC(Exchange exchange, boolean push) {
         for (String customProperty : getCustomProperties().split(",")) {
-            if (exchange.getProperty(customProperty) != null) {
-                if (push) {
-                    MDC.put(customProperty, 
exchange.getProperty(customProperty, String.class));
-                } else {
-                    MDC.remove(customProperty);
+            if (customProperty.contains("*")) {
+                for (String filteredProperty : 
filter(exchange.getProperties().keySet(), customProperty)) {
+                    String value = exchange.getProperty(filteredProperty, 
String.class);
+                    mdcAction(exchange, filteredProperty, value, push);
                 }
+            } else {
+                String value = exchange.getProperty(customProperty, 
String.class);
+                mdcAction(exchange, customProperty, value, push);
             }
         }
     }
diff --git 
a/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedHeadersWithWildcardTest.java
 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedHeadersWithWildcardTest.java
new file mode 100644
index 000000000000..f99973acaf45
--- /dev/null
+++ 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedHeadersWithWildcardTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.camel.mdc;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.ExchangeTestSupport;
+import org.junit.jupiter.api.Test;
+import org.slf4j.MDC;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MDCSelectedHeadersWithWildcardTest extends ExchangeTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        MDCService mdcSvc = new MDCService();
+        mdcSvc.setCustomHeaders("head*_filter");
+        CamelContext context = super.createCamelContext();
+        CamelContextAware.trySetCamelContext(mdcSvc, context);
+        mdcSvc.init(context);
+        return context;
+    }
+
+    @Test
+    void testRouteSingleRequest() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:assertMdc");
+        mock.expectedMessageCount(1);
+        mock.whenAnyExchangeReceived(exchange -> {
+            // Required MDC entries
+            assertNotNull(MDC.get(MDCService.MDC_MESSAGE_ID), "MDC_MESSAGE_ID 
should be set");
+            assertNotNull(MDC.get(MDCService.MDC_EXCHANGE_ID), 
"MDC_EXCHANGE_ID should be set");
+            assertNotNull(MDC.get(MDCService.MDC_ROUTE_ID), "MDC_ROUTE_ID 
should be set");
+            assertNotNull(MDC.get(MDCService.MDC_CAMEL_CONTEXT_ID), 
"MDC_CAMEL_CONTEXT_ID should be set");
+
+            // Headers propagated to MDC
+            assertEquals("Header1", MDC.get("head1_filter"));
+            assertEquals("Header2", MDC.get("head2_filter"));
+            assertNull(MDC.get("head3_noFilter")); // note: intentionally not 
included
+        });
+
+        // Trigger the route
+        template.request("direct:start", null);
+
+        // Wait for expectations
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .routeId("start")
+                        .log("A message")
+                        .setHeader("head1_filter", constant("Header1"))
+                        .setHeader("head2_filter", constant("Header2"))
+                        .setHeader("head3_noFilter", constant("Header3"))
+                        .to("mock:assertMdc")
+                        .to("log:info");
+            }
+        };
+    }
+
+}
diff --git 
a/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedPropertiesWithWildcardTest.java
 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedPropertiesWithWildcardTest.java
new file mode 100644
index 000000000000..d1b404e7bf05
--- /dev/null
+++ 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCSelectedPropertiesWithWildcardTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.camel.mdc;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.ExchangeTestSupport;
+import org.junit.jupiter.api.Test;
+import org.slf4j.MDC;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MDCSelectedPropertiesWithWildcardTest extends ExchangeTestSupport 
{
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        MDCService mdcSvc = new MDCService();
+        mdcSvc.setCustomProperties("prop*_filter,more*");
+        CamelContext context = super.createCamelContext();
+        CamelContextAware.trySetCamelContext(mdcSvc, context);
+        mdcSvc.init(context);
+        return context;
+    }
+
+    @Test
+    void testRouteSingleRequest() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:assertMdc");
+        mock.expectedMessageCount(1);
+
+        mock.whenAnyExchangeReceived(exchange -> {
+            // Required MDC entries
+            assertNotNull(MDC.get(MDCService.MDC_MESSAGE_ID), "MDC_MESSAGE_ID 
should be set");
+            assertNotNull(MDC.get(MDCService.MDC_EXCHANGE_ID), 
"MDC_EXCHANGE_ID should be set");
+            assertNotNull(MDC.get(MDCService.MDC_ROUTE_ID), "MDC_ROUTE_ID 
should be set");
+            assertNotNull(MDC.get(MDCService.MDC_CAMEL_CONTEXT_ID), 
"MDC_CAMEL_CONTEXT_ID should be set");
+
+            // Properties propagated to MDC
+            assertEquals("Property1", MDC.get("prop1_filter"));
+            assertEquals("Property2", MDC.get("prop2_filter"));
+            assertEquals("Property3", MDC.get("more_filter"));
+            assertNull(MDC.get("prop4")); // intentionally not included
+        });
+
+        // Trigger the route
+        template.request("direct:start", null);
+
+        // Wait for assertions
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .routeId("start")
+                        .log("A message")
+                        .setProperty("prop1_filter", constant("Property1"))
+                        .setProperty("prop2_filter", constant("Property2"))
+                        .setProperty("more_filter", constant("Property3"))
+                        .setProperty("prop4", constant("Property4"))
+                        // No assertions inside the route
+                        .to("mock:assertMdc")
+                        .to("log:info");
+            }
+        };
+    }
+
+}

Reply via email to