From 9c966896d47031051daae7ac9a01cffc81efa346 Mon Sep 17 00:00:00 2001 From: Paul Huerkamp <35733278+PAException@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:47:25 +0200 Subject: [PATCH] Added sentry --- docker-compose.yml | 2 ++ pom.xml | 12 +++++++ .../reserved/InformationController.java | 2 ++ .../scheduled/ArticleUpdateService.java | 3 ++ .../scheduled/CafeteriaUpdateService.java | 2 ++ .../service/scheduled/EventUpdateService.java | 2 ++ .../scheduled/SolarSystemUpdateService.java | 2 ++ .../scheduled/SubstituteUpdateService.java | 4 ++- .../engelsburg/api/util/LoggingComponent.java | 32 +++++++++++++++---- src/main/resources/application.yml | 5 +++ 10 files changed, 58 insertions(+), 8 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9b522db..bc37802 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,6 +27,8 @@ services: SSL_KEYSTORE: SSL_KEYSTORE_PASSWORD: SSL_KEYSTORE_KEY_PASSWORD: + SENTRY_ENABLED: + SENTRY_DSN: lets-encrypt-helper.domain: lets-encrypt-helper.contact: GOOGLE_ACCOUNT_CREDENTIALS: diff --git a/pom.xml b/pom.xml index 49e006d..f709b1f 100644 --- a/pom.xml +++ b/pom.xml @@ -152,6 +152,11 @@ springdoc-openapi-data-rest 1.6.15 + + io.sentry + sentry-spring-boot-starter + 7.12.1 + @@ -241,6 +246,13 @@ + + io.sentry + sentry-maven-plugin + 0.0.8 + + true + diff --git a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/InformationController.java b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/InformationController.java index f120c74..ba8b04c 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/InformationController.java +++ b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/InformationController.java @@ -18,10 +18,12 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; + import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; + import static io.github.paexception.engelsburg.api.database.model.TeacherModel.Job.BIOLOGIE; import static io.github.paexception.engelsburg.api.database.model.TeacherModel.Job.CHEMIE; import static io.github.paexception.engelsburg.api.database.model.TeacherModel.Job.DEUTSCH; diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java index e4e31f6..1f58ba4 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java @@ -17,6 +17,7 @@ import io.github.paexception.engelsburg.api.util.LoggingComponent; import io.github.paexception.engelsburg.api.util.Result; import io.github.paexception.engelsburg.api.util.WordPressAPI; +import io.sentry.spring.checkin.SentryCheckIn; import lombok.AllArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,6 +50,7 @@ public class ArticleUpdateService extends JsonFetchingService implements Logging * Call {@link #updateArticles(String, int)} every minute and return all articles published in that passed 1 minute. */ @Scheduled(fixedRate = 60 * 1000, initialDelay = 60 * 1000) + @SentryCheckIn("scheduled.article") public void fetchNewArticles() { if (counter != 0) return; //Locked by past fetch @@ -68,6 +70,7 @@ public void fetchNewArticles() { * Checks for updates of articles every 30 minutes. */ @Scheduled(fixedRate = 30 * 60 * 1000, initialDelay = 30 * 60 * 1000) + @SentryCheckIn("scheduled.article-changend") public void checkIfArticlesChanged() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; LOGGER.debug("[ARTICLE] Starting to check for changes..."); diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java index 2b6d08c..f301b48 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java @@ -10,6 +10,7 @@ import io.github.paexception.engelsburg.api.service.JsonFetchingService; import io.github.paexception.engelsburg.api.util.LoggingComponent; import io.github.paexception.engelsburg.api.util.WordPressAPI; +import io.sentry.spring.checkin.SentryCheckIn; import lombok.AllArgsConstructor; import org.jsoup.Jsoup; import org.slf4j.Logger; @@ -30,6 +31,7 @@ public class CafeteriaUpdateService extends JsonFetchingService implements Loggi * Scheduled function to update cafeteria information. */ @Scheduled(fixedRate = 2 * 60 * 1000) + @SentryCheckIn("scheduled.cafeteria") public void updateCafeteriaInformation() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; try { diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java index b801314..7fe1d36 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java @@ -9,6 +9,7 @@ import io.github.paexception.engelsburg.api.endpoint.dto.EventDTO; import io.github.paexception.engelsburg.api.service.JsonFetchingService; import io.github.paexception.engelsburg.api.util.LoggingComponent; +import io.sentry.spring.checkin.SentryCheckIn; import lombok.AllArgsConstructor; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; @@ -38,6 +39,7 @@ public class EventUpdateService extends JsonFetchingService implements LoggingCo * Scheduled function to update events every hour. */ @Scheduled(fixedRate = 5 * 60 * 1000) + @SentryCheckIn("scheduled.event") public void updateEvents() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; LOGGER.debug("[EVENT] Fetching..."); diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java index b5219c6..17d0ff8 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java @@ -8,6 +8,7 @@ import io.github.paexception.engelsburg.api.controller.shared.SolarSystemController; import io.github.paexception.engelsburg.api.service.HtmlFetchingService; import io.github.paexception.engelsburg.api.util.LoggingComponent; +import io.sentry.spring.checkin.SentryCheckIn; import lombok.AllArgsConstructor; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -33,6 +34,7 @@ public class SolarSystemUpdateService extends HtmlFetchingService implements Log * Scheduled function to update the solar system status. */ @Scheduled(fixedRate = 5 * 60 * 1000) + @SentryCheckIn("scheduled.solar") public void updateSolarSystemInfo() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; LOGGER.debug("[SOLAR] Fetching..."); diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java index c1133ac..def07b3 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java @@ -11,6 +11,7 @@ import io.github.paexception.engelsburg.api.endpoint.dto.request.CreateSubstituteMessageRequestDTO; import io.github.paexception.engelsburg.api.service.HtmlFetchingService; import io.github.paexception.engelsburg.api.util.LoggingComponent; +import io.sentry.spring.checkin.SentryCheckIn; import lombok.AllArgsConstructor; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -42,9 +43,10 @@ public class SubstituteUpdateService extends HtmlFetchingService implements Logg private final InformationController informationController; /** - * Scheduled function to update substitutes every 5 minutes. + * Scheduled function to update substitutes every minute. */ @Scheduled(fixedRate = 60 * 1000) + @SentryCheckIn("scheduled.substitute") public void updateSubstitutes() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; LOGGER.debug("[SUBSTITUTE] Fetching..."); diff --git a/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java b/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java index f14313b..5f7627f 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java +++ b/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java @@ -4,6 +4,9 @@ package io.github.paexception.engelsburg.api.util; +import io.sentry.Sentry; +import io.sentry.SentryEvent; +import io.sentry.protocol.Message; import org.slf4j.Logger; /** @@ -12,20 +15,35 @@ public interface LoggingComponent { /** - * Logs an error which is also send as notification. + * Logs an error message and the stack trace of the throwable. * - *

Should be called on every error log instead of {@link Logger#error(String, Throwable)}, - * otherwise notification won't be sent.

- * - * @param msg to log (simple error explanation) - * @param throwable actual error - * @param logger to log error to + * @param msg the message to log + * @param throwable the throwable to log + * @param logger the logger to log to */ default void logError(String msg, Throwable throwable, Logger logger) { logger.error(msg, throwable); + this.reportToSentry(msg, throwable); } default void logExpectedError(String msg, Exception exception, Logger logger) { logger.error(msg + " because of " + exception.getClass().getCanonicalName() + ": " + exception.getMessage()); + this.reportToSentry(msg, exception); + } + + /** + * Reports an exception to sentry. + * + * @param msg the message to log + * @param throwable the throwable to log + */ + private void reportToSentry(String msg, Throwable throwable) { + Message message = new Message(); + message.setMessage(msg); + + SentryEvent event = new SentryEvent(throwable); + event.setMessage(message); + + Sentry.captureEvent(event); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6e87b1d..14d9c38 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -29,3 +29,8 @@ springdoc: swagger-ui: path: /api-docs-ui tagsSorter: alpha +sentry: + dsn: ${SENTRY_DSN} + enable-tracing: true + enable-metrics: true + enabled: ${SENTRY_ENABLED}