Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDIT-813: ✨ Add opensearch health indicator #7

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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