Skip to content

Commit

Permalink
Add health check endpoint (#1016)
Browse files Browse the repository at this point in the history
* add health check endpoint

* rewrite test to pekko http
  • Loading branch information
micossow authored Jun 19, 2024
1 parent 771f7b2 commit 3c92fa4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,11 @@ ElasticMQ exposes `Queues` MBean. It contains three operations:
* `NumberOfMessagesForAllQueues` - returns tabular data that contains information about number of messages per queue
* `getNumberOfMessagesInQueue` - returns information about number of messages in specified queue

# Health check endpoint

When running the server in a container orchestration environment, it may be useful to have a health check endpoint.
ElasticMQ provides a simple health check endpoint that can be used for this purpose. The endpoint is available at `/health`.

# Technology

* Core: [Scala](http://scala-lang.org) and [Pekko](https://pekko.apache.org/).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.elasticmq.rest.sqs
import org.apache.pekko.http.scaladsl.Http
import org.apache.pekko.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes}
import org.apache.pekko.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.matchers.should.Matchers

import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, Future}

class HealthCheckTest extends SqsClientServerCommunication with ScalatestRouteTest with Matchers {

test("health check") {
val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = s"$ServiceEndpoint/health"))
val response = Await.result(responseFuture, 10.seconds)

response.status shouldBe StatusCodes.OK

val entityFuture = response.entity.dataBytes.runFold("")(_ ++ _.utf8String)
val entity = Await.result(entityFuture, 10.seconds)

entity shouldBe "OK"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import org.elasticmq.actor.QueueManagerActor
import org.elasticmq.metrics.QueuesMetrics
import org.elasticmq.rest.sqs.Constants._
import org.elasticmq.rest.sqs.XmlNsVersion.extractXmlNs
import org.elasticmq.rest.sqs.directives.{
AnyParamDirectives,
AWSProtocolDirectives,
ElasticMQDirectives,
UnmatchedActionRoutes
}
import org.elasticmq.rest.sqs.directives.{AWSProtocolDirectives, AnyParamDirectives, ElasticMQDirectives, UnmatchedActionRoutes}
import org.elasticmq.rest.sqs.model.RequestPayload
import org.elasticmq.util.{Logging, NowProvider}

Expand All @@ -29,10 +24,10 @@ import java.util.concurrent.atomic.AtomicReference
import javax.management.ObjectName
import scala.collection.immutable.TreeMap
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import scala.concurrent.{Await, Future}
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import scala.xml._

/** By default: <li> <ul>for `socketAddress`: when started, the server will bind to `localhost:9324`</ul> <ul>for
Expand Down Expand Up @@ -222,7 +217,13 @@ case class TheSQSRestServerBuilder(

val config = new ElasticMQConfig

val routes =
val healthCheckRoute = path("health") {
get {
complete("OK")
}
}

val sqsRoute =
extractXmlNs { (_version: XmlNsVersion) =>
implicit val version: XmlNsVersion = _version
extractProtocol { (_protocol: AWSProtocol) =>
Expand All @@ -242,6 +243,8 @@ case class TheSQSRestServerBuilder(
}
}

val routes = concat(healthCheckRoute, sqsRoute)

val appStartFuture = {
// Scala 3 fix: implicit resolution conflict
implicit val _implicitActorSystem: ActorSystem = implicitActorSystem
Expand Down

0 comments on commit 3c92fa4

Please sign in to comment.