Skip to content

Commit 80beb31

Browse files
authored
Add log4j autoinstrumentation for spring-boot-3 (#6072)
1 parent 10213f5 commit 80beb31

52 files changed

Lines changed: 2847 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jobs:
4646
- sample: "sentry-samples-spring-boot-webflux"
4747
agent: "false"
4848
agent-auto-init: "true"
49+
- sample: "sentry-samples-spring-boot-jakarta-log4j2"
50+
agent: "false"
51+
agent-auto-init: "true"
4952
- sample: "sentry-samples-spring-boot-jakarta-opentelemetry-noagent"
5053
agent: "false"
5154
agent-auto-init: "true"

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
```properties
3232
sentry.logs.enabled=true
3333
```
34+
- Sentry can now configure Log4j2 automatically for Spring Boot 3 when `sentry-log4j2` is on the classpath and Log4j2 Core is the active logging backend ([#6072](https://github.com/getsentry/sentry-java/pull/6072))
35+
- Disabled by default for now; enable it and configure levels the same way as described in the Spring Boot 4 entry above (`sentry.logging.enabled=true`)
3436

3537
### Fixes
3638

‎build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ apiValidation {
6666
"sentry-samples-spring-boot-opentelemetry",
6767
"sentry-samples-spring-boot-opentelemetry-noagent",
6868
"sentry-samples-spring-boot-jakarta",
69+
"sentry-samples-spring-boot-jakarta-log4j2",
6970
"sentry-samples-spring-boot-jakarta-opentelemetry",
7071
"sentry-samples-spring-boot-jakarta-opentelemetry-noagent",
7172
"sentry-samples-spring-boot-webflux",
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Sentry Sample Spring Boot 3.0+ with Log4j2
2+
3+
Sample application showing how to use Sentry with [Spring Boot](https://spring.io/projects/spring-boot) from version `3.0` onwards and [Log4j2](https://logging.apache.org/log4j/2.x/).
4+
5+
## How to run?
6+
7+
To see events triggered in this sample application in your Sentry dashboard, go to `src/main/resources/application.properties` and replace the test DSN with your own DSN.
8+
9+
Then, execute a command from the module directory:
10+
11+
```
12+
../../gradlew bootRun
13+
```
14+
15+
Make an HTTP request that will trigger events:
16+
17+
```
18+
curl -XPOST --user user:password http://localhost:8080/person/ -H "Content-Type:application/json" -d '{"firstName":"John","lastName":"Smith"}'
19+
```
20+
21+
## GraphQL
22+
23+
The following queries can be used to test the GraphQL integration.
24+
25+
### Greeting
26+
```
27+
{
28+
greeting(name: "crash")
29+
}
30+
```
31+
32+
### Greeting with variables
33+
34+
```
35+
query GreetingQuery($name: String) {
36+
greeting(name: $name)
37+
}
38+
```
39+
variables:
40+
```
41+
{
42+
"name": "crash"
43+
}
44+
```
45+
46+
### Project
47+
48+
```
49+
query ProjectQuery($slug: ID!) {
50+
project(slug: $slug) {
51+
slug
52+
name
53+
repositoryUrl
54+
status
55+
}
56+
}
57+
```
58+
variables:
59+
```
60+
{
61+
"slug": "statuscrash"
62+
}
63+
```
64+
65+
### Mutation
66+
67+
```
68+
mutation AddProjectMutation($slug: ID!) {
69+
addProject(slug: $slug)
70+
}
71+
```
72+
variables:
73+
```
74+
{
75+
"slug": "nocrash",
76+
"name": "nocrash"
77+
}
78+
```
79+
80+
### Subscription
81+
82+
```
83+
subscription SubscriptionNotifyNewTask($slug: ID!) {
84+
notifyNewTask(projectSlug: $slug) {
85+
id
86+
name
87+
assigneeId
88+
assignee {
89+
id
90+
name
91+
}
92+
}
93+
}
94+
```
95+
variables:
96+
```
97+
{
98+
"slug": "crash"
99+
}
100+
```
101+
102+
### Data loader
103+
104+
```
105+
query TasksAndAssigneesQuery($slug: ID!) {
106+
tasks(projectSlug: $slug) {
107+
id
108+
name
109+
assigneeId
110+
assignee {
111+
id
112+
name
113+
}
114+
}
115+
}
116+
```
117+
variables:
118+
```
119+
{
120+
"slug": "crash"
121+
}
122+
```
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import org.jetbrains.kotlin.config.KotlinCompilerVersion
2+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
3+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4+
5+
plugins {
6+
alias(libs.plugins.springboot3)
7+
alias(libs.plugins.spring.dependency.management)
8+
alias(libs.plugins.kotlin.jvm)
9+
alias(libs.plugins.kotlin.spring)
10+
id("io.sentry.systemtest")
11+
}
12+
13+
group = "io.sentry.sample.spring-boot-jakarta-log4j2"
14+
15+
version = "0.0.1-SNAPSHOT"
16+
17+
java.sourceCompatibility = JavaVersion.VERSION_17
18+
19+
java.targetCompatibility = JavaVersion.VERSION_17
20+
21+
dependencyManagement {
22+
imports {
23+
mavenBom("org.springframework.boot:spring-boot-dependencies:${libs.versions.springboot3.get()}")
24+
}
25+
}
26+
27+
// Apollo 4.x requires coroutines 1.9.0+, override Spring Boot's managed version
28+
extra["kotlin-coroutines.version"] = "1.9.0"
29+
30+
configure<JavaPluginExtension> {
31+
sourceCompatibility = JavaVersion.VERSION_17
32+
targetCompatibility = JavaVersion.VERSION_17
33+
}
34+
35+
tasks.withType<KotlinCompile>().configureEach { compilerOptions.jvmTarget = JvmTarget.JVM_17 }
36+
37+
tasks.withType<KotlinCompile>().configureEach {
38+
kotlin {
39+
compilerOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
40+
compilerOptions.jvmTarget = JvmTarget.JVM_17
41+
}
42+
}
43+
44+
dependencies {
45+
implementation(libs.springboot3.starter)
46+
implementation(libs.springboot3.starter.actuator)
47+
implementation(libs.springboot3.starter.aop)
48+
implementation(libs.springboot3.starter.graphql)
49+
implementation(libs.springboot3.starter.jdbc)
50+
implementation(libs.springboot3.starter.quartz)
51+
implementation(libs.springboot3.starter.security)
52+
implementation(libs.springboot3.starter.web)
53+
implementation(libs.springboot3.starter.webflux)
54+
implementation(libs.springboot3.starter.websocket)
55+
implementation(Config.Libs.aspectj)
56+
implementation(Config.Libs.kotlinReflect)
57+
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
58+
implementation(projects.sentrySpringBootStarterJakarta)
59+
implementation(projects.sentryLog4j2)
60+
implementation("org.springframework.boot:spring-boot-starter-log4j2")
61+
modules {
62+
module("org.springframework.boot:spring-boot-starter-logging") {
63+
replacedBy(
64+
"org.springframework.boot:spring-boot-starter-log4j2",
65+
"Use Log4j2 instead of Logback",
66+
)
67+
}
68+
}
69+
implementation(projects.sentryGraphql22)
70+
implementation(projects.sentryQuartz)
71+
implementation(projects.sentryAsyncProfiler)
72+
implementation(projects.sentryOpenfeature)
73+
74+
// cache tracing
75+
implementation(libs.springboot3.starter.cache)
76+
implementation(libs.caffeine)
77+
78+
// kafka
79+
implementation(libs.spring.kafka3)
80+
implementation(projects.sentryKafka)
81+
82+
// OpenFeature SDK
83+
implementation(libs.openfeature)
84+
85+
// database query tracing
86+
implementation(projects.sentryJdbc)
87+
runtimeOnly(libs.hsqldb)
88+
89+
testImplementation(kotlin(Config.kotlinStdLib))
90+
testImplementation(projects.sentry)
91+
testImplementation(projects.sentrySystemTestSupport)
92+
testImplementation(libs.kotlin.test.junit)
93+
testImplementation(libs.slf4j2.api)
94+
testImplementation(libs.springboot3.starter.test) {
95+
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
96+
}
97+
}
98+
99+
tasks.register<Test>("systemTest").configure {
100+
group = "verification"
101+
description = "Runs the System tests"
102+
103+
val test = project.extensions.getByType<SourceSetContainer>()["test"]
104+
testClassesDirs = test.output.classesDirs
105+
classpath = test.runtimeClasspath
106+
107+
maxParallelForks = 1
108+
109+
// Cap JVM args per test
110+
minHeapSize = "128m"
111+
maxHeapSize = "1g"
112+
113+
filter { includeTestsMatching("io.sentry.systemtest*") }
114+
}
115+
116+
tasks.named("test").configure {
117+
require(this is Test)
118+
119+
filter { excludeTestsMatching("io.sentry.systemtest.*") }
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.sentry.samples.spring.boot.jakarta;
2+
3+
import org.springframework.web.bind.annotation.DeleteMapping;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/cache/")
13+
public class CacheController {
14+
private final TodoService todoService;
15+
16+
public CacheController(TodoService todoService) {
17+
this.todoService = todoService;
18+
}
19+
20+
@GetMapping("{id}")
21+
Todo get(@PathVariable Long id) {
22+
return todoService.get(id);
23+
}
24+
25+
@PostMapping
26+
Todo save(@RequestBody Todo todo) {
27+
return todoService.save(todo);
28+
}
29+
30+
@DeleteMapping("{id}")
31+
void delete(@PathVariable Long id) {
32+
todoService.delete(id);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.sentry.samples.spring.boot.jakarta;
2+
3+
import io.sentry.EventProcessor;
4+
import io.sentry.Hint;
5+
import io.sentry.SentryEvent;
6+
import io.sentry.protocol.SentryRuntime;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.springframework.boot.SpringBootVersion;
9+
import org.springframework.stereotype.Component;
10+
11+
/**
12+
* Custom {@link EventProcessor} implementation lets modifying {@link SentryEvent}s before they are
13+
* sent to Sentry.
14+
*/
15+
@Component
16+
public class CustomEventProcessor implements EventProcessor {
17+
private final String springBootVersion;
18+
19+
public CustomEventProcessor(String springBootVersion) {
20+
this.springBootVersion = springBootVersion;
21+
}
22+
23+
public CustomEventProcessor() {
24+
this(SpringBootVersion.getVersion());
25+
}
26+
27+
@Override
28+
public @NotNull SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) {
29+
final SentryRuntime runtime = new SentryRuntime();
30+
runtime.setVersion(springBootVersion);
31+
runtime.setName("Spring Boot");
32+
event.getContexts().setRuntime(runtime);
33+
return event;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.sentry.samples.spring.boot.jakarta;
2+
3+
import io.sentry.spring.jakarta.checkin.SentryCheckIn;
4+
import io.sentry.spring.jakarta.tracing.SentryTransaction;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* {@link SentryTransaction} added on the class level, creates transaction around each method
11+
* execution of every method of the annotated class.
12+
*/
13+
@Component
14+
@SentryTransaction(operation = "scheduled")
15+
public class CustomJob {
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(CustomJob.class);
18+
19+
@SentryCheckIn("monitor_slug_1")
20+
// @Scheduled(fixedRate = 3 * 60 * 1000L)
21+
void execute() throws InterruptedException {
22+
LOGGER.info("Executing scheduled job");
23+
Thread.sleep(2000L);
24+
}
25+
}

0 commit comments

Comments
 (0)