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 07bd0776b7fe CAMEL-26683: camel-opentelemetry - 
OpenTelemetryTracingStrategy: find the correlation context processors behind 
other intercept strategies (#29431)
07bd0776b7fe is described below

commit 07bd0776b7fe439de7e17bc1cc95becf5083bac7
Author: allthingssecurity <[email protected]>
AuthorDate: Mon Nov 9 15:04:06 2026 +0530

    CAMEL-26683: camel-opentelemetry - OpenTelemetryTracingStrategy: find the 
correlation context processors behind other intercept strategies (#29431)
    
    * CAMEL-26683: camel-opentelemetry - OpenTelemetryTracingStrategy: find the 
correlation context processors behind other intercept strategies
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../OpenTelemetryTracingStrategy.java              | 23 ++++-
 ...lemetryTracingStrategyWrappedProcessorTest.java | 97 ++++++++++++++++++++++
 2 files changed, 116 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-opentelemetry/src/main/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategy.java
 
b/components/camel-opentelemetry/src/main/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategy.java
index 417f4283cabf..bdccd18a42d5 100644
--- 
a/components/camel-opentelemetry/src/main/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategy.java
+++ 
b/components/camel-opentelemetry/src/main/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategy.java
@@ -21,6 +21,7 @@ import io.opentelemetry.api.trace.StatusCode;
 import io.opentelemetry.context.Context;
 import io.opentelemetry.context.Scope;
 import org.apache.camel.CamelContext;
+import org.apache.camel.DelegateProcessor;
 import org.apache.camel.Exchange;
 import org.apache.camel.NamedNode;
 import org.apache.camel.Processor;
@@ -91,8 +92,7 @@ public class OpenTelemetryTracingStrategy implements 
InterceptStrategy {
                     .setAttribute("component", 
getComponentName(processorDefinition))
                     .startSpan();
 
-            boolean activateExchange = !(target instanceof 
GetCorrelationContextProcessor
-                    || target instanceof SetCorrelationContextProcessor);
+            boolean activateExchange = !isCorrelationContextProcessor(target);
 
             if (activateExchange) {
                 ActiveSpanManager.activate(exchange, new 
OpenTelemetrySpanAdapter(processorSpan));
@@ -133,8 +133,7 @@ public class OpenTelemetryTracingStrategy implements 
InterceptStrategy {
                 return;
             }
 
-            boolean activateExchange = !(target instanceof 
GetCorrelationContextProcessor
-                    || target instanceof SetCorrelationContextProcessor);
+            boolean activateExchange = !isCorrelationContextProcessor(target);
 
             if (activateExchange) {
                 ActiveSpanManager.activate(exchange, new 
OpenTelemetrySpanAdapter(span));
@@ -154,6 +153,22 @@ public class OpenTelemetryTracingStrategy implements 
InterceptStrategy {
         }
     }
 
+    // the target may be wrapped by intercept strategies that were applied 
before this one, so look through them
+    private static boolean isCorrelationContextProcessor(Processor target) {
+        Processor p = target;
+        while (p != null) {
+            if (p instanceof GetCorrelationContextProcessor || p instanceof 
SetCorrelationContextProcessor) {
+                return true;
+            }
+            if (p instanceof DelegateProcessor dp) {
+                p = dp.getProcessor();
+            } else {
+                break;
+            }
+        }
+        return false;
+    }
+
     private static String getComponentName(NamedNode processorDefinition) {
         return SpanDecorator.CAMEL_COMPONENT + 
processorDefinition.getShortName();
     }
diff --git 
a/components/camel-opentelemetry/src/test/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategyWrappedProcessorTest.java
 
b/components/camel-opentelemetry/src/test/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategyWrappedProcessorTest.java
new file mode 100644
index 000000000000..bb7f15a154a0
--- /dev/null
+++ 
b/components/camel-opentelemetry/src/test/java/org/apache/camel/opentelemetry/OpenTelemetryTracingStrategyWrappedProcessorTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.opentelemetry;
+
+import java.util.function.Function;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.support.processor.DelegateAsyncProcessor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Another intercept strategy wraps the processors before the OpenTelemetry 
strategy does. The OpenTelemetry strategy
+ * must still recognise the correlation context processors, as otherwise the 
baggage is set on a context of its own and
+ * lost.
+ */
+@Deprecated(since = "4.19.0")
+class OpenTelemetryTracingStrategyWrappedProcessorTest extends 
CamelOpenTelemetryTestSupport {
+
+    OpenTelemetryTracingStrategyWrappedProcessorTest() {
+        super(new SpanTestData[0]);
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        // added after the OpenTelemetry strategy, so it is applied first and 
the OpenTelemetry strategy gets its wrapper
+        InterceptStrategy wrapping = (camelContext, definition, target, 
nextTarget) -> new DelegateAsyncProcessor(target);
+        context.getCamelContextExtension().addInterceptStrategy(wrapping);
+        return context;
+    }
+
+    @Override
+    protected String getExcludePatterns() {
+        return "propagate-*";
+    }
+
+    @Override
+    protected Function<OpenTelemetryTracer, InterceptStrategy> 
getTracingStrategy() {
+        return tracer -> {
+            OpenTelemetryTracingStrategy strategy = new 
OpenTelemetryTracingStrategy(tracer);
+            strategy.setPropagateContext(true);
+            return strategy;
+        };
+    }
+
+    @Test
+    void correlationContextProcessorsWithProcessorSpans() {
+        Exchange result = template.request("direct:traced", exchange -> 
exchange.getIn().setBody("Hello"));
+
+        assertEquals("baggage-value", 
result.getMessage().getHeader("baggage-header", String.class));
+    }
+
+    @Test
+    void correlationContextProcessorsWithPropagatedContext() {
+        Exchange result = template.request("direct:propagated", exchange -> 
exchange.getIn().setBody("Hello"));
+
+        assertEquals("baggage-value", 
result.getMessage().getHeader("baggage-header", String.class));
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:traced").routeId("traced")
+                        .process(new 
SetCorrelationContextProcessor("a-baggage", 
constant("baggage-value"))).id("set")
+                        .process(new 
GetCorrelationContextProcessor("a-baggage", "baggage-header")).id("get");
+
+                // excluded from tracing, so the strategy only propagates the 
context to these processors
+                from("direct:propagated").routeId("propagated")
+                        .process(new 
SetCorrelationContextProcessor("a-baggage", constant("baggage-value")))
+                        .id("propagate-set")
+                        .process(new 
GetCorrelationContextProcessor("a-baggage", 
"baggage-header")).id("propagate-get");
+            }
+        };
+    }
+}

Reply via email to