1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.rest;
20
21 import com.fasterxml.jackson.databind.json.JsonMapper;
22 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
23 import java.util.List;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.apache.cxf.jaxrs.client.WebClient;
28 import org.apache.syncope.client.console.SyncopeWebApplication;
29 import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class SRAStatisticsRestClient {
34
35 private static final Logger LOG = LoggerFactory.getLogger(SRAStatisticsRestClient.class);
36
37 private static final List<?> JAX_RS_PROVIDERS =
38 List.of(new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build()));
39
40 protected String getActuatorEndpoint(final List<NetworkService> instances) {
41 return instances.get(0).getAddress() + "actuator/metrics/spring.cloud.gateway.requests";
42 }
43
44 public SRAStatistics get(final List<NetworkService> instances, final List<Pair<String, String>> selected) {
45 try {
46 WebClient client = WebClient.create(
47 getActuatorEndpoint(instances),
48 JAX_RS_PROVIDERS,
49 SyncopeWebApplication.get().getAnonymousUser(),
50 SyncopeWebApplication.get().getAnonymousKey(),
51 null).
52 accept(MediaType.APPLICATION_JSON_TYPE);
53
54 if (!selected.isEmpty()) {
55 client.query("tag", selected.stream().map(s -> s.getKey() + ":" + s.getValue()).toArray());
56 }
57
58 Response response = client.get();
59 if (response.getStatus() == Response.Status.OK.getStatusCode()) {
60 return response.readEntity(SRAStatistics.class);
61 }
62
63 LOG.error("Unexpected response for SRA statistics from {}: {}",
64 getActuatorEndpoint(instances), response.getStatus());
65 } catch (Exception e) {
66 LOG.error("Could not fetch SRA statistics from {}", getActuatorEndpoint(instances), e);
67 }
68
69 return new SRAStatistics();
70 }
71 }