1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.ext.elasticsearch.client;
20
21 import co.elastic.clients.elasticsearch.ElasticsearchClient;
22 import co.elastic.clients.elasticsearch.cluster.HealthResponse;
23 import org.springframework.boot.actuate.health.Health;
24 import org.springframework.boot.actuate.health.HealthIndicator;
25
26 public class SyncopeElasticsearchHealthContributor implements HealthIndicator {
27
28 protected final ElasticsearchClient client;
29
30 public SyncopeElasticsearchHealthContributor(final ElasticsearchClient 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 }