如何以编程方式获取http.server.requests指标

问题描述 投票:0回答:1

好像我没有获得春季启动指标

我有spring boot 2应用程序和org.springframework.boot:spring-boot-starter-actuator

如何以编程方式从此响应中获取数据?

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"
        ]
    }
]

}

我尝试过

@Autowired
private SimpleMeterRegistry meterRegistry;


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

但是它没有我需要的数据。它是零

spring spring-boot spring-boot-actuator
1个回答
1
投票

您可以将COUNT作为follow.获得。但是,我建议使用Spring Boot Admin,其中显示了所有相关指标和其他详细信息。

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;
© www.soinside.com 2019 - 2024. All rights reserved.