Skip to content

Commit

Permalink
feat: First implimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
NomadBlacky committed Sep 25, 2023
1 parent 65a0c28 commit be5b385
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 16 deletions.
8 changes: 6 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Dependencies._

lazy val scala2_13 = "2.13.12"
lazy val scala3 = "3.3.1"
lazy val supportedScalaVersions = List(scala2_13, scala3)
Expand Down Expand Up @@ -41,7 +43,9 @@ lazy val root = (project in file("."))
lazy val `scalatest-otel-reporter` = (project in file("scalatest-otel-reporter"))
.settings(
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.16" % Provided,
"io.opentelemetry" % "opentelemetry-sdk" % "1.30.0" % Provided,
scalatest % Provided,
otelSdk % Provided,
),
)
),
)
20 changes: 20 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sbt._

object Dependencies {
private val v = new {
val otel = "1.30.0"
}

// scalatest
val scalatest = "org.scalatest" %% "scalatest" % "3.2.16"

// OpenTelemetry
val otelSdk = "io.opentelemetry" % "opentelemetry-sdk" % v.otel
val otelExporterOTLP = "io.opentelemetry" % "opentelemetry-exporter-otlp" % v.otel
val otelAutoConfigure = "io.opentelemetry" % "opentelemetry-sdk-extension-autoconfigure" % v.otel
val otelSemConv = "io.opentelemetry.semconv" % "opentelemetry-semconv" % "1.21.0-alpha"

// Testing utilities
val wiremock = "org.wiremock" % "wiremock" % "3.1.0"
val testcontainers = "org.testcontainers" % "testcontainers" % "1.19.0"
}
5 changes: 0 additions & 5 deletions scalatest-otel-reporter/src/main/scala/Main.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dev.nomadblacky.scalatest_otel_reporter

import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.{GlobalOpenTelemetry, OpenTelemetry}
import io.opentelemetry.context.Context
import org.scalatest.Reporter
import org.scalatest.events._

import java.util.concurrent.ConcurrentHashMap

trait BaseOpenTelemetryTestReporter extends Reporter {
def otel: OpenTelemetry

private val tracer = otel.getTracerProvider.get("scalatest")

private var testRunSpan: Span = _
private val suitesMap = new ConcurrentHashMap[String, Span]()
private val testsMap = new ConcurrentHashMap[String, Span]()

def apply(event: Event): Unit =
event match {
case starting: TestStarting =>
val suiteSpan = Option(suitesMap.get(starting.suiteId))
val parentContext = suiteSpan.fold(Context.current())(Context.current().`with`)
val testSpan = tracer.spanBuilder(starting.testName).setParent(parentContext).startSpan()
testsMap.put(starting.testName, testSpan)
case succeeded: TestSucceeded =>
Option(testsMap.remove(succeeded.testName))
.fold(throw new IllegalStateException(s"Test not found: $succeeded"))(_.end())
case _: TestFailed =>
// TODO
case ignored: TestIgnored =>
// TODO
case pending: TestPending =>
// TODO
case canceled: TestCanceled =>
// TODO
case starting: SuiteStarting =>
val suiteSpan =
tracer.spanBuilder(starting.suiteName).setParent(Context.current().`with`(testRunSpan)).startSpan()
suitesMap.put(starting.suiteId, suiteSpan)
case completed: SuiteCompleted =>
val span = suitesMap.remove(completed.suiteId)
span.end()
case aborted: SuiteAborted =>
// TODO
case starting: RunStarting =>
testRunSpan = tracer.spanBuilder("UNIT_TEST").startSpan()
case completed: RunCompleted =>
testRunSpan.end()
case stopped: RunStopped =>
// TODO
case aborted: RunAborted =>
// TODO
case opened: ScopeOpened =>
// TODO
case closed: ScopeClosed =>
// TODO
case pending: ScopePending =>
// TODO
case _: DiscoveryStarting => ()
case _: DiscoveryCompleted => ()
case _: RecordableEvent => ()
case _: ExceptionalEvent => ()
case _: NotificationEvent => ()
case _: InfoProvided => ()
case _: AlertProvided => ()
case _: NoteProvided => ()
case _: MarkupProvided => ()
}
}

class OpenTelemetryTestReporter extends BaseOpenTelemetryTestReporter {
def otel: OpenTelemetry = GlobalOpenTelemetry.get()
}
9 changes: 0 additions & 9 deletions scalatest-otel-reporter/src/test/scala/FooSpec.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.nomadblacky.scalatest_otel_reporter

import org.scalatest.Args
import org.scalatest.funspec.AnyFunSpecLike
import org.scalatest.funsuite.AnyFunSuiteLike

class SimpleTestSpec extends AnyFunSuiteLike {

class SimpleTests extends AnyFunSpecLike {
describe("Describe") {
it("It") {
Thread.sleep(1000)
assert(1 == 1)
}
}
}

test("SimpleTest") {
val simpleTests = new SimpleTests
simpleTests.run(None, Args(new OpenTelemetryTestReporter))
}
}

0 comments on commit be5b385

Please sign in to comment.