3
3
import io .prometheus .client .Histogram ;
4
4
import org .springframework .beans .factory .annotation .Autowired ;
5
5
import org .springframework .beans .factory .annotation .Value ;
6
- import org .springframework .web .method .HandlerMethod ;
6
+ import org .springframework .context .ApplicationContext ;
7
+ import org .springframework .data .rest .core .config .RepositoryRestConfiguration ;
8
+ import org .springframework .data .rest .core .mapping .ResourceMappings ;
9
+ import org .springframework .data .rest .webmvc .RepositoryRestHandlerMapping ;
10
+ import org .springframework .data .rest .webmvc .support .JpaHelper ;
7
11
import org .springframework .web .servlet .HandlerInterceptor ;
8
12
import org .springframework .web .servlet .ModelAndView ;
9
- import org .springframework .web .servlet .mvc .method . RequestMappingInfo ;
13
+ import org .springframework .web .servlet .mvc .condition . PatternsRequestCondition ;
10
14
import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerMapping ;
11
15
12
16
import javax .servlet .http .HttpServletRequest ;
13
17
import javax .servlet .http .HttpServletResponse ;
14
- import java .util .Map ;
18
+ import java .util .HashSet ;
19
+ import java .util .Set ;
15
20
16
21
public class HTTPMonitoringInterceptor implements HandlerInterceptor {
17
22
static final Histogram requestLatency = Histogram .build ()
@@ -21,12 +26,19 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
21
26
.register ();
22
27
23
28
private static final String startTimeKey = "startTime" ;
24
-
25
- @ Value ("${spring.application.name:carts}" )
26
- private String serviceName ;
27
-
28
29
@ Autowired
29
- private RequestMappingHandlerMapping requestMappingHandlerMapping ;
30
+ ResourceMappings mappings ;
31
+ @ Autowired
32
+ JpaHelper jpaHelper ;
33
+ @ Autowired
34
+ RepositoryRestConfiguration repositoryConfiguration ;
35
+ @ Autowired
36
+ ApplicationContext applicationContext ;
37
+ @ Autowired
38
+ RequestMappingHandlerMapping requestMappingHandlerMapping ;
39
+ private Set <PatternsRequestCondition > urlPatterns ;
40
+ @ Value ("${spring.application.name:orders}" )
41
+ private String serviceName ;
30
42
31
43
@ Override
32
44
public boolean preHandle (HttpServletRequest httpServletRequest , HttpServletResponse
@@ -41,12 +53,15 @@ public void postHandle(HttpServletRequest httpServletRequest, HttpServletRespons
41
53
long start = (long ) httpServletRequest .getAttribute (startTimeKey );
42
54
long elapsed = System .nanoTime () - start ;
43
55
double seconds = (double ) elapsed / 1000000000.0 ;
44
- requestLatency .labels (
45
- serviceName ,
46
- httpServletRequest .getMethod (),
47
- getMatchingURLPattern (httpServletRequest ),
48
- Integer .toString (httpServletResponse .getStatus ())
49
- ).observe (seconds );
56
+ String matchedUrl = getMatchingURLPattern (httpServletRequest );
57
+ if (!matchedUrl .equals ("" )) {
58
+ requestLatency .labels (
59
+ serviceName ,
60
+ httpServletRequest .getMethod (),
61
+ matchedUrl ,
62
+ Integer .toString (httpServletResponse .getStatus ())
63
+ ).observe (seconds );
64
+ }
50
65
}
51
66
52
67
@ Override
@@ -55,19 +70,31 @@ public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletRe
55
70
}
56
71
57
72
private String getMatchingURLPattern (HttpServletRequest httpServletRequest ) {
58
- String res = httpServletRequest .getServletPath ();
59
-
60
- for (Map .Entry <RequestMappingInfo , HandlerMethod > item : requestMappingHandlerMapping
61
- .getHandlerMethods ().entrySet ()) {
62
- RequestMappingInfo mapping = item .getKey ();
63
- if (mapping .getPatternsCondition ().getMatchingCondition (httpServletRequest ) != null &&
64
- mapping .getMethodsCondition ().getMatchingCondition (httpServletRequest ) !=
65
- null ) {
66
- res = mapping .getPatternsCondition ().getMatchingCondition (httpServletRequest )
67
- .getPatterns ().iterator ().next ();
73
+ String res = "" ;
74
+ for (PatternsRequestCondition pattern : getUrlPatterns ()) {
75
+ if (pattern .getMatchingCondition (httpServletRequest ) != null &&
76
+ !httpServletRequest .getServletPath ().equals ("/error" )) {
77
+ res = pattern .getMatchingCondition (httpServletRequest ).getPatterns ().iterator ()
78
+ .next ();
68
79
break ;
69
80
}
70
81
}
71
82
return res ;
72
83
}
84
+
85
+ private Set <PatternsRequestCondition > getUrlPatterns () {
86
+ if (this .urlPatterns == null ) {
87
+ this .urlPatterns = new HashSet <>();
88
+ requestMappingHandlerMapping .getHandlerMethods ().forEach ((mapping , handlerMethod ) ->
89
+ urlPatterns .add (mapping .getPatternsCondition ()));
90
+ RepositoryRestHandlerMapping repositoryRestHandlerMapping = new
91
+ RepositoryRestHandlerMapping (mappings , repositoryConfiguration );
92
+ repositoryRestHandlerMapping .setJpaHelper (jpaHelper );
93
+ repositoryRestHandlerMapping .setApplicationContext (applicationContext );
94
+ repositoryRestHandlerMapping .afterPropertiesSet ();
95
+ repositoryRestHandlerMapping .getHandlerMethods ().forEach ((mapping , handlerMethod ) ->
96
+ urlPatterns .add (mapping .getPatternsCondition ()));
97
+ }
98
+ return this .urlPatterns ;
99
+ }
73
100
}
0 commit comments