Skip to content

Commit

Permalink
Merge pull request #124 from PAException/dev
Browse files Browse the repository at this point in the history
Sentry to production
  • Loading branch information
PAException authored Jul 29, 2024
2 parents ba8aa38 + 42f20db commit d224c2f
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.6.15</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter</artifactId>
<version>7.12.1</version>
</dependency>
</dependencies>

<repositories>
Expand Down Expand Up @@ -241,6 +246,13 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.sentry</groupId>
<artifactId>sentry-maven-plugin</artifactId>
<version>0.0.8</version>
<!-- Required to allow auto-install of Sentry SDK and Integrations -->
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
*
* <p>Should be called on every error log instead of {@link Logger#error(String, Throwable)},
* otherwise notification won't be sent.</p>
*
* @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);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit d224c2f

Please sign in to comment.