0

It look's like I didn't get spring boot metrics

I have spring boot 2 app and org.springframework.boot:spring-boot-starter-actuator

How to get data from this response programmatically?

https://localhost/actuator/metrics/http.server.requests?tag=status:200

{
"name": "http.server.requests",
"baseUnit": "seconds",
"measurements": [
    {
        "statistic": "COUNT",
        "value": 1.0
    },
    {
        "statistic": "TOTAL_TIME",
        "value": 250.654721314
    },
    {
        "statistic": "MAX",
        "value": 0.0
    }
],
"availableTags": [
    {
        "tag": "exception",
        "values": [
            "ClientAbortException"
        ]
    },
    {
        "tag": "method",
        "values": [
            "GET"
        ]
    },
    {
        "tag": "uri",
        "values": [
            "/users/metric"
        ]
    },
    {
        "tag": "outcome",
        "values": [
            "SUCCESS"
        ]
    }
]

}

I tried

@Autowired
private SimpleMeterRegistry meterRegistry;


    Counter counter = meterRegistry.counter("http.server.requests");
    Iterable<Measurement> measure = counter.measure();

but it doesn't have data I need. It's zero

maxb_pro
  • 53
  • 1
  • 1
  • 5

3 Answers3

1

You can get the COUNT as follow. However, I suggest using Spring Boot Admin which shows all the relevant metrics and other details.

int count = 0;
      StringBuilder result = new StringBuilder();
      URL url = new URL(URLtoMap);  //http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + endPoint + "";
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = rd.readLine()) != null) {
         result.append(line);
      }
      rd.close();

      try {
            JSONObject jsonObject =new JSONObject(result.toString().replace("\"", "")); 
            JSONObject jsonCountObject = new JSONObject(jsonObject.getJSONArray("measurements").get(0).toString());
            count =(int) jsonCountObject.get("value");
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

      return count;
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
1

Im kinda new to actuator, but one way u can do it, without requests, using injected MeterRegistry is :

meterRegistry.get("http.server.requests").timers()
    .forEach(t -> {
        System.out.println("Request count: " + t.count());
        System.out.println("Request type:" + t.getId().getTag("method"));
        System.out.println("Request status:" + t.getId().getTag("status"));
        System.out.println("Request uri:" + t.getId().getTag("uri"));
     });
0

You can use this code that adds all url calls:

meterRegistry.get("http.server.requests").timers().stream().mapToLong(Timer::count).sum();

Ali Sadeghi
  • 312
  • 2
  • 7