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

davsclaus 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 d8f81166a61f CAMEL-27073: camel-mdc - camel.mdc.customExchangeHeaders 
and customExchangeProperties work on Camel Main (#30233)
d8f81166a61f is described below

commit d8f81166a61ff5148b0c88111d4736ecf1be6c7d
Author: Federico Mariani <[email protected]>
AuthorDate: Mon Nov 16 22:53:34 2026 +0100

    CAMEL-27073: camel-mdc - camel.mdc.customExchangeHeaders and 
customExchangeProperties work on Camel Main (#30233)
    
    Co-authored-by: Claude <[email protected]>
---
 .../org/apache/camel/catalog/docs/mdc.adoc         |   6 +-
 .../catalog/docs/micrometer-observability.adoc     |   2 +-
 .../apache/camel/catalog/docs/telemetry-dev.adoc   |   2 +-
 components/camel-mdc/pom.xml                       |   5 +
 components/camel-mdc/src/main/docs/mdc.adoc        |   6 +-
 .../main/java/org/apache/camel/mdc/MDCService.java |  24 +++++
 .../apache/camel/mdc/MDCMainConfigurationTest.java | 104 +++++++++++++++++++++
 .../src/main/docs/micrometer-observability.adoc    |   2 +-
 .../src/main/docs/telemetry-dev.adoc               |   2 +-
 9 files changed, 145 insertions(+), 8 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mdc.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mdc.adoc
index fcaae53169ac..5a69e42922ae 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mdc.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mdc.adoc
@@ -35,7 +35,7 @@ And later you had to make sure to provide MDC context 
propagation in async compo
     .setHeader("myCustomMDCKey", simple("myCustomKeyValue"))
 ----
 
-include the MDC service and additionally instruct the Camel application to 
treat that header as a MDC trace (via `camel.mdc.customHeaders=myCustomMDCKey` 
or `*` to include all headers). You won't need any longer to worry about 
context propagation as the propagation will be done via Camel Exchange instead.
+include the MDC service and additionally instruct the Camel application to 
treat that header as a MDC trace (via 
`camel.mdc.customExchangeHeaders=myCustomMDCKey` or `*` to include all 
headers). You won't need any longer to worry about context propagation as the 
propagation will be done via Camel Exchange instead.
 
 NOTE: you won't also need any longer to hack the code using Java DSL, as you 
can put headers in any Camel DSL.
 
@@ -60,7 +60,7 @@ This is the list of default MDC values included in each 
execution:
 * camel.contextId
 * camel.threadId
 
-If they exist in the Exchange, then, they will be included in the MDC. You can 
use `camel.mdc.customHeaders` and `camel.mdc.customProperties` to include any 
further header and property you need to trace.
+If they exist in the Exchange, then, they will be included in the MDC. You can 
use `camel.mdc.customExchangeHeaders` and `camel.mdc.customExchangeProperties` 
to include any further header and property you need to trace.
 
 == Configuration
 
@@ -73,3 +73,5 @@ The configuration properties for the MDC component are:
 |`camel.mdc.customExchangeHeaders` |  | 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.customExchangeProperties` |  | 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.
 |=======================================================================
+
+On Camel Main and Camel JBang, the older names `camel.mdc.customHeaders` and 
`camel.mdc.customProperties` also work.
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-observability.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-observability.adoc
index c58a6155d6b9..a09a23f2121b 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-observability.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-observability.adoc
@@ -145,7 +145,7 @@ Your application may require a Java agent in order to get 
the traces generated b
 
 == MDC logging
 
-You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration). This is 
the idiomatic way in Camel.
+You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customExchangeHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration). 
This is the idiomatic way in Camel.
 
 As an alternative, you can add Mapped Diagnostic Context tracing information 
(ie, `trace_id` and `span_id`) adding the specific 
https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/logger-mdc-instrumentation.md[Opentelemetry
 Logger MDC auto instrumentation]. This would be available if you configure the 
Opentelemetry. The logging configuration depends on the logging framework 
you're using.
 
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/telemetry-dev.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/telemetry-dev.adoc
index d55eca07aac0..8c7f910a79d3 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/telemetry-dev.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/telemetry-dev.adoc
@@ -76,4 +76,4 @@ Default is a very simple serialization into the default Java 
object `toString()`
 
 === MDC logging
 
-You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration).
+You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customExchangeHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration).
diff --git a/components/camel-mdc/pom.xml b/components/camel-mdc/pom.xml
index d8f98f05335c..8ab1de402fd0 100644
--- a/components/camel-mdc/pom.xml
+++ b/components/camel-mdc/pom.xml
@@ -53,6 +53,11 @@
       <artifactId>camel-test-spring-junit6</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-main</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
diff --git a/components/camel-mdc/src/main/docs/mdc.adoc 
b/components/camel-mdc/src/main/docs/mdc.adoc
index fcaae53169ac..5a69e42922ae 100644
--- a/components/camel-mdc/src/main/docs/mdc.adoc
+++ b/components/camel-mdc/src/main/docs/mdc.adoc
@@ -35,7 +35,7 @@ And later you had to make sure to provide MDC context 
propagation in async compo
     .setHeader("myCustomMDCKey", simple("myCustomKeyValue"))
 ----
 
-include the MDC service and additionally instruct the Camel application to 
treat that header as a MDC trace (via `camel.mdc.customHeaders=myCustomMDCKey` 
or `*` to include all headers). You won't need any longer to worry about 
context propagation as the propagation will be done via Camel Exchange instead.
+include the MDC service and additionally instruct the Camel application to 
treat that header as a MDC trace (via 
`camel.mdc.customExchangeHeaders=myCustomMDCKey` or `*` to include all 
headers). You won't need any longer to worry about context propagation as the 
propagation will be done via Camel Exchange instead.
 
 NOTE: you won't also need any longer to hack the code using Java DSL, as you 
can put headers in any Camel DSL.
 
@@ -60,7 +60,7 @@ This is the list of default MDC values included in each 
execution:
 * camel.contextId
 * camel.threadId
 
-If they exist in the Exchange, then, they will be included in the MDC. You can 
use `camel.mdc.customHeaders` and `camel.mdc.customProperties` to include any 
further header and property you need to trace.
+If they exist in the Exchange, then, they will be included in the MDC. You can 
use `camel.mdc.customExchangeHeaders` and `camel.mdc.customExchangeProperties` 
to include any further header and property you need to trace.
 
 == Configuration
 
@@ -73,3 +73,5 @@ The configuration properties for the MDC component are:
 |`camel.mdc.customExchangeHeaders` |  | 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.customExchangeProperties` |  | 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.
 |=======================================================================
+
+On Camel Main and Camel JBang, the older names `camel.mdc.customHeaders` and 
`camel.mdc.customProperties` also work.
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 87934751c628..37492240f4d9 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
@@ -80,6 +80,30 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
         this.customProperties = customProperties;
     }
 
+    public String getCustomExchangeHeaders() {
+        return customHeaders;
+    }
+
+    /**
+     * The exchange headers to include in the MDC (the same as {@link 
#setCustomHeaders(String)}). This is the
+     * {@code camel.mdc.customExchangeHeaders} option.
+     */
+    public void setCustomExchangeHeaders(String customExchangeHeaders) {
+        this.customHeaders = customExchangeHeaders;
+    }
+
+    public String getCustomExchangeProperties() {
+        return customProperties;
+    }
+
+    /**
+     * The exchange properties to include in the MDC (the same as {@link 
#setCustomProperties(String)}). This is the
+     * {@code camel.mdc.customExchangeProperties} option.
+     */
+    public void setCustomExchangeProperties(String customExchangeProperties) {
+        this.customProperties = customExchangeProperties;
+    }
+
     /**
      * Registers this {@link MDCService} on the {@link CamelContext} if not 
already registered.
      */
diff --git 
a/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCMainConfigurationTest.java
 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCMainConfigurationTest.java
new file mode 100644
index 000000000000..c2824a24bf33
--- /dev/null
+++ 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCMainConfigurationTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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 java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.main.Main;
+import org.apache.camel.spi.CamelMDCService;
+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.assertInstanceOf;
+
+/**
+ * The camel.mdc.* options of Camel Main are bound on the {@link MDCService}.
+ */
+public class MDCMainConfigurationTest {
+
+    private final AtomicReference<String> seen = new AtomicReference<>();
+
+    private Main newMain() {
+        Main main = new Main();
+        main.configure().addRoutesBuilder(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").routeId("start")
+                        .setHeader("foo", constant("bar"))
+                        .setProperty("myProp", constant("baz"))
+                        .process(e -> seen.set(MDC.get("foo") + "|" + 
MDC.get("myProp")));
+            }
+        });
+        main.addInitialProperty("camel.mdc.enabled", "true");
+        return main;
+    }
+
+    private void sendAndAssertMdc(Main main) throws Exception {
+        try (ProducerTemplate template = 
main.getCamelContext().createProducerTemplate()) {
+            template.sendBody("direct:start", "Hello");
+        }
+        assertEquals("bar|baz", seen.get());
+    }
+
+    @Test
+    void testCustomExchangeHeadersAndProperties() throws Exception {
+        Main main = newMain();
+        main.addInitialProperty("camel.mdc.customExchangeHeaders", "foo");
+        main.addInitialProperty("camel.mdc.customExchangeProperties", 
"myProp");
+        main.start();
+        try {
+            MDCService mdc = assertInstanceOf(MDCService.class,
+                    main.getCamelContext().hasService(CamelMDCService.class));
+            assertEquals("foo", mdc.getCustomExchangeHeaders());
+            assertEquals("myProp", mdc.getCustomExchangeProperties());
+            assertEquals("foo", mdc.getCustomHeaders());
+            assertEquals("myProp", mdc.getCustomProperties());
+            sendAndAssertMdc(main);
+        } finally {
+            main.stop();
+        }
+    }
+
+    @Test
+    void testCustomExchangeHeadersAndPropertiesWithWildcard() throws Exception 
{
+        Main main = newMain();
+        main.addInitialProperty("camel.mdc.customExchangeHeaders", "f*");
+        main.addInitialProperty("camel.mdc.customExchangeProperties", "*");
+        main.start();
+        try {
+            sendAndAssertMdc(main);
+        } finally {
+            main.stop();
+        }
+    }
+
+    @Test
+    void testCustomHeadersAndProperties() throws Exception {
+        Main main = newMain();
+        main.addInitialProperty("camel.mdc.customHeaders", "foo");
+        main.addInitialProperty("camel.mdc.customProperties", "myProp");
+        main.start();
+        try {
+            sendAndAssertMdc(main);
+        } finally {
+            main.stop();
+        }
+    }
+}
diff --git 
a/components/camel-micrometer-observability/src/main/docs/micrometer-observability.adoc
 
