Skip to content

Commit e6d8eba

Browse files
committed
chore(components): configure baggage in process
Ref CAMEL-23349
1 parent 122ed1a commit e6d8eba

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

components/camel-opentelemetry2/src/main/java/org/apache/camel/opentelemetry2/OpenTelemetryTracer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public Span create(String spanName, Span parent, SpanContextPropagationExtractor
108108
if (parent != null) {
109109
OpenTelemetrySpanAdapter otelParentSpan = (OpenTelemetrySpanAdapter) parent;
110110
builder = builder.setParent(Context.current().with(otelParentSpan.getSpan()));
111-
baggage = otelParentSpan.getBaggage();
111+
if (baggage.isEmpty()) {
112+
baggage = otelParentSpan.getBaggage();
113+
}
112114
} else {
113115
Context current = Context.root();
114116
// If the current span was generated by Camel, then, this is a "dirty" context.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.opentelemetry2;
18+
19+
import java.io.IOException;
20+
import java.util.Map;
21+
22+
import io.opentelemetry.api.baggage.Baggage;
23+
import io.opentelemetry.api.trace.Tracer;
24+
import io.opentelemetry.context.Scope;
25+
import org.apache.camel.CamelContext;
26+
import org.apache.camel.CamelContextAware;
27+
import org.apache.camel.Exchange;
28+
import org.apache.camel.Processor;
29+
import org.apache.camel.RoutesBuilder;
30+
import org.apache.camel.builder.RouteBuilder;
31+
import org.apache.camel.opentelemetry2.CamelOpenTelemetryExtension.OtelTrace;
32+
import org.junit.jupiter.api.Test;
33+
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
36+
public class BaggageInjectionInternalTest extends OpenTelemetryTracerTestSupport {
37+
38+
Tracer tracer = otelExtension.getOpenTelemetry().getTracer("spanInjection");
39+
40+
@Override
41+
protected CamelContext createCamelContext() throws Exception {
42+
OpenTelemetryTracer tst = new OpenTelemetryTracer();
43+
tst.setTracer(tracer);
44+
tst.setContextPropagators(otelExtension.getOpenTelemetry().getPropagators());
45+
tst.setTraceProcessors(true);
46+
CamelContext context = super.createCamelContext();
47+
CamelContextAware.trySetCamelContext(tst, context);
48+
tst.init(context);
49+
return context;
50+
}
51+
52+
@Test
53+
void testRouteExternalBaggage() throws IOException {
54+
template.sendBody("direct:start", "my-body");
55+
Map<String, OtelTrace> traces = otelExtension.getTraces();
56+
assertEquals(1, traces.size());
57+
}
58+
59+
@Override
60+
protected RoutesBuilder createRouteBuilder() {
61+
return new RouteBuilder() {
62+
63+
Scope baggageScope;
64+
65+
@Override
66+
public void configure() {
67+
from("direct:start")
68+
.process(new Processor() {
69+
@Override
70+
public void process(Exchange exchange) throws Exception {
71+
// Open the scope to propagate some baggage info
72+
baggageScope = Baggage.current().toBuilder().put("my.id", "9876").build().makeCurrent();
73+
}
74+
})
75+
.routeId("start")
76+
.log("A message")
77+
.process(new Processor() {
78+
@Override
79+
public void process(Exchange exchange) throws Exception {
80+
assertEquals("9876", Baggage.current().getEntryValue("my.id"));
81+
}
82+
})
83+
.to("log:info")
84+
.process(new Processor() {
85+
@Override
86+
public void process(Exchange exchange) throws Exception {
87+
// Close the scope previously opened
88+
baggageScope.close();
89+
}
90+
});
91+
}
92+
};
93+
}
94+
}

0 commit comments

Comments
 (0)