From 3ad9291263017e0566ab6986a922e4fd7f32db7a Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 4 Aug 2025 07:29:12 +0200 Subject: [PATCH 1/4] Add E2E tests for Logback --- .../sentry-samples-logback/build.gradle.kts | 71 ++++++++++++++++++- .../src/test/kotlin/io/sentry/DummyTest.kt | 12 ++++ .../ConsoleApplicationSystemTest.kt | 62 ++++++++++++++++ test/system-test-runner.py | 1 + 4 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/DummyTest.kt create mode 100644 sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt diff --git a/sentry-samples/sentry-samples-logback/build.gradle.kts b/sentry-samples/sentry-samples-logback/build.gradle.kts index 526a629e3d2..b2767727ac5 100644 --- a/sentry-samples/sentry-samples-logback/build.gradle.kts +++ b/sentry-samples/sentry-samples-logback/build.gradle.kts @@ -1,17 +1,84 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + plugins { java application + kotlin("jvm") alias(libs.plugins.gradle.versions) + id("com.github.johnrengelman.shadow") version "8.1.1" } application { mainClass.set("io.sentry.samples.logback.Main") } +java.sourceCompatibility = JavaVersion.VERSION_17 + +java.targetCompatibility = JavaVersion.VERSION_17 + +repositories { mavenCentral() } + configure { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +tasks.withType().configureEach { + kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString() +} + +tasks.withType().configureEach { + kotlinOptions { + freeCompilerArgs = listOf("-Xjsr305=strict") + jvmTarget = JavaVersion.VERSION_17.toString() + } } dependencies { implementation(projects.sentryLogback) implementation(libs.logback.classic) + + testImplementation(kotlin(Config.kotlinStdLib)) + testImplementation(projects.sentry) + testImplementation(projects.sentrySystemTestSupport) + testImplementation(libs.kotlin.test.junit) + testImplementation(libs.slf4j.api) + testImplementation(libs.slf4j.jdk14) +} + +// Configure the Shadow JAR (executable JAR with all dependencies) +tasks.shadowJar { + manifest { attributes["Main-Class"] = "io.sentry.samples.logback.Main" } + archiveClassifier.set("") // Remove the classifier so it replaces the regular JAR + mergeServiceFiles() +} + +// Make the regular jar task depend on shadowJar +tasks.jar { + enabled = false + dependsOn(tasks.shadowJar) +} + +// Fix the startScripts task dependency +tasks.startScripts { dependsOn(tasks.shadowJar) } + +configure { test { java.srcDir("src/test/java") } } + +tasks.register("systemTest").configure { + group = "verification" + description = "Runs the System tests" + + outputs.upToDateWhen { false } + + maxParallelForks = 1 + + // Cap JVM args per test + minHeapSize = "128m" + maxHeapSize = "1g" + + filter { includeTestsMatching("io.sentry.systemtest*") } +} + +tasks.named("test").configure { + require(this is Test) + + filter { excludeTestsMatching("io.sentry.systemtest.*") } } diff --git a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/DummyTest.kt b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/DummyTest.kt new file mode 100644 index 00000000000..6f762b7e453 --- /dev/null +++ b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/DummyTest.kt @@ -0,0 +1,12 @@ +package io.sentry + +import kotlin.test.Test +import kotlin.test.assertTrue + +class DummyTest { + @Test + fun `the only test`() { + // only needed to have more than 0 tests and not fail the build + assertTrue(true) + } +} diff --git a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt new file mode 100644 index 00000000000..cfaed2437d4 --- /dev/null +++ b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt @@ -0,0 +1,62 @@ +package io.sentry.systemtest.io.sentry.systemtest + +import io.sentry.SentryLevel +import io.sentry.systemtest.util.TestHelper +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import java.util.concurrent.TimeUnit + +class ConsoleApplicationSystemTest { + lateinit var testHelper: TestHelper + + @Before + fun setup() { + testHelper = TestHelper("http://localhost:8000") + testHelper.reset() + } + + @Test + fun `logback application sends expected events when run as JAR`() { + val jarFile = testHelper.findJar("sentry-samples-logback") + val process = + testHelper.launch( + jarFile, + mapOf( + "SENTRY_DSN" to testHelper.dsn, + "SENTRY_TRACES_SAMPLE_RATE" to "1.0", + "SENTRY_ENABLE_PRETTY_SERIALIZATION_OUTPUT" to "false", + "SENTRY_DEBUG" to "true", + ), + ) + + process.waitFor(30, TimeUnit.SECONDS) + Assert.assertEquals(0, process.exitValue()) + + // Verify that we received the expected events + verifyExpectedEvents() + } + + private fun verifyExpectedEvents() { + // Verify we received the RuntimeException + testHelper.ensureErrorReceived { event -> + event.exceptions?.any { ex -> ex.type == "RuntimeException" && ex.value == "Invalid productId=445" } == + true && + event.message?.formatted == "Something went wrong" && + event.level?.name == "ERROR" + } + + testHelper.ensureErrorReceived { event -> + event.breadcrumbs?.firstOrNull { it.message == "Hello Sentry!" && it.level == SentryLevel.DEBUG } != null + } + + testHelper.ensureErrorReceived { event -> + event.breadcrumbs?.firstOrNull { it.message == "User has made a purchase of product: 445" && it.level == SentryLevel.INFO } != null + } + + testHelper.ensureLogsReceived { logs, _ -> + testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") && + testHelper.doesContainLogWithBody(logs, "Something went wrong") + } + } +} diff --git a/test/system-test-runner.py b/test/system-test-runner.py index fda5042c819..7b3210909fb 100644 --- a/test/system-test-runner.py +++ b/test/system-test-runner.py @@ -588,6 +588,7 @@ def get_available_modules(self) -> List[ModuleConfig]: ModuleConfig("sentry-samples-spring-boot-jakarta-opentelemetry", "true", "false", "false"), ModuleConfig("sentry-samples-console", "false", "true", "false"), ModuleConfig("sentry-samples-console-opentelemetry-noagent", "false", "true", "false"), + ModuleConfig("sentry-samples-logback", "false", "true", "false"), ] def _find_module_number(self, module_name: str, agent: str, auto_init: str) -> int: From 5058719bd8d2337d73bc9a0a7eb87231c675c0bc Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Mon, 4 Aug 2025 11:31:39 +0000 Subject: [PATCH 2/4] Format code --- .../systemtest/ConsoleApplicationSystemTest.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt index cfaed2437d4..e608f523b5a 100644 --- a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt +++ b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt @@ -2,10 +2,10 @@ package io.sentry.systemtest.io.sentry.systemtest import io.sentry.SentryLevel import io.sentry.systemtest.util.TestHelper +import java.util.concurrent.TimeUnit import org.junit.Assert import org.junit.Before import org.junit.Test -import java.util.concurrent.TimeUnit class ConsoleApplicationSystemTest { lateinit var testHelper: TestHelper @@ -31,7 +31,7 @@ class ConsoleApplicationSystemTest { ) process.waitFor(30, TimeUnit.SECONDS) - Assert.assertEquals(0, process.exitValue()) + Assert.assertEquals(0, process.exitValue()) // Verify that we received the expected events verifyExpectedEvents() @@ -40,18 +40,23 @@ class ConsoleApplicationSystemTest { private fun verifyExpectedEvents() { // Verify we received the RuntimeException testHelper.ensureErrorReceived { event -> - event.exceptions?.any { ex -> ex.type == "RuntimeException" && ex.value == "Invalid productId=445" } == - true && + event.exceptions?.any { ex -> + ex.type == "RuntimeException" && ex.value == "Invalid productId=445" + } == true && event.message?.formatted == "Something went wrong" && event.level?.name == "ERROR" } testHelper.ensureErrorReceived { event -> - event.breadcrumbs?.firstOrNull { it.message == "Hello Sentry!" && it.level == SentryLevel.DEBUG } != null + event.breadcrumbs?.firstOrNull { + it.message == "Hello Sentry!" && it.level == SentryLevel.DEBUG + } != null } testHelper.ensureErrorReceived { event -> - event.breadcrumbs?.firstOrNull { it.message == "User has made a purchase of product: 445" && it.level == SentryLevel.INFO } != null + event.breadcrumbs?.firstOrNull { + it.message == "User has made a purchase of product: 445" && it.level == SentryLevel.INFO + } != null } testHelper.ensureLogsReceived { logs, _ -> From d6eb984384a3f4232044a5460c522d41c545be5b Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 12 Aug 2025 14:20:41 +0200 Subject: [PATCH 3/4] code review fixes --- sentry-samples/sentry-samples-logback/build.gradle.kts | 4 ---- .../io/sentry/systemtest/ConsoleApplicationSystemTest.kt | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/sentry-samples/sentry-samples-logback/build.gradle.kts b/sentry-samples/sentry-samples-logback/build.gradle.kts index b2767727ac5..618ced977e3 100644 --- a/sentry-samples/sentry-samples-logback/build.gradle.kts +++ b/sentry-samples/sentry-samples-logback/build.gradle.kts @@ -21,10 +21,6 @@ configure { targetCompatibility = JavaVersion.VERSION_17 } -tasks.withType().configureEach { - kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString() -} - tasks.withType().configureEach { kotlinOptions { freeCompilerArgs = listOf("-Xjsr305=strict") diff --git a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt index e608f523b5a..72744c0ec31 100644 --- a/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt +++ b/sentry-samples/sentry-samples-logback/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt @@ -1,4 +1,4 @@ -package io.sentry.systemtest.io.sentry.systemtest +package io.sentry.systemtest import io.sentry.SentryLevel import io.sentry.systemtest.util.TestHelper From 2858e13b5428dae9251ab2def5e2ec7f0f506a63 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 12 Aug 2025 16:27:25 +0200 Subject: [PATCH 4/4] enable system tests for logback on GH --- .github/workflows/system-tests-backend.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/system-tests-backend.yml b/.github/workflows/system-tests-backend.yml index a052e4a03d7..5b00a00d507 100644 --- a/.github/workflows/system-tests-backend.yml +++ b/.github/workflows/system-tests-backend.yml @@ -54,6 +54,9 @@ jobs: - sample: "sentry-samples-console" agent: "false" agent-auto-init: "true" + - sample: "sentry-samples-logback" + agent: "false" + agent-auto-init: "true" steps: - uses: actions/checkout@v4 with: