-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sentry Configuration to
application-server
(#205)
- Loading branch information
Showing
4 changed files
with
95 additions
and
5 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
81 changes: 81 additions & 0 deletions
81
...pplication-server/src/main/java/de/tum/in/www1/hephaestus/config/SentryConfiguration.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,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; | ||
}; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -51,3 +51,6 @@ github: | |
slack: | ||
token: ${SLACK_BOT_TOKEN} | ||
signing-secret: ${SLACK_SIGNING_SECRET} | ||
|
||
sentry: | ||
dsn: ${SENTRY_DSN} |
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