Skip to content

Commit

Permalink
SDIT-813: ✨ Add opensearch health indicator (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
petergphillips authored May 19, 2023
1 parent bc96425 commit 752d314
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.justice.digital.hmpps.prisonersearchindexer.health

import org.opensearch.data.client.orhlc.OpenSearchRestTemplate
import org.springframework.boot.actuate.health.AbstractHealthIndicator
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.Status
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth
import org.springframework.stereotype.Component

@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@Component
class OpenSearchHealthIndicator(private val template: OpenSearchRestTemplate) : AbstractHealthIndicator() {
override fun doHealthCheck(builder: Health.Builder): Unit =
processResponse(builder, template.cluster().health())

private fun processResponse(builder: Health.Builder, response: ClusterHealth) {
if (response.isTimedOut) {
builder.down().build()
} else {
val status = response.status
builder.status(if (status == "RED") Status.OUT_OF_SERVICE else Status.UP)
builder.withDetail("cluster_name", response.clusterName)
builder.withDetail("status", response.status)
builder.withDetail("timed_out", response.isTimedOut)
builder.withDetail("number_of_nodes", response.numberOfNodes)
builder.withDetail("number_of_data_nodes", response.numberOfDataNodes)
builder.withDetail("active_primary_shards", response.activePrimaryShards)
builder.withDetail("active_shards", response.activeShards)
builder.withDetail("relocating_shards", response.relocatingShards)
builder.withDetail("initializing_shards", response.initializingShards)
builder.withDetail("unassigned_shards", response.unassignedShards)
builder.withDetail("delayed_unassigned_shards", response.delayedUnassignedShards)
builder.withDetail("number_of_pending_tasks", response.numberOfPendingTasks)
builder.withDetail("number_of_in_flight_fetch", response.numberOfInFlightFetch)
builder.withDetail("task_max_waiting_in_queue_millis", response.taskMaxWaitingTimeMillis)
builder.withDetail("active_shards_percent_as_number", response.activeShardsPercent)
builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class HealthCheckTest : IntegrationTestBase() {
.jsonPath("status").isEqualTo("UP")
}

@Test
fun `health check reports opensearch status`() {
stubPingWithResponse(200)

webTestClient.get().uri("/health")
.exchange()
.expectStatus().isOk
.expectBody().jsonPath("components.openSearch.status").isEqualTo("UP")
}

private fun stubPingWithResponse(status: Int) {
hmppsAuth.stubHealthPing(status)
restrictedPatientsApi.stubHealthPing(status)
Expand Down

0 comments on commit 752d314

Please sign in to comment.