1+ /**
2+ * Copyright 2016-2019 The OpenTracing Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+ * or implied. See the License for the specific language governing permissions and limitations under
12+ * the License.
13+ */
14+ package io .opentracing .contrib .spring .web .starter ;
15+
16+ import java .util .ArrayList ;
17+ import java .util .Collection ;
18+ import java .util .List ;
19+ import java .util .Optional ;
20+ import java .util .regex .Pattern ;
21+ import java .util .stream .Collectors ;
22+
23+ import org .springframework .beans .factory .annotation .Autowired ;
24+ import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointProperties ;
25+ import org .springframework .boot .actuate .autoconfigure .web .server .ConditionalOnManagementPort ;
26+ import org .springframework .boot .actuate .autoconfigure .web .server .ManagementPortType ;
27+ import org .springframework .boot .actuate .autoconfigure .web .server .ManagementServerProperties ;
28+ import org .springframework .boot .actuate .endpoint .EndpointsSupplier ;
29+ import org .springframework .boot .actuate .endpoint .web .ExposableWebEndpoint ;
30+ import org .springframework .boot .actuate .endpoint .web .PathMappedEndpoint ;
31+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
32+ import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
33+ import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
34+ import org .springframework .boot .autoconfigure .web .ServerProperties ;
35+ import org .springframework .context .annotation .Bean ;
36+ import org .springframework .context .annotation .Configuration ;
37+ import org .springframework .util .StringUtils ;
38+
39+ import io .opentracing .contrib .spring .web .webfilter .SkipPattern ;
40+
41+ /**
42+ * @author Gilles Robert
43+ */
44+ @ Configuration
45+ @ ConditionalOnProperty (name = "opentracing.spring.web.enabled" , havingValue = "true" , matchIfMissing = true )
46+ public class SkipPatternAutoConfiguration {
47+
48+ @ Autowired (required = false )
49+ private List <SkipPattern > patterns = new ArrayList <>();
50+
51+ @ Bean (name = "skipPattern" )
52+ public Pattern skipPattern () {
53+ return Pattern .compile (this .patterns
54+ .stream ()
55+ .map (SkipPattern ::pattern )
56+ .filter (Optional ::isPresent ).map (Optional ::get )
57+ .map (Pattern ::pattern )
58+ .collect (Collectors .joining ("|" )));
59+ }
60+
61+ @ Configuration
62+ @ ConditionalOnClass (ManagementServerProperties .class )
63+ @ ConditionalOnProperty (value = "opentracing.spring.web.ignoreAutoConfiguredSkipPatterns" , havingValue = "false" , matchIfMissing = true )
64+ protected static class ManagementSkipPatternProviderConfig {
65+
66+ static Optional <Pattern > getPatternForManagementServerProperties (
67+ ManagementServerProperties managementServerProperties ) {
68+ String contextPath = managementServerProperties .getServlet ().getContextPath ();
69+ if (StringUtils .hasText (contextPath )) {
70+ return Optional .of (Pattern .compile (contextPath + ".*" ));
71+ }
72+ return Optional .empty ();
73+ }
74+
75+ @ Bean
76+ @ ConditionalOnBean (ManagementServerProperties .class )
77+ public SkipPattern skipPatternForManagementServerProperties (
78+ final ManagementServerProperties managementServerProperties ) {
79+ return () -> getPatternForManagementServerProperties (managementServerProperties );
80+ }
81+ }
82+
83+ @ Configuration
84+ @ ConditionalOnClass ( {ServerProperties .class , EndpointsSupplier .class , ExposableWebEndpoint .class })
85+ @ ConditionalOnBean (ServerProperties .class )
86+ @ ConditionalOnProperty (value = "opentracing.spring.web.ignoreAutoConfiguredSkipPatterns" , havingValue = "false" , matchIfMissing = true )
87+ protected static class ActuatorSkipPatternProviderConfig {
88+
89+ static Optional <Pattern > getEndpointsPatterns (String contextPath ,
90+ WebEndpointProperties webEndpointProperties ,
91+ EndpointsSupplier <ExposableWebEndpoint > endpointsSupplier ) {
92+ Collection <ExposableWebEndpoint > endpoints = endpointsSupplier .getEndpoints ();
93+
94+ if (endpoints .isEmpty ()) {
95+ return Optional .empty ();
96+ }
97+
98+ String pattern = endpoints .stream ().map (PathMappedEndpoint ::getRootPath )
99+ .map (path -> path + "|" + path + "/.*" ).collect (
100+ Collectors .joining ("|" ,
101+ getPathPrefix (contextPath ,
102+ webEndpointProperties .getBasePath ()) + "/(" ,
103+ ")" ));
104+ if (StringUtils .hasText (pattern )) {
105+ return Optional .of (Pattern .compile (pattern ));
106+ }
107+ return Optional .empty ();
108+ }
109+
110+ private static String getPathPrefix (String contextPath , String actuatorBasePath ) {
111+ String result = "" ;
112+ if (StringUtils .hasText (contextPath )) {
113+ result += contextPath ;
114+ }
115+ if (!actuatorBasePath .equals ("/" )) {
116+ result += actuatorBasePath ;
117+ }
118+ return result ;
119+ }
120+
121+ @ Bean
122+ @ ConditionalOnManagementPort (ManagementPortType .SAME )
123+ public SkipPattern skipPatternForActuatorEndpointsSamePort (
124+ final ServerProperties serverProperties ,
125+ final WebEndpointProperties webEndpointProperties ,
126+ final EndpointsSupplier <ExposableWebEndpoint > endpointsSupplier ) {
127+ return () -> getEndpointsPatterns (
128+ serverProperties .getServlet ().getContextPath (), webEndpointProperties ,
129+ endpointsSupplier );
130+ }
131+
132+ @ Bean
133+ @ ConditionalOnManagementPort (ManagementPortType .DIFFERENT )
134+ @ ConditionalOnProperty (name = "management.server.servlet.context-path" , havingValue = "/" , matchIfMissing = true )
135+ public SkipPattern skipPatternForActuatorEndpointsDifferentPort (
136+ final WebEndpointProperties webEndpointProperties ,
137+ final EndpointsSupplier <ExposableWebEndpoint > endpointsSupplier ) {
138+ return () -> getEndpointsPatterns (null , webEndpointProperties ,
139+ endpointsSupplier );
140+ }
141+ }
142+
143+ @ Configuration
144+ protected static class DefaultSkipPatternConfig {
145+
146+ private static String combinedPatterns (String skipPattern ) {
147+ String pattern = skipPattern ;
148+ if (!StringUtils .hasText (skipPattern )) {
149+ pattern = WebTracingProperties .DEFAULT_SKIP_PATTERN ;
150+ }
151+ return pattern ;
152+ }
153+
154+ @ Bean
155+ SkipPattern defaultSkipPatternBean (WebTracingProperties webTracingProperties ) {
156+ return () -> Optional .of (Pattern .compile (combinedPatterns (webTracingProperties .getSkipPattern ())));
157+ }
158+ }
159+
160+ }
0 commit comments