Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public Span create(String spanName, Span parent, SpanContextPropagationExtractor
if (parent != null) {
OpenTelemetrySpanAdapter otelParentSpan = (OpenTelemetrySpanAdapter) parent;
builder = builder.setParent(Context.current().with(otelParentSpan.getSpan()));
baggage = otelParentSpan.getBaggage();
if (baggage.isEmpty()) {
baggage = otelParentSpan.getBaggage();
}
} else {
Context current = Context.root();
// If the current span was generated by Camel, then, this is a "dirty" context.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.opentelemetry2;

import java.io.IOException;
import java.util.Map;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.opentelemetry2.CamelOpenTelemetryExtension.OtelTrace;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BaggageInjectionInternalTest extends OpenTelemetryTracerTestSupport {

Tracer tracer = otelExtension.getOpenTelemetry().getTracer("spanInjection");

@Override
protected CamelContext createCamelContext() throws Exception {
OpenTelemetryTracer tst = new OpenTelemetryTracer();
tst.setTracer(tracer);
tst.setContextPropagators(otelExtension.getOpenTelemetry().getPropagators());
tst.setTraceProcessors(true);
CamelContext context = super.createCamelContext();
CamelContextAware.trySetCamelContext(tst, context);
tst.init(context);
return context;
}

@Test
void testRouteExternalBaggage() throws IOException {
template.sendBody("direct:start", "my-body");
Map<String, OtelTrace> traces = otelExtension.getTraces();
assertEquals(1, traces.size());
}

@Override
protected RoutesBuilder createRouteBuilder() {
return new RouteBuilder() {

Scope baggageScope;

@Override
public void configure() {
from("direct:start")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Open the scope to propagate some baggage info
baggageScope = Baggage.current().toBuilder().put("my.id", "9876").build().makeCurrent();
}
})
.routeId("start")
.log("A message")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
assertEquals("9876", Baggage.current().getEntryValue("my.id"));
}
})
.to("log:info")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Close the scope previously opened
baggageScope.close();
}
});
}
};
}
}
Loading