|
| 1 | +package io.sentry.opentelemetry.agent |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.opentelemetry.agent.fixture.HelloServer |
| 5 | +import io.sentry.test.MockSentryServer |
| 6 | +import io.sentry.test.transaction |
| 7 | +import io.sentry.time.Deadline |
| 8 | +import io.sentry.time.JavaMonotonicTicker |
| 9 | +import java.io.File |
| 10 | +import java.net.URL |
| 11 | +import java.nio.file.Files |
| 12 | +import java.util.concurrent.TimeUnit |
| 13 | +import kotlin.test.AfterTest |
| 14 | +import kotlin.test.Test |
| 15 | + |
| 16 | +/** |
| 17 | + * Runs an app with the built agent attached, in a child JVM, so the test's own JVM stays |
| 18 | + * uninstrumented. Instrumenting it would turn the test's HTTP calls and [MockSentryServer] itself |
| 19 | + * into spans. |
| 20 | + */ |
| 21 | +class AgentSmokeTest { |
| 22 | + private val server = MockSentryServer() |
| 23 | + private val workDir = Files.createTempDirectory("agent-smoke-test").toFile() |
| 24 | + private val appLog = File(workDir, "app.log") |
| 25 | + private var app: Process? = null |
| 26 | + |
| 27 | + @AfterTest |
| 28 | + fun teardown() { |
| 29 | + app?.destroyForcibly()?.waitFor() |
| 30 | + server.close() |
| 31 | + if (appLog.exists()) { |
| 32 | + println(appLog.readText()) |
| 33 | + } |
| 34 | + workDir.deleteRecursively() |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `agent sends a transaction for an incoming request`() { |
| 39 | + val port = launchWithAgent(HelloServer::class.java) |
| 40 | + |
| 41 | + URL("http://127.0.0.1:$port/hello").readText() |
| 42 | + |
| 43 | + // The agent's BatchSpanProcessor exports every 5s. |
| 44 | + val envelope = |
| 45 | + server.awaitEnvelope(timeoutMillis = 30_000) { |
| 46 | + it.transaction()?.contexts?.trace?.operation == "http.server" |
| 47 | + } |
| 48 | + assertThat(envelope.header.sdkVersion!!.name).isEqualTo("sentry.java.opentelemetry.agent") |
| 49 | + } |
| 50 | + |
| 51 | + private fun launchWithAgent(mainClass: Class<*>): Int { |
| 52 | + val portFile = File(workDir, "port") |
| 53 | + val process = |
| 54 | + ProcessBuilder( |
| 55 | + File(System.getProperty("java.home"), "bin/java").path, |
| 56 | + "-javaagent:${System.getProperty("sentry.test.agentJar")}", |
| 57 | + "-cp", |
| 58 | + File(mainClass.protectionDomain.codeSource.location.toURI()).path, |
| 59 | + mainClass.name, |
| 60 | + portFile.path, |
| 61 | + ) |
| 62 | + .redirectErrorStream(true) |
| 63 | + .redirectOutput(appLog) |
| 64 | + .apply { |
| 65 | + environment()["SENTRY_DSN"] = server.dsn |
| 66 | + environment()["SENTRY_TRACES_SAMPLE_RATE"] = "1.0" |
| 67 | + environment()["OTEL_TRACES_EXPORTER"] = "none" |
| 68 | + environment()["OTEL_METRICS_EXPORTER"] = "none" |
| 69 | + environment()["OTEL_LOGS_EXPORTER"] = "none" |
| 70 | + } |
| 71 | + .start() |
| 72 | + app = process |
| 73 | + |
| 74 | + val deadline = Deadline.after(JavaMonotonicTicker.getInstance(), 60, TimeUnit.SECONDS) |
| 75 | + while (true) { |
| 76 | + portFile |
| 77 | + .takeIf { it.exists() } |
| 78 | + ?.readText() |
| 79 | + ?.toIntOrNull() |
| 80 | + ?.let { |
| 81 | + return it |
| 82 | + } |
| 83 | + check(process.isAlive) { "App exited with ${process.exitValue()}" } |
| 84 | + check(!deadline.hasPassed()) { "App did not start within 60s" } |
| 85 | + Thread.sleep(100) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments