From c719453d99e79d25f71b2c3117d61281c1abc685 Mon Sep 17 00:00:00 2001 From: James Moessis Date: Thu, 7 Nov 2024 16:18:47 +1100 Subject: [PATCH] Implements getList() in the Spring Web MVC instrumentation's JakartaHttpServletRequestGetter. Implements a test to demonstrate the new W3CBaggagePropagator working with multiple baggage headers (using a locally published version of opentelemetry-api and opentelemetry-context). The TextMapGetter operates on a Jakarta servlet HttpServletRequest, a very common interface used in many servers. This demonstrates that using getList() will work in many area / servers. --- .../library/build.gradle.kts | 12 ++++++++ .../v6_0/JakartaHttpServletRequestGetter.java | 7 +++++ .../webmvc/v6_0/NewBaggagePropagatorTest.java | 30 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/test/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/NewBaggagePropagatorTest.java diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/build.gradle.kts b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/build.gradle.kts index 18188322f6e3..7d0e1bf92ce2 100644 --- a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/build.gradle.kts +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/build.gradle.kts @@ -2,6 +2,18 @@ plugins { id("otel.library-instrumentation") } +repositories { + mavenLocal() + mavenCentral() +} + +configurations.all { + resolutionStrategy { + force("io.opentelemetry:opentelemetry-context:1.44.0-SNAPSHOT") + force("io.opentelemetry:opentelemetry-api:1.44.0-SNAPSHOT") + } +} + dependencies { compileOnly("org.springframework:spring-webmvc:6.0.0") compileOnly("jakarta.servlet:jakarta.servlet-api:5.0.0") diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/JakartaHttpServletRequestGetter.java b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/JakartaHttpServletRequestGetter.java index 2955fb5d7b25..6270fb4790a5 100644 --- a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/JakartaHttpServletRequestGetter.java +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/JakartaHttpServletRequestGetter.java @@ -7,7 +7,9 @@ import io.opentelemetry.context.propagation.TextMapGetter; import jakarta.servlet.http.HttpServletRequest; +import javax.annotation.Nullable; import java.util.Collections; +import java.util.List; enum JakartaHttpServletRequestGetter implements TextMapGetter { INSTANCE; @@ -21,4 +23,9 @@ public Iterable keys(HttpServletRequest carrier) { public String get(HttpServletRequest carrier, String key) { return carrier.getHeader(key); } + + @Override + public List getList(@Nullable HttpServletRequest carrier, String key) { + return Collections.list(carrier.getHeaders(key)); + } } diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/test/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/NewBaggagePropagatorTest.java b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/test/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/NewBaggagePropagatorTest.java new file mode 100644 index 000000000000..fa078c3be406 --- /dev/null +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/test/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/NewBaggagePropagatorTest.java @@ -0,0 +1,30 @@ +package io.opentelemetry.instrumentation.spring.webmvc.v6_0; + +import io.opentelemetry.api.baggage.Baggage; +import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; +import io.opentelemetry.context.Context; +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +import static org.assertj.core.api.Assertions.assertThat; + +class NewBaggagePropagatorTest { + @Test + void testBaggageMultipleHeaders() { + W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance(); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("baggage", "k1=v1"); + request.addHeader("baggage", "k2=v2"); + + Context result = + propagator.extract( + Context.root(), + request, + JakartaHttpServletRequestGetter.INSTANCE); + + Baggage expectedBaggage = Baggage.builder().put("k1", "v1").put("k2", "v2").build(); + assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage); + } +} +