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.ext.opensearch.client;
20  
21  import org.opensearch.client.opensearch.OpenSearchClient;
22  import org.opensearch.client.opensearch.cluster.HealthResponse;
23  import org.springframework.boot.actuate.health.Health;
24  import org.springframework.boot.actuate.health.HealthIndicator;
25  
26  public class SyncopeOpenSearchHealthContributor implements HealthIndicator {
27  
28      protected final OpenSearchClient client;
29  
30      public SyncopeOpenSearchHealthContributor(final OpenSearchClient client) {
31          this.client = client;
32      }
33  
34      @Override
35      public Health health() {
36          Health.Builder builder = new Health.Builder();
37  
38          try {
39              HealthResponse health = client.cluster().health();
40              switch (health.status()) {
41                  case Green:
42                  case Yellow:
43                      builder.up();
44                      break;
45  
46                  case Red:
47                  default:
48                      builder.down();
49              }
50              builder.withDetail("cluster_name", health.clusterName());
51              builder.withDetail("status", health.status().jsonValue());
52              builder.withDetail("timed_out", health.timedOut());
53              builder.withDetail("number_of_nodes", health.numberOfNodes());
54              builder.withDetail("number_of_data_nodes", health.numberOfDataNodes());
55              builder.withDetail("active_primary_shards", health.activePrimaryShards());
56              builder.withDetail("relocating_shards", health.relocatingShards());
57              builder.withDetail("initializing_shards", health.initializingShards());
58              builder.withDetail("unassigned_shards", health.unassignedShards());
59              builder.withDetail("delayed_unassigned_shards", health.delayedUnassignedShards());
60              builder.withDetail("number_of_pending_tasks", health.numberOfPendingTasks());
61              builder.withDetail("number_of_in_flight_fetch", health.numberOfInFlightFetch());
62              builder.withDetail("task_max_waiting_in_queue_millis", health.taskMaxWaitingInQueueMillis());
63              builder.withDetail("active_shards_percent_as_number", health.activeShardsPercentAsNumber());
64          } catch (Exception e) {
65              builder.down(e);
66          }
67  
68          return builder.build();
69      }
70  }