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

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

commit f7a8ab401e50de53cb0fd28e46f9892af889050b
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Wed May 27 09:21:51 2026 +0200

    feat(components): otel2 baggage from properties
    
    We uniform the logic after the change developed for the other telemetry 
components
---
 .../src/main/docs/opentelemetry2.adoc              |  7 +++---
 .../TraceProcessorsOtelInterceptStrategy.java      | 29 ++++++++++++++++++++--
 .../camel/opentelemetry2/BaggageSettingTest.java   |  4 +--
 3 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/components/camel-opentelemetry2/src/main/docs/opentelemetry2.adoc 
b/components/camel-opentelemetry2/src/main/docs/opentelemetry2.adoc
index ef42bad2eaa4..5d66034680c4 100644
--- a/components/camel-opentelemetry2/src/main/docs/opentelemetry2.adoc
+++ b/components/camel-opentelemetry2/src/main/docs/opentelemetry2.adoc
@@ -241,18 +241,17 @@ For this reason, whenever you need to provide custom 
telemetry information, it i
 
 === Baggage customization
 
-`Baggage` is a way to attach key-value metadata to a request and carry it 
across service boundaries. In the context of OpenTelemetry, baggage travels 
along with the context (like trace/span), but it's meant for custom data you 
define, not telemetry internals. Camel allows you to programmatically provide 
any `Baggage` information via header settings. Whenever the component finds an 
header defined as `OTEL_BAGGAGE_xyz` it will consider it as a baggage variable 
named `xyz`. For example, in  [...]
+`Baggage` is a way to attach key-value metadata to a request and carry it 
across service boundaries. In the context of OpenTelemetry, baggage travels 
along with the context (like trace/span), but it's meant for custom data you 
define, not telemetry internals. Camel allows you to programmatically provide 
any `Baggage` information via Exchange property settings. Whenever the 
component finds a property defined as `CamelBaggage_xyz` it will consider it as 
a baggage variable named `xyz`. For  [...]
 
 [source,java]
 ----
                 from("direct:start")
-                        .setHeader("OTEL_BAGGAGE_myValue", constant("1234"))
+                        .setProperty("CamelBaggage_myValue", constant("1234"))
                         .routeId("start")
                         .log("A message")
                         .process(new Processor() {
                             @Override
                             public void process(Exchange exchange) throws 
Exception {
-                                exchange.getIn().setHeader("operation", 
"fake");
                                 // Baggage is available via the OpenTelemetry 
API
                                 String val = 
Baggage.current().getEntryValue("myValue");
                             }
@@ -260,6 +259,6 @@ For this reason, whenever you need to provide custom 
telemetry information, it i
                         .to("log:info");
 ----
 
-Any span executed after the `setHeader` will include a baggage variable named 
`myValue` with value `1234` which will be reflected in your telemetry result.
+Any span executed after the `setProperty` will include a baggage variable 
named `myValue` with value `1234` which will be reflected in your telemetry 
result.
 
 NOTE: any baggage setting defined externally (i.e., calling the Camel process 
with a context propagation) is normally going to be propagated in Camel logic.
diff --git 
a/components/camel-opentelemetry2/src/main/java/org/apache/camel/opentelemetry2/TraceProcessorsOtelInterceptStrategy.java
 
b/components/camel-opentelemetry2/src/main/java/org/apache/camel/opentelemetry2/TraceProcessorsOtelInterceptStrategy.java
index 813ce637d16f..23e0165cdb19 100644
--- 
a/components/camel-opentelemetry2/src/main/java/org/apache/camel/opentelemetry2/TraceProcessorsOtelInterceptStrategy.java
+++ 
b/components/camel-opentelemetry2/src/main/java/org/apache/camel/opentelemetry2/TraceProcessorsOtelInterceptStrategy.java
@@ -18,6 +18,7 @@ package org.apache.camel.opentelemetry2;
 
 import java.util.concurrent.CompletableFuture;
 
+import io.opentelemetry.api.baggage.Baggage;
 import io.opentelemetry.context.Scope;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelContext;
@@ -62,8 +63,9 @@ public class TraceProcessorsOtelInterceptStrategy implements 
InterceptStrategy {
             Span activeSpan = spanStorage.peek(exchange);
             if (activeSpan != null) {
                 OpenTelemetrySpanAdapter otelSpan = (OpenTelemetrySpanAdapter) 
activeSpan;
+                Baggage baggage = 
getBaggageFromProperties(otelSpan.getBaggage(), exchange);
                 try (Scope scope = otelSpan.getSpan().makeCurrent();
-                     Scope baggageScope = otelSpan.getBaggage().makeCurrent()) 
{
+                     Scope baggageScope = baggage.makeCurrent()) {
                     processor.process(exchange);
                 }
             } else {
@@ -76,8 +78,9 @@ public class TraceProcessorsOtelInterceptStrategy implements 
InterceptStrategy {
             Span activeSpan = spanStorage.peek(exchange);
             if (activeSpan != null) {
                 OpenTelemetrySpanAdapter otelSpan = (OpenTelemetrySpanAdapter) 
activeSpan;
+                Baggage baggage = 
getBaggageFromProperties(otelSpan.getBaggage(), exchange);
                 try (Scope scope = otelSpan.getSpan().makeCurrent();
-                     Scope baggageScope = otelSpan.getBaggage().makeCurrent()) 
{
+                     Scope baggageScope = baggage.makeCurrent()) {
                     return processor.process(exchange, doneSync -> {
                         callback.done(doneSync);
                     });
@@ -98,4 +101,26 @@ public class TraceProcessorsOtelInterceptStrategy 
implements InterceptStrategy {
         }
     }
 
+    // We inspect the exchange in order to find any baggage variable
+    private Baggage getBaggageFromProperties(Baggage baggage, Exchange 
exchange) {
+        for (String propertyKey : exchange.getProperties().keySet()) {
+            String key = getBaggageVar(propertyKey);
+            if (key != null) {
+                String value = exchange.getProperty(propertyKey) == null
+                        ? null : exchange.getProperty(propertyKey).toString();
+                baggage = baggage.toBuilder().put(key, value).build();
+            }
+        }
+
+        return baggage;
+    }
+
+    private String getBaggageVar(String key) {
+        if (key == null || 
!key.startsWith(org.apache.camel.telemetry.Tracer.BAGGAGE_PROPERTY)) {
+            return null;
+        }
+
+        return 
key.substring(org.apache.camel.telemetry.Tracer.BAGGAGE_PROPERTY.length());
+    }
+
 }
diff --git 
a/components/camel-opentelemetry2/src/test/java/org/apache/camel/opentelemetry2/BaggageSettingTest.java
 
b/components/camel-opentelemetry2/src/test/java/org/apache/camel/opentelemetry2/BaggageSettingTest.java
index dbbc1fd0710f..f953f0098564 100644
--- 
a/components/camel-opentelemetry2/src/test/java/org/apache/camel/opentelemetry2/BaggageSettingTest.java
+++ 
b/components/camel-opentelemetry2/src/test/java/org/apache/camel/opentelemetry2/BaggageSettingTest.java
@@ -87,14 +87,12 @@ public class BaggageSettingTest extends 
OpenTelemetryTracerTestSupport {
             @Override
             public void configure() {
                 from("direct:start")
-                        .setHeader("OTEL_BAGGAGE_tenant.id", constant("1234"))
+                        .setProperty("CamelBaggage_tenant.id", 
constant("1234"))
                         .routeId("start")
                         .log("A message")
                         .process(new Processor() {
                             @Override
                             public void process(Exchange exchange) throws 
Exception {
-                                exchange.getIn().setHeader("operation", 
"fake");
-
                                 assertEquals("1234", 
Baggage.current().getEntryValue("tenant.id"));
                             }
                         })

Reply via email to