Skip to content

Commit

Permalink
[3.x ] - Fix OpenTracingSpan Baggage propagation issue (#6987)
Browse files Browse the repository at this point in the history
* Fix OpenTracingSpan Baggage propagation issue
  • Loading branch information
dalexandrov authored Jun 15, 2023
1 parent 85e7e33 commit d785d8b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 2 deletions.
51 changes: 51 additions & 0 deletions tests/integration/gh-6970/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-se</artifactId>
<version>3.2.2-SNAPSHOT</version>
<relativePath>../../../applications/se/pom.xml</relativePath>
</parent>
<groupId>io.helidon.tests.integration</groupId>
<artifactId>helidon-tests-integration-tracer-baggage</artifactId>
<version>3.2.2-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing-jaeger</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed 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 io.helidon.tests.integration.tracerbaggage;

import io.helidon.config.Config;
import io.helidon.tracing.HeaderConsumer;
import io.helidon.tracing.HeaderProvider;
import io.helidon.tracing.Span;
import io.helidon.tracing.Tracer;
import io.helidon.tracing.TracerBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

class MainTest {

public static final String KEY = "fubar";
public static final String VALUE = "1";

@BeforeAll
static void prepareTracer() {
Config config = Config.create();

TracerBuilder.create(config.get("tracing"))
.serviceName("helidon-service")
.registerGlobal(true)
.build();


}

/**
* Test for: https://github.com/helidon-io/helidon/issues/6970
*/
@Test
void baggageCanaryMinimal() {
Tracer tracer = Tracer.global();
Span span = tracer.spanBuilder("baggageCanaryMinimal").start();
// Set baggage and confirm that it's known in the span
span.baggage(KEY, VALUE);
assertThat(span.baggage(KEY).orElse(null), is(VALUE));

// Inject the span (context) into the consumer
HeaderConsumer consumer = HeaderConsumer
.create(new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
tracer.inject(span.context(), HeaderProvider.empty(), consumer);

// Check baggage propagated
List<String> result = new ArrayList<>();
consumer.keys().forEach(result::add);
assertThat(result, hasItem(containsString(KEY)));

//Check baggage value
String fubar = result.stream().filter(e -> e.contains(KEY)).findFirst().orElseThrow();
assertThat(consumer.get(fubar).orElseThrow(), is(VALUE));
span.end();
}

}
20 changes: 20 additions & 0 deletions tests/integration/gh-6970/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright (c) 2023 Oracle and/or its affiliates.
#
# Licensed 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.
#

tracing:
service: "helidon-service"
protocol: "https"
host: "jaeger"
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>oidc</module>
<module>gh-5792</module>
<module>se-gh-6845</module>
<module>gh-6970</module>
</modules>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public Span baggage(String key, String value) {
Baggage.builder()
.put(key, value)
.build()
.storeInContext(getContext())
.storeInContext(getContext()
.with(delegate))
.makeCurrent();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ void testBaggage() {
Span span = tracer.spanBuilder("test-span").start();
Span spanWithBaggage = span.baggage("key", "value");
Optional<String> result = spanWithBaggage.baggage("key");
span.end();
assertThat(result.isPresent(), is(true));
assertThat(result.get(), equalTo("value"));
span.end();
}

@Test
Expand Down

0 comments on commit d785d8b

Please sign in to comment.