-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#14 castalia metrics
- Loading branch information
Showing
24 changed files
with
394 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package castalia | ||
|
||
import com.twitter.finagle | ||
import com.twitter.finagle.http._ | ||
import com.twitter.io.Bufs._ | ||
import com.twitter.util.Await | ||
import org.scalatest.DoNotDiscover | ||
|
||
@DoNotDiscover | ||
class MngmtServerTest extends IntegrationTestBase { | ||
|
||
val client = finagle.Http.newService(s"$serverAddress") | ||
val mngmtClient = finagle.Http.newService(s"$mngmtAddress") | ||
val mngmtPath = "castalia/manager/endpoints" | ||
|
||
describe("starting up stubserver with 'castaliaT.json'") { | ||
|
||
it("should be possible to configure a new endpoint via manager endpoint") { | ||
|
||
val url = s"http://$mngmtAddress/$mngmtPath" | ||
val data = """{"endpoint": "my/endpoint/$1", "responses": [{"ids": {"1": "0"},"httpStatusCode": 200}]}""" | ||
|
||
When(s"""I do a HTTP POST to "$url" with new endpoint """) | ||
|
||
val mngmtRequest = RequestBuilder().url(url).setHeader("Content-Type", "application/json").buildPost(utf8Buf(data)) | ||
|
||
Then("the response should be new endpoint") | ||
val response: Response = Await.result(mngmtClient(mngmtRequest)) | ||
|
||
response.status shouldBe Status.Ok | ||
response.contentString shouldBe "my/endpoint/$1" | ||
} | ||
} | ||
|
||
it("should be possible to fetch metrics via manager endpoint") { | ||
|
||
When("Getting endpoint metrics") | ||
val mngmtRequest = Request(Method.Get, s"/$mngmtPath/metrics") | ||
mngmtRequest.host = mngmtAddress | ||
|
||
Then("the response should contain the metrics") | ||
var responseMng: Response = Await.result(mngmtClient(mngmtRequest)) | ||
|
||
responseMng.status shouldBe Status.Ok | ||
responseMng.contentType.get shouldBe "application/json" | ||
responseMng.contentString.contains(""""my/endpoint/$1": { | ||
| "calls": 0 | ||
| }""") | ||
|
||
val request = Request(Method.Get, "/my/endpoint/0") | ||
request.host = serverAddress | ||
|
||
When("Getting endpoint metrics after a GET on the endpoint") | ||
val responseStub: Response = Await.result(client(request)) | ||
responseStub.status shouldBe Status.Ok | ||
|
||
Then("the response should contain updated stats") | ||
responseMng = Await.result(mngmtClient(mngmtRequest)) | ||
|
||
responseMng.status shouldBe Status.Ok | ||
responseMng.contentType.get shouldBe "application/json" | ||
responseMng.contentString.contains(""""my/endpoint/$1": { | ||
| "calls": 1 | ||
| }""") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/scala/castalia/actors/EndpointRequestInterceptor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package castalia.actors | ||
|
||
import akka.actor.ActorRef | ||
import akka.contrib.pattern.ReceivePipeline | ||
import akka.contrib.pattern.ReceivePipeline.Inner | ||
import castalia.matcher.RequestMatch | ||
import castalia.model.Messages._ | ||
import castalia.model.Model.StubConfig | ||
|
||
/** | ||
* Triggers registration of endpoint stats by intercepting RequestMatch messages | ||
* send to the actors extending this trait. | ||
*/ | ||
trait EndpointRequestInterceptor { | ||
|
||
this: ReceivePipeline => | ||
|
||
def stubConfig: StubConfig | ||
def metricsCollector: ActorRef | ||
|
||
metricsCollector ! EndpointMetricsInit(stubConfig.endpoint) | ||
|
||
pipelineOuter { | ||
case rm: RequestMatch => { metricsCollector ! EndpointCalled(stubConfig.endpoint) } | ||
Inner(rm) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/scala/castalia/metrics/MetricsCollectorActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package castalia.metrics | ||
|
||
import akka.actor.{Actor, ActorLogging, Props} | ||
import castalia.model.Messages.{EndpointCalled, EndpointMetricsInit, EndpointMetricsGet} | ||
import castalia.model.Model.EndpointMetrics | ||
import MetricsCollectorActor._ | ||
|
||
object MetricsCollectorActor { | ||
|
||
val metricNumberOfCalls = "calls" | ||
|
||
def props: Props = Props(new MetricsCollectorActor) | ||
} | ||
|
||
/** | ||
* Actor that aggregates various metrics specific to endpoints. | ||
*/ | ||
class MetricsCollectorActor extends Actor with ActorLogging { | ||
|
||
override def receive: Receive = active(new MetricsRegistry()) | ||
|
||
def active(metricsRegistry: MetricsRegistry): Receive = { | ||
|
||
case EndpointMetricsInit(endpoint) => context.become(active(metricsRegistry.reset(endpoint, metricNumberOfCalls))) | ||
|
||
case EndpointCalled(endpoint) => context.become(active(metricsRegistry.increment(endpoint, metricNumberOfCalls))) | ||
|
||
case EndpointMetricsGet => sender ! EndpointMetrics(metricsRegistry.metricsByEndpoint) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package castalia.metrics | ||
|
||
import castalia._ | ||
|
||
class MetricsRegistry(val metricsByEndpoint: Map[Endpoint, Metrics]) { | ||
|
||
def this() = this(Map()) | ||
|
||
def reset(endpoint: Endpoint, metric: String): MetricsRegistry = { | ||
add(endpoint, metric, 0) | ||
} | ||
|
||
def increment(endpoint: Endpoint, metric: String): MetricsRegistry = { | ||
val value = metricsByEndpoint.getOrElse(endpoint, Map()).getOrElse(metric, 0) + 1 | ||
add(endpoint, metric, value) | ||
} | ||
|
||
private def add(endpoint: Endpoint, metric: String, value: Int) = { | ||
val metrics = metricsByEndpoint.getOrElse(endpoint, Map()) ++ Map(metric -> value) | ||
new MetricsRegistry(metricsByEndpoint + (endpoint -> metrics)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package castalia.model | ||
|
||
import castalia.Endpoint | ||
import castalia.model.Model.StubConfig | ||
|
||
object Messages { | ||
case class UpsertEndpoint(stubConfig: StubConfig) | ||
case class Done(endpoint: String) | ||
|
||
case class EndpointCalled(endpoint: Endpoint) | ||
case class EndpointMetricsInit(endpoint: Endpoint) | ||
case object EndpointMetricsGet | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.