79456390

Date: 2025-02-21 05:14:44
Score: 1
Natty:
Report link

Found a similar issue while calling Prometheus API from Java. Followed @Sotirios Delimanolis. It is working now. Below is the sample code to get CPU metric from Prometheus.

String mode = "idle";
        String sort = "{mode=\""+mode+"\"}";
        String metric = "node_cpu_seconds_total{sort}";
        String rate = "rate(" + metric + "[5m])";
        String avg = "avg by (instance) (" + rate + ")";
        String calculation = "100 - (" + avg + " * 100)";
        System.out.println("calc : " + calculation);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:9090/api/v1/query_range")
                .queryParam("query", calculation)
                .queryParam("start", 1740048845)
                .queryParam("end", 1740048905)
                .queryParam("step", 15);
        String prometheusUrl = builder.build().toString();
        Map<String, Object> response = restTemplate.getForObject(query, Map.class, sort);
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Sotirios
  • Low reputation (1):
Posted by: Vinayak Pattar