View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }