From 0282f81c63694e8f8f6cec4bee9b11f88b9b965e Mon Sep 17 00:00:00 2001 From: Joel Dickson <9032274+joeldickson@users.noreply.github.com> Date: Thu, 12 Sep 2024 12:15:49 +0000 Subject: [PATCH] add http post --- scalatest-listener/build.sbt | 1 + .../TestMetricsReporter.scala | 47 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/scalatest-listener/build.sbt b/scalatest-listener/build.sbt index 22259b2..f03c0de 100644 --- a/scalatest-listener/build.sbt +++ b/scalatest-listener/build.sbt @@ -8,6 +8,7 @@ libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "3.2.15", "com.fasterxml.jackson.core" % "jackson-databind" % "2.13.0", "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.13.0" + "org.scalaj" %% "scalaj-http" % "2.4.2" ) testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-C", "io.agodadev.testmetricsscala.TestMetricsReporter") \ No newline at end of file diff --git a/scalatest-listener/src/main/scala/io/agodadev/testmetricsscala/TestMetricsReporter.scala b/scalatest-listener/src/main/scala/io/agodadev/testmetricsscala/TestMetricsReporter.scala index ce0900d..d7797db 100644 --- a/scalatest-listener/src/main/scala/io/agodadev/testmetricsscala/TestMetricsReporter.scala +++ b/scalatest-listener/src/main/scala/io/agodadev/testmetricsscala/TestMetricsReporter.scala @@ -1,16 +1,19 @@ -package io.agodadev.testmetricsscala +package io.agodadev.testmetrics import org.scalatest.Reporter import org.scalatest.events._ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.{ArrayNode, ObjectNode} +import com.fasterxml.jackson.module.scala.DefaultScalaModule +import scalaj.http.{Http, HttpOptions} import java.time.Instant import java.util.UUID import scala.collection.mutable +import scala.util.{Failure, Success, Try} class TestMetricsReporter extends Reporter { - private val objectMapper = new ObjectMapper() + private val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule) private val testCases = mutable.Map[String, TestCaseInfo]() private var suiteStartTime: Instant = _ private var totalTests: Int = 0 @@ -18,15 +21,17 @@ class TestMetricsReporter extends Reporter { private var failedTests: Int = 0 private var ignoredTests: Int = 0 + private val endpointUrl = sys.env.getOrElse("BUILD_METRICS_ES_ENDPOINT", "http://compilation-metrics/scalatest") + case class TestCaseInfo( - id: String, - name: String, - suiteName: String, - status: String, - startTime: Instant, - endTime: Instant = Instant.now(), - duration: Long = 0 - ) + id: String, + name: String, + suiteName: String, + status: String, + startTime: Instant, + endTime: Instant = Instant.now(), + duration: Long = 0 + ) override def apply(event: Event): Unit = event match { case RunStarting(ordinal, testCount, configMap, formatter, location, payload, threadName, timeStamp) => @@ -52,7 +57,7 @@ class TestMetricsReporter extends Reporter { case RunCompleted(ordinal, duration, summary, formatter, location, payload, threadName, timeStamp) => val jsonReport = generateJsonReport(summary.getOrElse(Summary(0, 0, 0, 0, 0, 0, 0, 0)), duration) - println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonReport)) + sendReportToEndpoint(jsonReport) case _ => // Ignore other events } @@ -105,6 +110,26 @@ class TestMetricsReporter extends Reporter { rootNode } + private def sendReportToEndpoint(jsonReport: ObjectNode): Unit = { + val jsonString = objectMapper.writeValueAsString(jsonReport) + Try { + val response = Http(endpointUrl) + .postData(jsonString) + .header("Content-Type", "application/json") + .header("Charset", "UTF-8") + .option(HttpOptions.readTimeout(10000)) + .asString + if (response.isSuccess) { + println(s"Successfully sent report to $endpointUrl") + } else { + println(s"Failed to send report. Status code: ${response.code}, Body: ${response.body}") + } + } match { + case Success(_) => // Do nothing, already logged + case Failure(exception) => println(s"Exception when sending report: ${exception.getMessage}") + } + } + private def determinePlatform(): String = { if (System.getProperty("java.vendor").contains("Android")) "Android" else if (System.getenv("DOCKER_CONTAINER") != null) "Docker"