Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
usommerl committed May 14, 2021
2 parents 73538a2 + 64c7223 commit 4086680
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
17 changes: 4 additions & 13 deletions src/main/scala/app/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,11 @@ object MetricsApi {
* See: https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
*/
private def toExpositionFormat(l: NonEmptyList[CounteeRecord]): String = {
val metricName1 = "countee_first_counter_item_value"
val metricName2 = s"${metricName1}_ts_provided"
val docMetric1 =
s"# HELP $metricName1 Value of the first counter item.\n# TYPE $metricName1 gauge\n"
val docMetric2 =
s"# HELP $metricName2 Value of the first counter item (Uses the provided timestamp from Countee)\n# TYPE $metricName2 gauge\n"

val metrics1 = l.foldLeft(docMetric1) {
case (acc, r) => s"""${acc}${metricName1}{name="${r.name}"}\t${r.firstCounterItemVal}\n"""
}
val metrics2 = l.foldLeft(docMetric2) {
case (acc, r) => s"""${acc}${metricName2}{name="${r.name}"}\t${r.firstCounterItemVal}\t${r.tsSeconds * 1000}\n"""
val metricName = "countee_first_counter_item_value"
val doc = s"# HELP $metricName Value of the first counter item.\n# TYPE $metricName gauge\n"
l.foldLeft(doc) {
case (acc, r) => s"""${acc}${metricName}{name="${r.name}"}\t${r.firstCounterItemVal}\n"""
}
(metrics1 + metrics2)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/app/CounteeClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ object CounteeClient {
private implicit val recordDecoder: Decoder[CounteeRecord] = (c: HCursor) =>
for {
name <- c.get[String]("name")
data = c.downField("counteritems").downArray
firstCounterItemVal <- data.get[Int]("val")
timestamp <- data.get[Long]("ts_created")
} yield (CounteeRecord(name, firstCounterItemVal, timestamp))
firstCounterItemVal <- c.downField("counteritems").downArray.get[Int]("val")
} yield (CounteeRecord(name, firstCounterItemVal))

private implicit val counteeResponseDecoder: Decoder[NonEmptyList[CounteeRecord]] = (c: HCursor) => {
c.downField("response")
Expand All @@ -37,4 +35,4 @@ object CounteeClient {
}
}

case class CounteeRecord(name: String, firstCounterItemVal: Int, tsSeconds: Long)
case class CounteeRecord(name: String, firstCounterItemVal: Int)
8 changes: 2 additions & 6 deletions src/test/scala/app/ApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ import org.http4s.implicits._
class ApiSpec extends ApiSuite {
test("GET /metrics should return obtained records in prometheus exposition format") {
val client =
counteeClient(IO.pure(NonEmptyList.of(CounteeRecord("Annaberg IZ", 0, 1620903532), CounteeRecord("Belgern IZ", 0, 1))))
counteeClient(IO.pure(NonEmptyList.of(CounteeRecord("Annaberg IZ", 0), CounteeRecord("Belgern IZ", 0))))
val response = api(client).run(Request[IO](method = GET, uri = uri"/metrics"))
val expectedBody =
s"""# HELP countee_first_counter_item_value Value of the first counter item.
|# TYPE countee_first_counter_item_value gauge
|countee_first_counter_item_value{name="Annaberg IZ"}\t0
|countee_first_counter_item_value{name="Belgern IZ"}\t0
|# HELP countee_first_counter_item_value_ts_provided Value of the first counter item (Uses the provided timestamp from Countee)
|# TYPE countee_first_counter_item_value_ts_provided gauge
|countee_first_counter_item_value_ts_provided{name="Annaberg IZ"}\t0\t1620903532000
|countee_first_counter_item_value_ts_provided{name="Belgern IZ"}\t0\t1000\n""".stripMargin
|countee_first_counter_item_value{name="Belgern IZ"}\t0\n""".stripMargin

check(response, Ok, expectedBody)
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/app/CounteeClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CounteeClientSpec extends CatsEffectSuite {

test("Decoding of a valid API response should yield a list of records") {
val mockResponse = Ok(excerptOfManuallyRecordedResponse)
val expected = NonEmptyList.of(CounteeRecord("Annaberg IZ", 0, 1620836932), CounteeRecord("Belgern IZ", 0, 1620804532))
val expected = NonEmptyList.of(CounteeRecord("Annaberg IZ", 0), CounteeRecord("Belgern IZ", 0))
client(mockResponse).fetch.map(obtained => assertEquals(obtained, expected))
}

Expand Down

0 comments on commit 4086680

Please sign in to comment.