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.client.lib.WebClientBuilder;
30 import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public final class SRAStatisticsRestClient {
35
36 private static final Logger LOG = LoggerFactory.getLogger(SRAStatisticsRestClient.class);
37
38 private static final List<?> JAX_RS_PROVIDERS =
39 List.of(new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build()));
40
41 protected String getActuatorEndpoint(final List<NetworkService> instances) {
42 return instances.get(0).getAddress() + "actuator/metrics/spring.cloud.gateway.requests";
43 }
44
45 public SRAStatistics get(final List<NetworkService> instances, final List<Pair<String, String>> selected) {
46 try {
47 WebClient client = WebClientBuilder.build(getActuatorEndpoint(instances),
48 SyncopeWebApplication.get().getAnonymousUser(),
49 SyncopeWebApplication.get().getAnonymousKey(),
50 JAX_RS_PROVIDERS).accept(MediaType.APPLICATION_JSON_TYPE);
51
52 if (!selected.isEmpty()) {
53 client.query("tag", selected.stream().map(s -> s.getKey() + ":" + s.getValue()).toArray());
54 }
55
56 Response response = client.get();
57 if (response.getStatus() == Response.Status.OK.getStatusCode()) {
58 return response.readEntity(SRAStatistics.class);
59 }
60
61 LOG.error("Unexpected response for SRA statistics from {}: {}",
62 getActuatorEndpoint(instances), response.getStatus());
63 } catch (Exception e) {
64 LOG.error("Could not fetch SRA statistics from {}", getActuatorEndpoint(instances), e);
65 }
66
67 return new SRAStatistics();
68 }
69 }