b/components/camel-micrometer-observability/src/main/docs/micrometer-observability.adoc
index c58a6155d6b9..a09a23f2121b 100644
--- 
a/components/camel-micrometer-observability/src/main/docs/micrometer-observability.adoc
+++ 
b/components/camel-micrometer-observability/src/main/docs/micrometer-observability.adoc
@@ -145,7 +145,7 @@ Your application may require a Java agent in order to get 
the traces generated b
 
 == MDC logging
 
-You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration). This is 
the idiomatic way in Camel.
+You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customExchangeHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration). 
This is the idiomatic way in Camel.
 
 As an alternative, you can add Mapped Diagnostic Context tracing information 
(ie, `trace_id` and `span_id`) adding the specific 
https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/logger-mdc-instrumentation.md[Opentelemetry
 Logger MDC auto instrumentation]. This would be available if you configure the 
Opentelemetry. The logging configuration depends on the logging framework 
you're using.
 
diff --git a/components/camel-telemetry-dev/src/main/docs/telemetry-dev.adoc 
b/components/camel-telemetry-dev/src/main/docs/telemetry-dev.adoc
index d55eca07aac0..8c7f910a79d3 100644
--- a/components/camel-telemetry-dev/src/main/docs/telemetry-dev.adoc
+++ b/components/camel-telemetry-dev/src/main/docs/telemetry-dev.adoc
@@ -76,4 +76,4 @@ Default is a very simple serialization into the default Java 
object `toString()`
 
 === MDC logging
 
-You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration).
+You can leverage the `traceHeadersInclusion` to include the generated 
`CAMEL_TRACE_ID` and `CAMEL_SPAN_ID` into the Camel Exchange and together with 
`camel-mdc` you can make those headers available in the MDC context (via 
`camel.mdc.customExchangeHeaders=CAMEL_TRACE_ID,CAMEL_SPAN_ID` configuration).

Reply via email to