Skip to content

Commit a75e37b

Browse files
runningcodeclaude
andcommitted
test(log4j2): Move the log4j2 sample's system test into sentry-log4j2
Adds SentryAppenderIntegrationTest, which configures the Sentry appender from XML in a logger context of its own and checks what reaches MockSentryServer: the warning event with its context tag and flag, the error with its exception and breadcrumbs, and the logs with their attributes. Logs are enabled through external configuration, as the sample's sentry.properties does, because the appender has no attribute for them. The sample has no system tests left, so it comes off the system-test runner and the backend workflow, and drops the Kotlin plugin it only needed for the test. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 334a404 commit a75e37b

8 files changed

Lines changed: 95 additions & 137 deletions

File tree

‎.github/workflows/system-tests-backend.yml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ jobs:
3434
- sample: "sentry-samples-logback"
3535
agent: "false"
3636
agent-auto-init: "true"
37-
- sample: "sentry-samples-log4j2"
38-
agent: "false"
39-
agent-auto-init: "true"
4037
- sample: "sentry-samples-jul"
4138
agent: "false"
4239
agent-auto-init: "true"

‎sentry-log4j2/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
// tests
3131
testImplementation(projects.sentryTestSupport)
3232
testImplementation(kotlin(Config.kotlinStdLib))
33+
testImplementation(libs.google.truth)
3334
testImplementation(libs.kotlin.test.junit)
3435
testImplementation(libs.log4j.api)
3536
testImplementation(libs.log4j.core)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.sentry.log4j2
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.Sentry
5+
import io.sentry.SentryLevel
6+
import io.sentry.test.MockSentryServer
7+
import io.sentry.test.event
8+
import io.sentry.test.hasFlag
9+
import io.sentry.test.logs
10+
import kotlin.test.AfterTest
11+
import kotlin.test.Test
12+
import org.apache.logging.log4j.ThreadContext
13+
import org.apache.logging.log4j.core.LoggerContext
14+
15+
class SentryAppenderIntegrationTest {
16+
private val server = MockSentryServer()
17+
18+
// A context of its own, so the other tests' context keeps its configuration.
19+
private val loggerContext =
20+
LoggerContext(
21+
"sentry-appender-integration-test",
22+
null,
23+
javaClass.getResource("/sentry-appender-integration-test.xml")!!.toURI(),
24+
)
25+
26+
@AfterTest
27+
fun teardown() {
28+
loggerContext.stop()
29+
Sentry.close()
30+
server.close()
31+
ThreadContext.clearAll()
32+
System.clearProperty("integrationTestDsn")
33+
System.clearProperty("sentry.logs.enabled")
34+
}
35+
36+
@Test
37+
fun `appender configured from XML sends events, breadcrumbs and logs`() {
38+
System.setProperty("integrationTestDsn", server.dsn)
39+
// The appender has no attribute for logs, so they're enabled the way an app's sentry.properties
40+
// would: as external configuration.
41+
System.setProperty("sentry.logs.enabled", "true")
42+
loggerContext.start()
43+
val logger = loggerContext.getLogger(SentryAppenderIntegrationTest::class.java.name)
44+
45+
logger.debug("Hello Sentry!")
46+
ThreadContext.put("userId", "user-1")
47+
Sentry.setAttribute("user.type", "admin")
48+
Sentry.setAttribute("feature.version", 2)
49+
Sentry.setAttribute("debug.enabled", true)
50+
Sentry.addFeatureFlag("my-feature-flag", true)
51+
logger.info("User has made a purchase of product: {}", 445)
52+
logger.warn("Important warning")
53+
logger.error("Something went wrong", RuntimeException("Invalid productId=445"))
54+
Sentry.flush(5000)
55+
56+
val warning = server.awaitEnvelope { it.event()?.level == SentryLevel.WARNING }.event()!!
57+
assertThat(warning.message!!.message).isEqualTo("Important warning")
58+
assertThat(warning.tags).containsEntry("userId", "user-1")
59+
assertThat(warning.hasFlag("my-feature-flag", true)).isTrue()
60+
61+
val error = server.awaitEnvelope { it.event()?.level == SentryLevel.ERROR }.event()!!
62+
assertThat(error.message!!.formatted).isEqualTo("Something went wrong")
63+
assertThat(error.exceptions!!.map { it.type to it.value })
64+
.contains("RuntimeException" to "Invalid productId=445")
65+
assertThat(error.breadcrumbs!!.map { it.message to it.level })
66+
.containsAtLeast(
67+
"Hello Sentry!" to SentryLevel.DEBUG,
68+
"User has made a purchase of product: 445" to SentryLevel.INFO,
69+
)
70+
71+
val logs = server.awaitEnvelope { it.logs() != null }.logs()!!
72+
assertThat(logs.items.map { it.body }).contains("User has made a purchase of product: 445")
73+
val failureLog = logs.items.single { it.body == "Something went wrong" }
74+
assertThat(failureLog.attributes!!.mapValues { it.value.value })
75+
.containsAtLeast("user.type", "admin", "feature.version", 2, "debug.enabled", true)
76+
}
77+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Sentry name="Sentry"
5+
dsn="${sys:integrationTestDsn}"
6+
minimumBreadcrumbLevel="DEBUG"
7+
minimumEventLevel="WARN"
8+
minimumLevel="DEBUG"
9+
contextTags="userId"
10+
/>
11+
</Appenders>
12+
<Loggers>
13+
<Root level="debug">
14+
<AppenderRef ref="Sentry"/>
15+
</Root>
16+
</Loggers>
17+
</Configuration>
Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
plugins {
42
java
53
application
6-
alias(libs.plugins.kotlin.jvm)
74
alias(libs.plugins.gradle.versions)
85
alias(libs.plugins.shadow)
9-
id("io.sentry.systemtest")
106
}
117

128
application { mainClass.set("io.sentry.samples.log4j2.Main") }
139

14-
tasks.withType<KotlinCompile>().configureEach {
15-
kotlin {
16-
compilerOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
17-
}
18-
}
19-
2010
dependencies {
2111
implementation(projects.sentryLog4j2)
2212
implementation(libs.log4j.api)
2313
implementation(libs.log4j.core)
24-
25-
testImplementation(kotlin(Config.kotlinStdLib))
26-
testImplementation(projects.sentry)
27-
testImplementation(projects.sentrySystemTestSupport)
28-
testImplementation(libs.kotlin.test.junit)
29-
testImplementation(libs.slf4j.api)
30-
testImplementation(libs.slf4j.jdk14)
3114
}
3215

3316
// Configure the Shadow JAR (executable JAR with all dependencies)
@@ -50,22 +33,3 @@ tasks.jar {
5033

5134
// Fix the startScripts task dependency
5235
tasks.startScripts { dependsOn(tasks.shadowJar) }
53-
54-
tasks.register<Test>("systemTest").configure {
55-
group = "verification"
56-
description = "Runs the System tests"
57-
58-
val test = project.extensions.getByType<SourceSetContainer>()["test"]
59-
testClassesDirs = test.output.classesDirs
60-
classpath = test.runtimeClasspath
61-
62-
maxParallelForks = 1
63-
64-
filter { includeTestsMatching("io.sentry.systemtest*") }
65-
}
66-
67-
tasks.named("test").configure {
68-
require(this is Test)
69-
70-
filter { excludeTestsMatching("io.sentry.systemtest.*") }
71-
}

‎sentry-samples/sentry-samples-log4j2/src/test/kotlin/io/sentry/DummyTest.kt‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎sentry-samples/sentry-samples-log4j2/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt‎

Lines changed: 0 additions & 85 deletions
This file was deleted.

‎test/system-test-runner.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ def get_available_modules(self) -> List[ModuleConfig]:
832832
ModuleConfig("sentry-samples-console-otlp", "false", "true", "false"),
833833
ModuleConfig("sentry-samples-console-opentelemetry-noagent", "false", "true", "false"),
834834
ModuleConfig("sentry-samples-logback", "false", "true", "false"),
835-
ModuleConfig("sentry-samples-log4j2", "false", "true", "false"),
836835
ModuleConfig("sentry-samples-jul", "false", "true", "false"),
837836
]
838837

0 commit comments

Comments
 (0)