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 e081da340fd3 CAMEL-27076: camel-mdc - camel.routeId is the route being 
run (#30234)
e081da340fd3 is described below

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

    CAMEL-27076: camel-mdc - camel.routeId is the route being run (#30234)
    
    Co-authored-by: Claude <[email protected]>
---
 .../org/apache/camel/catalog/docs/mdc.adoc         |   2 +-
 components/camel-mdc/src/main/docs/mdc.adoc        |   2 +-
 .../main/java/org/apache/camel/mdc/MDCService.java |   6 +-
 .../java/org/apache/camel/mdc/MDCRouteIdTest.java  | 109 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |   6 ++
 5 files changed, 121 insertions(+), 4 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 5a69e42922ae..6c574ab9955a 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
@@ -56,7 +56,7 @@ This is the list of default MDC values included in each 
execution:
 * camel.exchangeId
 * camel.messageId
 * camel.correlationId
-* camel.routeId
+* camel.routeId (the route being run; when a route calls another route, the id 
of the called route)
 * camel.contextId
 * camel.threadId
 
diff --git a/components/camel-mdc/src/main/docs/mdc.adoc 
b/components/camel-mdc/src/main/docs/mdc.adoc
index 5a69e42922ae..6c574ab9955a 100644
--- a/components/camel-mdc/src/main/docs/mdc.adoc
+++ b/components/camel-mdc/src/main/docs/mdc.adoc
@@ -56,7 +56,7 @@ This is the list of default MDC values included in each 
execution:
 * camel.exchangeId
 * camel.messageId
 * camel.correlationId
-* camel.routeId
+* camel.routeId (the route being run; when a route calls another route, the id 
of the called route)
 * camel.contextId
 * camel.threadId
 
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 37492240f4d9..3269685b5cd1 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
@@ -30,6 +30,7 @@ import org.apache.camel.spi.CamelMDCService;
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.spi.LogListener;
 import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -202,7 +203,8 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
             if (breadcrumbId != null) {
                 MDC.put(MDC_BREADCRUMB_ID, breadcrumbId);
             }
-            String routeId = exchange.getFromRouteId();
+            // the route being run (falls back to the route that created the 
exchange)
+            String routeId = ExchangeHelper.getRouteId(exchange);
             if (routeId != null) {
                 MDC.put(MDC_ROUTE_ID, routeId);
             }
@@ -220,7 +222,7 @@ public class MDCService extends ServiceSupport implements 
CamelMDCService {
             if (breadcrumbId != null) {
                 MDC.remove(MDC_BREADCRUMB_ID);
             }
-            String routeId = exchange.getFromRouteId();
+            String routeId = ExchangeHelper.getRouteId(exchange);
             if (routeId != null) {
                 MDC.remove(MDC_ROUTE_ID);
             }
diff --git 
a/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCRouteIdTest.java 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCRouteIdTest.java
new file mode 100644
index 000000000000..f114fead6c0f
--- /dev/null
+++ 
b/components/camel-mdc/src/test/java/org/apache/camel/mdc/MDCRouteIdTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.Exchange;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.CamelLogger;
+import org.apache.camel.spi.LogListener;
+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;
+
+/**
+ * camel.routeId is the id of the route that runs the processor, also when a 
route calls another route.
+ */
+public class MDCRouteIdTest extends ExchangeTestSupport {
+
+    private final List<String> logged = new CopyOnWriteArrayList<>();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        MDCService mdcSvc = new MDCService();
+        CamelContext context = super.createCamelContext();
+        CamelContextAware.trySetCamelContext(mdcSvc, context);
+        mdcSvc.init(context);
+        // added after the MDC log listener, so it sees the MDC set for the log
+        context.getCamelContextExtension().addLogListener(new LogListener() {
+            @Override
+            public String onLog(Exchange exchange, CamelLogger camelLogger, 
String message) {
+                logged.add(message + "=" + MDC.get(MDCService.MDC_ROUTE_ID));
+                return message;
+            }
+        });
+        return context;
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:a").routeId("a")
+                        .process(e -> e.getMessage().setHeader("before", 
MDC.get(MDCService.MDC_ROUTE_ID)))
+                        .log("in a")
+                        .to("direct:b")
+                        .process(e -> e.getMessage().setHeader("after", 
MDC.get(MDCService.MDC_ROUTE_ID)))
+                        .log("back in a")
+                        .to("mock:a");
+
+                from("direct:b").routeId("b")
+                        .process(e -> e.getMessage().setHeader("inB", 
MDC.get(MDCService.MDC_ROUTE_ID)))
+                        .log("in b")
+                        .to("mock:b");
+            }
+        };
+    }
+
+    @Test
+    void testRouteIdIsTheRouteBeingRun() throws Exception {
+        MockEndpoint a = getMockEndpoint("mock:a");
+        a.expectedMessageCount(1);
+        a.expectedHeaderReceived("before", "a");
+        a.expectedHeaderReceived("inB", "b");
+        a.expectedHeaderReceived("after", "a");
+        MockEndpoint b = getMockEndpoint("mock:b");
+        b.expectedMessageCount(1);
+        b.expectedHeaderReceived("inB", "b");
+
+        template.sendBody("direct:a", "Hello");
+
+        MockEndpoint.assertIsSatisfied(context);
+        assertEquals(List.of("in a=a", "in b=b", "back in a=a"), logged);
+    }
+
+    @Test
+    void testRouteIdOfASingleRoute() throws Exception {
+        MockEndpoint b = getMockEndpoint("mock:b");
+        b.expectedMessageCount(1);
+        b.expectedHeaderReceived("inB", "b");
+
+        template.sendBody("direct:b", "Hello");
+
+        MockEndpoint.assertIsSatisfied(context);
+        assertEquals(List.of("in b=b"), logged);
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 0b35a7033514..f241dbdfff56 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -647,6 +647,12 @@ is now sent with the first request; before, the request 
failed with `IllegalStat
 when `authDomain` is not set. Before, the endpoint answered only Basic and 
Digest challenges with these credentials,
 and the component did not use them. If the server asks for Basic credentials, 
remove `authMethod=NTLM`.
 
+=== camel-mdc - camel.routeId is the route being run
+
+The `camel.routeId` MDC value is now the id of the route that runs the step, 
as with the deprecated
+`camel.main.useMdcLogging`. Before, it was the id of the route that created 
the exchange, so the log lines of a route
+called with `direct:` (or another route-to-route endpoint) showed the id of 
the calling route.
+
 === Java
 
 *Breaking change:* Camel 4.24 requires *Java 21* as the minimum runtime 
version.

Reply via email to