Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
carts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cld
systems
sock-shop
carts
Commits
abd7e71f
Commit
abd7e71f
authored
8 years ago
by
Phil Winder
Browse files
Options
Downloads
Patches
Plain Diff
Match request to prevent metric for each ID.
parent
1ef22f59
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/works/weave/socks/cart/middleware/HTTPMonitoringInterceptor.java
+26
-1
26 additions, 1 deletion
...eave/socks/cart/middleware/HTTPMonitoringInterceptor.java
with
26 additions
and
1 deletion
src/main/java/works/weave/socks/cart/middleware/HTTPMonitoringInterceptor.java
+
26
−
1
View file @
abd7e71f
package
works.weave.socks.cart.middleware
;
import
io.prometheus.client.Histogram
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
import
org.springframework.web.servlet.mvc.method.RequestMappingInfo
;
import
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.Map
;
public
class
HTTPMonitoringInterceptor
implements
HandlerInterceptor
{
static
final
Histogram
requestLatency
=
Histogram
.
build
()
...
...
@@ -20,6 +25,9 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
@Value
(
"${spring.application.name:carts}"
)
private
String
serviceName
;
@Autowired
private
RequestMappingHandlerMapping
requestMappingHandlerMapping
;
@Override
public
boolean
preHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
)
throws
Exception
{
...
...
@@ -36,7 +44,7 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
requestLatency
.
labels
(
serviceName
,
httpServletRequest
.
getMethod
(),
httpServletRequest
.
getServletPath
(
),
getMatchingURLPattern
(
httpServletRequest
),
Integer
.
toString
(
httpServletResponse
.
getStatus
())
).
observe
(
seconds
);
}
...
...
@@ -45,4 +53,21 @@ public class HTTPMonitoringInterceptor implements HandlerInterceptor {
public
void
afterCompletion
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
Exception
e
)
throws
Exception
{
}
private
String
getMatchingURLPattern
(
HttpServletRequest
httpServletRequest
)
{
String
res
=
httpServletRequest
.
getServletPath
();
for
(
Map
.
Entry
<
RequestMappingInfo
,
HandlerMethod
>
item
:
requestMappingHandlerMapping
.
getHandlerMethods
().
entrySet
())
{
RequestMappingInfo
mapping
=
item
.
getKey
();
if
(
mapping
.
getPatternsCondition
().
getMatchingCondition
(
httpServletRequest
)
!=
null
&&
mapping
.
getMethodsCondition
().
getMatchingCondition
(
httpServletRequest
)
!=
null
)
{
res
=
mapping
.
getPatternsCondition
().
getMatchingCondition
(
httpServletRequest
)
.
getPatterns
().
iterator
().
next
();
break
;
}
}
return
res
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment