From 4e801207f2413544b46f7558f950d472dd22e362 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 10 Feb 2023 13:52:55 +0200 Subject: [PATCH] Fix autoconfigure for spring boot 3 --- .../build.gradle.kts | 2 ++ .../webmvc/WebMvcFilterAutoConfiguration.java | 2 +- .../WebMvcFilterAutoConfigurationSpring6.java | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6.java diff --git a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts index ef69cf871cea..7bbe6d1dc15f 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts +++ b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts @@ -19,6 +19,8 @@ dependencies { implementation(project(":instrumentation:spring:spring-kafka-2.7:library")) implementation(project(":instrumentation:spring:spring-web:spring-web-3.1:library")) implementation(project(":instrumentation:spring:spring-webmvc:spring-webmvc-5.3:library")) + implementation(project(":instrumentation:spring:spring-webmvc:spring-webmvc-6.0:library")) + compileOnly("jakarta.servlet:jakarta.servlet-api:5.0.0") implementation(project(":instrumentation:spring:spring-webflux-5.0:library")) implementation(project(":instrumentation:micrometer:micrometer-1.5:library")) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfiguration.java index 8ffdf2895a67..3b8f0af52b8f 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfiguration.java @@ -21,7 +21,7 @@ @Configuration @EnableConfigurationProperties(WebMvcProperties.class) @ConditionalOnProperty(prefix = "otel.springboot.web", name = "enabled", matchIfMissing = true) -@ConditionalOnClass({OncePerRequestFilter.class, DispatcherServlet.class}) +@ConditionalOnClass({Filter.class, OncePerRequestFilter.class, DispatcherServlet.class}) @ConditionalOnBean(OpenTelemetry.class) public class WebMvcFilterAutoConfiguration { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6.java new file mode 100644 index 000000000000..0eec311f6724 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6.java @@ -0,0 +1,32 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.spring.webmvc.v6_0.SpringWebMvcTelemetry; +import jakarta.servlet.Filter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.OncePerRequestFilter; +import org.springframework.web.servlet.DispatcherServlet; + +/** Configures {@link SpringWebMvcTelemetry} for tracing. */ +@Configuration +@EnableConfigurationProperties(WebMvcProperties.class) +@ConditionalOnProperty(prefix = "otel.springboot.web", name = "enabled", matchIfMissing = true) +@ConditionalOnClass({Filter.class, OncePerRequestFilter.class, DispatcherServlet.class}) +@ConditionalOnBean(OpenTelemetry.class) +public class WebMvcFilterAutoConfigurationSpring6 { + + @Bean + public Filter otelWebMvcInstrumentationFilter(OpenTelemetry openTelemetry) { + return SpringWebMvcTelemetry.create(openTelemetry).createServletFilter(); + } +}