Skip to content

Commit

Permalink
Merge branch 'main' into update/scalafmt-core-3.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
NomadBlacky authored Jun 20, 2024
2 parents 4aa9f85 + 38d754e commit db501af
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ lazy val `my-project` = (project in file("my-project"))
"io.opentelemetry" % "opentelemetry-sdk" % "1.38.0" % Test,
"io.opentelemetry" % "opentelemetry-exporter-otlp" % "1.38.0" % Test,
"io.opentelemetry.semconv" % "opentelemetry-semconv" % "1.21.0-alpha" % Test,
"dev.nomadblacky" %% "scalatest-otel-reporter" % "0.2.0-alpha" % Test,
"dev.nomadblacky" %% "scalatest-otel-reporter" % "<version>" % Test,
),
// Add the reporter class
Test / testOptions += Tests.Argument(
Expand All @@ -49,7 +49,7 @@ Below is an example of sending trace data to Jaeger in localhost.
```scala
package example

import dev.nomadblacky.scalatest_otel_reporter.OpenTelemetryTestReporter
import dev.nomadblacky.scalatest_otel_reporter.OpenTelemetrySdkTestReporter
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter
Expand All @@ -62,8 +62,8 @@ import io.opentelemetry.semconv.ResourceAttributes
import java.util.concurrent.TimeUnit

// Mix-In OpenTelemetryTestReporter
class JaegerTestReporter extends OpenTelemetryTestReporter {
def otel: OpenTelemetry = {
class JaegerTestReporter extends OpenTelemetrySdkTestReporter {
def initOpenTelemetry: OpenTelemetrySdk = {
// Direct trace data to Jaeger on localhost
val jaegerOtlpExporter =
OtlpGrpcSpanExporter.builder.setEndpoint("http://localhost:4317").setTimeout(30, TimeUnit.SECONDS).build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.nomadblacky.scalatest_otel_reporter.examples

import dev.nomadblacky.scalatest_otel_reporter.OpenTelemetryTestReporter
import io.opentelemetry.api.OpenTelemetry
import dev.nomadblacky.scalatest_otel_reporter.OpenTelemetrySdkTestReporter
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter
import io.opentelemetry.sdk.OpenTelemetrySdk
Expand All @@ -12,8 +11,8 @@ import io.opentelemetry.semconv.ResourceAttributes

import java.util.concurrent.TimeUnit

class JaegerTestReporter extends OpenTelemetryTestReporter {
def otel: OpenTelemetry = {
class JaegerTestReporter extends OpenTelemetrySdkTestReporter {
def initOpenTelemetry: OpenTelemetrySdk = {
// Export traces to Jaeger over OTLP
val jaegerOtlpExporter =
OtlpGrpcSpanExporter.builder.setEndpoint("http://localhost:4317").setTimeout(30, TimeUnit.SECONDS).build
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.nomadblacky.scalatest_otel_reporter

import io.opentelemetry.api.OpenTelemetry

import java.util.logging.Logger

trait OpenTelemetryApiTestReporter extends OpenTelemetryTestReporter[OpenTelemetry] {
private lazy val logger: Logger = Logger.getLogger(classOf[OpenTelemetryApiTestReporter].getName)

protected def shutdownOtel(): Unit = {
// OpenTelemetryApiTestReporter does not shutdown OpenTelemetry because OpenTelemetry API does not have a shutdown method.
// If you are using something like the OpenTelemetry Java Agent, it will automatically shutdown when the process terminates.
// However, in other environments, it will not run shutdown, so if the process terminates immediately after the test ends, telemetry data may be lost.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.nomadblacky.scalatest_otel_reporter
import io.opentelemetry.sdk.OpenTelemetrySdk

import java.util.logging.Logger

trait OpenTelemetrySdkTestReporter extends OpenTelemetryTestReporter[OpenTelemetrySdk] {
private lazy val logger = Logger.getLogger(classOf[OpenTelemetrySdkTestReporter].getName)

override protected def shutdownOtel(): Unit = {
logger.info("Shutting down OpenTelemetry")
otel.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import org.scalatest.events._
import java.util.concurrent.ConcurrentHashMap
import java.util.logging.Logger

trait OpenTelemetryTestReporter extends Reporter {
trait OpenTelemetryTestReporter[A <: OpenTelemetry] extends Reporter {
import OpenTelemetryTestReporter._

def otel: OpenTelemetry
def initOpenTelemetry: A
protected def shutdownOtel(): Unit

private val tracer = otel.getTracerProvider.get("scalatest")
private val logger = Logger.getLogger(classOf[OpenTelemetryTestReporter].getName)
protected lazy val otel: A = initOpenTelemetry

private lazy val tracer = otel.getTracerProvider.get("scalatest")
private lazy val logger = Logger.getLogger(classOf[OpenTelemetryTestReporter.type].getName)

// TODO: Extract spans to a container class
private var testRootSpan: Span = _
Expand All @@ -37,12 +40,14 @@ trait OpenTelemetryTestReporter extends Reporter {
Option(testRootSpan).fold(throw new IllegalStateException("TestRootSpan not found")) { span =>
span.end()
}
shutdownOtel()

case stopped: RunStopped =>
logger.fine(s"RunStopped")
Option(testRootSpan).fold(throw new IllegalStateException("TestRootSpan not found")) { span =>
span.end()
}
shutdownOtel()

case aborted: RunAborted =>
logger.fine(s"RunAborted")
Expand All @@ -52,6 +57,7 @@ trait OpenTelemetryTestReporter extends Reporter {
.recordException(aborted.throwable.orNull)
.end()
}
shutdownOtel()

/*
* Suite events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.nomadblacky.scalatest_otel_reporter

import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
import io.opentelemetry.sdk.OpenTelemetrySdk
Expand All @@ -11,8 +10,8 @@ import io.opentelemetry.semconv.ResourceAttributes

import java.util.concurrent.TimeUnit

class WireMockOTelTestReporter(host: String, port: Int) extends OpenTelemetryTestReporter {
def otel: OpenTelemetry = {
class WireMockOTelTestReporter(host: String, port: Int) extends OpenTelemetrySdkTestReporter {
def initOpenTelemetry: OpenTelemetrySdk = {
// Export traces to the WireMock server over OTLP
val wireMockOtlpExporter =
OtlpHttpSpanExporter.builder
Expand Down

0 comments on commit db501af

Please sign in to comment.