I have a requirement to count the views on each endpoint. The idea is to create one common Request Count Mapping for all endpoints which should return the view count based on a dynamically entred endpoint.
Let's say someone wants to check the view counts on http://localhost:8080/user/101
.
- RequestMappping
path = /admin/count & RequestParam = url (Here /user/101)
- Then create the
dynamic Request based on RequestParam
http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101
Get and Return the Response
of dynamic Request(JSON Object)
and get the value ofCOUNT
I stuck on how to send a
dynamic request
to http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101 and return the response of it and get the count value
@RequestMapping(path="/admin/count",method=RequestMethod.POST)
public JSONObject count(@RequestParam(name="url") final String url)//@PathVariable(name="url") final String url
{
String finalURL = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + url + "";
return sendRequestToURL(finalURL);
}
@RequestMapping(path="/{finalURL}",method=RequestMethod.GET)
public JSONObject sendRequestToURL(@PathVariable("finalURL") String url)
{
//How to return the response Here
}
This is what I get when Directly fire the URL
GET: http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101
{
"name": "http.server.requests",
"description": null,
"baseUnit": "seconds",
"measurements": [
{
"statistic": "COUNT",
"value": 1
},
{
"statistic": "TOTAL_TIME",
"value": 0.3229436
},
{
"statistic": "MAX",
"value": 0.3229436
}
],
"availableTags": [
{
"tag": "exception",
"values": [
"None"
]
},
{
"tag": "method",
"values": [
"GET"
]
},
{
"tag": "outcome",
"values": [
"SUCCESS"
]
},
{
"tag": "status",
"values": [
"200"
]
}
]
}
Environment:
`spring boot 2.1.2.RELEASE`
<java.version>1.8</java.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>