diff --git a/server/application-server/pom.xml b/server/application-server/pom.xml index a83859628..34fa80314 100644 --- a/server/application-server/pom.xml +++ b/server/application-server/pom.xml @@ -1,7 +1,6 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot @@ -197,6 +196,11 @@ keycloak-admin-client 26.0.3 + + io.sentry + sentry-spring-boot-starter-jakarta + 7.18.1 + @@ -283,9 +287,7 @@ root root - hibernate:spring:de.tum.in.www1.hephaestus?dialect=org.hibernate.dialect.PostgreSQLDialect - &hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy - &hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy + hibernate:spring:de.tum.in.www1.hephaestus?dialect=org.hibernate.dialect.PostgreSQLDialect &hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy &hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy true diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/config/SentryConfiguration.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/config/SentryConfiguration.java new file mode 100644 index 000000000..104a0ec77 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/config/SentryConfiguration.java @@ -0,0 +1,81 @@ +package de.tum.in.www1.hephaestus.config; + +import io.sentry.Sentry; +import jakarta.annotation.PostConstruct; +import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; + +@Configuration +public class SentryConfiguration { + + private static final Logger logger = LoggerFactory.getLogger(SentryConfiguration.class); + + @Autowired + private Environment environment; + + @Value("${spring.application.version}") + private String hephaestusVersion; + + @Value("${sentry.dsn}") + private Optional sentryDsn; + + /** + * Init sentry with the correct environment and version + */ + @PostConstruct + public void init() { + if (environment.matchesProfiles("specs")) { + logger.info("Sentry is disabled in specs profile"); + return; + } + + if (sentryDsn.isEmpty() || sentryDsn.get().isEmpty()) { + logger.info("Sentry is disabled: Provide a DSN to enable Sentry."); + return; + } + + try { + final String dsn = sentryDsn.get() + "?stacktrace.app.packages=de.tum.in.www1.hephaestus"; + + Sentry.init(options -> { + options.setDsn(dsn); + options.setSendDefaultPii(true); + options.setEnvironment(getEnvironment()); + options.setRelease(hephaestusVersion); + options.setTracesSampleRate(getTracesSampleRate()); + }); + + logger.info("Sentry configuration was successful"); + } catch (Exception ex) { + logger.error("Sentry configuration was not successful due to exception!", ex); + } + } + + private String getEnvironment() { + if (environment.matchesProfiles("test")) { + return "test"; + } else if (environment.matchesProfiles("prod")) { + return "prod"; + } else { + return "local"; + } + } + + /** + * Get the traces sample rate based on the environment. + * + * @return 0% for local, 100% for test, 20% for production environments + */ + private double getTracesSampleRate() { + return switch (getEnvironment()) { + case "test" -> 1.0; + case "prod" -> 0.2; + default -> 0.0; + }; + } +} diff --git a/server/application-server/src/main/resources/application-prod.yml b/server/application-server/src/main/resources/application-prod.yml index e68034f97..5383a00f9 100644 --- a/server/application-server/src/main/resources/application-prod.yml +++ b/server/application-server/src/main/resources/application-prod.yml @@ -51,3 +51,6 @@ github: slack: token: ${SLACK_BOT_TOKEN} signing-secret: ${SLACK_SIGNING_SECRET} + +sentry: + dsn: ${SENTRY_DSN} diff --git a/server/application-server/src/main/resources/application.yml b/server/application-server/src/main/resources/application.yml index adf3c303f..7f1313221 100644 --- a/server/application-server/src/main/resources/application.yml +++ b/server/application-server/src/main/resources/application.yml @@ -1,6 +1,7 @@ spring: application: name: Hephaestus + version: "0.0.1" datasource: url: jdbc:postgresql://localhost:5432/hephaestus @@ -74,3 +75,6 @@ github: slack: token: "" signing-secret: "" + +sentry: + dsn: "" \ No newline at end of file