|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.observation.web.servlet; |
| 18 | + |
| 19 | +import io.micrometer.observation.ObservationRegistry; |
| 20 | +import io.micrometer.tracing.Tracer; |
| 21 | +import jakarta.servlet.DispatcherType; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.ObjectProvider; |
| 24 | +import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 28 | +import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean; |
| 29 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.core.Ordered; |
| 33 | +import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; |
| 34 | +import org.springframework.http.server.observation.ServerRequestObservationConvention; |
| 35 | +import org.springframework.web.filter.ServerHttpObservationFilter; |
| 36 | + |
| 37 | +/** |
| 38 | + * Observation filter configurations imported by |
| 39 | + * {@link WebMvcObservationAutoConfiguration}. |
| 40 | + * |
| 41 | + * @author Brian Clozel |
| 42 | + */ |
| 43 | +abstract class ObservationFilterConfigurations { |
| 44 | + |
| 45 | + static <T extends ServerHttpObservationFilter> FilterRegistrationBean<T> filterRegistration(T filter) { |
| 46 | + FilterRegistrationBean<T> registration = new FilterRegistrationBean<>(filter); |
| 47 | + registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); |
| 48 | + registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC); |
| 49 | + return registration; |
| 50 | + } |
| 51 | + |
| 52 | + @Configuration(proxyBeanMethods = false) |
| 53 | + @ConditionalOnClass(Tracer.class) |
| 54 | + static class TracingHeaderObservation { |
| 55 | + |
| 56 | + @Bean |
| 57 | + @ConditionalOnProperty(prefix = "management.observations.http.server.requests", name = "write-trace-header") |
| 58 | + @ConditionalOnBean(Tracer.class) |
| 59 | + @ConditionalOnMissingFilterBean({ ServerHttpObservationFilter.class, TraceHeaderObservationFilter.class }) |
| 60 | + FilterRegistrationBean<TraceHeaderObservationFilter> webMvcObservationFilter(ObservationRegistry registry, |
| 61 | + Tracer tracer, ObjectProvider<ServerRequestObservationConvention> customConvention, |
| 62 | + ObservationProperties observationProperties) { |
| 63 | + String name = observationProperties.getHttp().getServer().getRequests().getName(); |
| 64 | + ServerRequestObservationConvention convention = customConvention |
| 65 | + .getIfAvailable(() -> new DefaultServerRequestObservationConvention(name)); |
| 66 | + TraceHeaderObservationFilter filter = new TraceHeaderObservationFilter(tracer, registry, convention); |
| 67 | + return filterRegistration(filter); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Configuration(proxyBeanMethods = false) |
| 73 | + static class DefaultObservation { |
| 74 | + |
| 75 | + @Bean |
| 76 | + @ConditionalOnMissingFilterBean({ ServerHttpObservationFilter.class, TraceHeaderObservationFilter.class }) |
| 77 | + FilterRegistrationBean<ServerHttpObservationFilter> webMvcObservationFilter(ObservationRegistry registry, |
| 78 | + ObjectProvider<ServerRequestObservationConvention> customConvention, |
| 79 | + ObservationProperties observationProperties) { |
| 80 | + String name = observationProperties.getHttp().getServer().getRequests().getName(); |
| 81 | + ServerRequestObservationConvention convention = customConvention |
| 82 | + .getIfAvailable(() -> new DefaultServerRequestObservationConvention(name)); |
| 83 | + ServerHttpObservationFilter filter = new ServerHttpObservationFilter(registry, convention); |
| 84 | + return filterRegistration(filter); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments