Skip to content

Commit

Permalink
Add Sentry Configuration to application-server (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums authored and iam-flo committed Dec 30, 2024
1 parent 4f1f7c1 commit b143f38
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
12 changes: 7 additions & 5 deletions server/application-server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -197,6 +196,11 @@
<artifactId>keycloak-admin-client</artifactId>
<version>26.0.3</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter-jakarta</artifactId>
<version>7.18.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -283,9 +287,7 @@
<username>root</username>
<password>root</password>
<!-- Hibernate URL for reference schema using the Hibernate 6 dialect -->
<referenceUrl>hibernate:spring:de.tum.in.www1.hephaestus?dialect=org.hibernate.dialect.PostgreSQLDialect
&amp;hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
<referenceUrl>hibernate:spring:de.tum.in.www1.hephaestus?dialect=org.hibernate.dialect.PostgreSQLDialect &amp;hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy &amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
</referenceUrl>
<!-- Enable detailed logging output -->
<verbose>true</verbose>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ github:
slack:
token: ${SLACK_BOT_TOKEN}
signing-secret: ${SLACK_SIGNING_SECRET}

sentry:
dsn: ${SENTRY_DSN}
4 changes: 4 additions & 0 deletions server/application-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
spring:
application:
name: Hephaestus
version: "0.0.1"

datasource:
url: jdbc:postgresql://localhost:5432/hephaestus
Expand Down Expand Up @@ -74,3 +75,6 @@ github:
slack:
token: ""
signing-secret: ""

sentry:
dsn: ""

0 comments on commit b143f38

Please sign in to comment.