-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Populate events with dependencies metadata (#396)
- Loading branch information
Showing
38 changed files
with
1,084 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import org.jetbrains.kotlin.config.KotlinCompilerVersion | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id(Samples.SpringBoot.springBoot) version BuildPluginsVersion.SPRING_BOOT | ||
id(Samples.SpringBoot.springDependencyManagement) version BuildPluginsVersion.SPRING_DEP_MANAGEMENT | ||
kotlin("jvm") | ||
kotlin("plugin.spring") version BuildPluginsVersion.KOTLIN | ||
id("io.sentry.android.gradle") | ||
} | ||
|
||
group = "io.sentry.samples.spring-boot" | ||
version = "0.0.1-SNAPSHOT" | ||
java.sourceCompatibility = JavaVersion.VERSION_1_8 | ||
java.targetCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(Samples.SpringBoot.springBootStarterSecurity) | ||
implementation(Samples.SpringBoot.springBootStarterWeb) | ||
implementation(Samples.SpringBoot.springBootStarterWebflux) | ||
implementation(Samples.SpringBoot.springBootStarterAop) | ||
implementation(Samples.SpringBoot.aspectj) | ||
implementation(Samples.SpringBoot.springBootStarter) | ||
implementation(Samples.SpringBoot.kotlinReflect) | ||
implementation(Samples.SpringBoot.springBootStarterJdbc) | ||
implementation(kotlin(Samples.SpringBoot.kotlinStdLib, KotlinCompilerVersion.VERSION)) | ||
implementation("io.sentry:sentry-spring-boot-starter:6.5.0") | ||
implementation("io.sentry:sentry-logback:6.5.0") | ||
|
||
// database query tracing | ||
implementation("io.sentry:sentry-jdbc:6.5.0") | ||
runtimeOnly(Samples.SpringBoot.hsqldb) | ||
testImplementation(Samples.SpringBoot.springBootStarterTest) { | ||
exclude(group = "org.junit.vintage", module = "junit-vintage-engine") | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs = listOf("-Xjsr305=strict") | ||
jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/CustomEventProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import io.sentry.EventProcessor; | ||
import io.sentry.Hint; | ||
import io.sentry.SentryEvent; | ||
import io.sentry.protocol.SentryRuntime; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.boot.SpringBootVersion; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Custom {@link EventProcessor} implementation lets modifying {@link SentryEvent}s before they are | ||
* sent to Sentry. | ||
*/ | ||
@Component | ||
public class CustomEventProcessor implements EventProcessor { | ||
private final String springBootVersion; | ||
|
||
public CustomEventProcessor(String springBootVersion) { | ||
this.springBootVersion = springBootVersion; | ||
} | ||
|
||
public CustomEventProcessor() { | ||
this(SpringBootVersion.getVersion()); | ||
} | ||
|
||
@Override | ||
public @NotNull SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) { | ||
final SentryRuntime runtime = new SentryRuntime(); | ||
runtime.setVersion(springBootVersion); | ||
runtime.setName("Spring Boot"); | ||
event.getContexts().setRuntime(runtime); | ||
return event; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/CustomJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import io.sentry.spring.tracing.SentryTransaction; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* {@link SentryTransaction} added on the class level, creates transaction around each method | ||
* execution of every method of the annotated class. | ||
*/ | ||
@Component | ||
@SentryTransaction(operation = "scheduled") | ||
public class CustomJob { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomJob.class); | ||
|
||
@Scheduled(fixedRate = 3 * 1000L) | ||
void execute() throws InterruptedException { | ||
LOGGER.info("Executing scheduled job"); | ||
Thread.sleep(2000L); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
public class Person { | ||
private final String firstName; | ||
private final String lastName; | ||
|
||
public Person(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ples/spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/PersonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/person/") | ||
public class PersonController { | ||
private final PersonService personService; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PersonController.class); | ||
|
||
public PersonController(PersonService personService) { | ||
this.personService = personService; | ||
} | ||
|
||
@GetMapping("{id}") | ||
Person person(@PathVariable Long id) { | ||
LOGGER.info("Loading person with id={}", id); | ||
throw new IllegalArgumentException("Something went wrong [id=" + id + "]"); | ||
} | ||
|
||
@PostMapping | ||
Person create(@RequestBody Person person) { | ||
return personService.create(person); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
examples/spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/PersonService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import io.sentry.ISpan; | ||
import io.sentry.Sentry; | ||
import io.sentry.spring.tracing.SentrySpan; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* {@link SentrySpan} can be added either on the class or the method to create spans around method | ||
* executions. | ||
*/ | ||
@Service | ||
@SentrySpan | ||
public class PersonService { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PersonService.class); | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
private int createCount = 0; | ||
|
||
public PersonService(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
Person create(Person person) { | ||
createCount++; | ||
final ISpan span = Sentry.getSpan(); | ||
if (span != null) { | ||
span.setMeasurement("create_count", createCount); | ||
} | ||
|
||
jdbcTemplate.update( | ||
"insert into person (firstName, lastName) values (?, ?)", | ||
person.getFirstName(), | ||
person.getLastName()); | ||
return person; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
|
||
@Configuration | ||
@SuppressWarnings("deprecation") // WebSecurityConfigurerAdapter has been deprecated | ||
public class SecurityConfiguration | ||
extends org.springframework.security.config.annotation.web.configuration | ||
.WebSecurityConfigurerAdapter { | ||
|
||
// this API is meant to be consumed by non-browser clients thus the CSRF protection is not needed. | ||
@Override | ||
@SuppressWarnings("lgtm[java/spring-disabled-csrf-protection]") | ||
protected void configure(final @NotNull HttpSecurity http) throws Exception { | ||
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic(); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public @NotNull UserDetailsService userDetailsService() { | ||
final PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
|
||
final UserDetails user = | ||
User.builder() | ||
.passwordEncoder(encoder::encode) | ||
.username("user") | ||
.password("password") | ||
.roles("USER") | ||
.build(); | ||
|
||
return new InMemoryUserDetailsManager(user); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/SentryDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@SpringBootApplication | ||
@EnableScheduling | ||
public class SentryDemoApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(SentryDemoApplication.class, args); | ||
} | ||
|
||
@Bean | ||
RestTemplate restTemplate(RestTemplateBuilder builder) { | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
WebClient webClient(WebClient.Builder builder) { | ||
return builder.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
examples/spring-boot-sample/src/main/java/io/sentry/samples/spring/boot/Todo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.sentry.samples.spring.boot; | ||
|
||
public class Todo { | ||
private final Long id; | ||
private final String title; | ||
private final boolean completed; | ||
|
||
public Todo(Long id, String title, boolean completed) { | ||
this.id = id; | ||
this.title = title; | ||
this.completed = completed; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public boolean isCompleted() { | ||
return completed; | ||
} | ||
} |
Oops, something went wrong.