From 8c55b85fc29f993798f434175ecb7f0cfadee992 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 30 Oct 2022 19:56:04 +0000 Subject: [PATCH] Allow using sentry to log errors. --- build.gradle | 5 +++-- config.properties | 3 +++ src/main/java/me/kavin/piped/Main.java | 11 +++++------ src/main/java/me/kavin/piped/consts/Constants.java | 3 +++ .../java/me/kavin/piped/utils/ExceptionHandler.java | 11 ++++++++--- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 1bf8f54..7708164 100644 --- a/build.gradle +++ b/build.gradle @@ -39,8 +39,9 @@ dependencies { implementation 'org.springframework.security:spring-security-crypto:5.7.4' implementation 'commons-logging:commons-logging:1.2' implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0")) - implementation("com.squareup.okhttp3:okhttp") - implementation("com.squareup.okhttp3:okhttp-brotli") + implementation 'com.squareup.okhttp3:okhttp' + implementation 'com.squareup.okhttp3:okhttp-brotli' + implementation 'io.sentry:sentry:6.6.0' } shadowJar { diff --git a/config.properties b/config.properties index 6b2bbc4..adc11a7 100644 --- a/config.properties +++ b/config.properties @@ -34,6 +34,9 @@ DISABLE_SERVER:false DISABLE_LBRY:false # How long should unauthenticated subscriptions last for SUBSCRIPTIONS_EXPIRY:30 +# Sentry DSN +# Use Sentry to log errors and trace performance +#SENTRY_DSN:INSERT_HERE # Hibernate properties hibernate.connection.url:jdbc:postgresql://postgres:5432/piped hibernate.connection.driver_class:org.postgresql.Driver diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 221a28f..f7ad988 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -1,6 +1,7 @@ package me.kavin.piped; import io.activej.inject.Injector; +import io.sentry.Sentry; import jakarta.persistence.criteria.CriteriaBuilder; import me.kavin.piped.consts.Constants; import me.kavin.piped.server.ServerLauncher; @@ -28,17 +29,15 @@ public class Main { YoutubeStreamExtractor.forceFetchAndroidClient(true); YoutubeStreamExtractor.forceFetchIosClient(true); + Sentry.init(Constants.SENTRY_DSN); + Injector.useSpecializer(); new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { - try { - System.out.printf("ThrottlingCache: %o entries%n", YoutubeThrottlingDecrypter.getCacheSize()); - YoutubeThrottlingDecrypter.clearCache(); - } catch (Exception e) { - e.printStackTrace(); - } + System.out.printf("ThrottlingCache: %o entries%n", YoutubeThrottlingDecrypter.getCacheSize()); + YoutubeThrottlingDecrypter.clearCache(); } }, 0, TimeUnit.MINUTES.toMillis(60)); diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index ac39a39..16b99a5 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -68,6 +68,8 @@ public class Constants { public static final int SUBSCRIPTIONS_EXPIRY; + public static final String SENTRY_DSN; + public static final String VERSION; public static final ObjectMapper mapper = JsonMapper.builder() @@ -107,6 +109,7 @@ public class Constants { DISABLE_SERVER = Boolean.parseBoolean(getProperty(prop, "DISABLE_SERVER", "false")); DISABLE_LBRY = Boolean.parseBoolean(getProperty(prop, "DISABLE_LBRY", "false")); SUBSCRIPTIONS_EXPIRY = Integer.parseInt(getProperty(prop, "SUBSCRIPTIONS_EXPIRY", "30")); + SENTRY_DSN = getProperty(prop, "SENTRY_DSN", ""); System.getenv().forEach((key, value) -> { if (key.startsWith("hibernate")) hibernateProperties.put(key, value); diff --git a/src/main/java/me/kavin/piped/utils/ExceptionHandler.java b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java index 31253cd..c4c1c00 100644 --- a/src/main/java/me/kavin/piped/utils/ExceptionHandler.java +++ b/src/main/java/me/kavin/piped/utils/ExceptionHandler.java @@ -1,5 +1,7 @@ package me.kavin.piped.utils; +import io.sentry.Sentry; +import me.kavin.piped.consts.Constants; import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import java.util.concurrent.CompletionException; @@ -17,9 +19,12 @@ public class ExceptionHandler { e = (Exception) e.getCause(); if (!(e instanceof ContentNotAvailableException)) { - if (path != null) - System.err.println("An error occoured in the path: " + path); - e.printStackTrace(); + Sentry.captureException(e); + if (Constants.SENTRY_DSN.isEmpty()) { + if (path != null) + System.err.println("An error occoured in the path: " + path); + e.printStackTrace(); + } } return e;