Skip to content

Commit

Permalink
Start of sentry integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Oct 22, 2023
1 parent aa2374e commit 71c0803
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 2 deletions.
6 changes: 5 additions & 1 deletion eternalcore-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ eternalShadow {

// metrics
library("org.bstats:bstats-bukkit:3.0.2")
libraryRelocate("org.bstats")
library("io.sentry:sentry:6.32.0")
libraryRelocate(
"org.bstats",
/* "io.sentry"*/
)

// pixel-width
library("solar.squares:pixel-width-core:1.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eternalcode.annotations.scan.feature.FeatureDocs;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import io.sentry.Sentry;
import org.apache.commons.io.FileUtils;

import java.io.File;
Expand Down Expand Up @@ -86,6 +87,7 @@ private void copyToBackupFile(File targetFolder, File path) {
Files.copy(targetFolder.toPath(), path.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
catch (IOException exception) {
Sentry.captureException(exception);
exception.printStackTrace();
}
}
Expand Down Expand Up @@ -114,6 +116,7 @@ private void deleteIfOlderDirectory(File backupFolder) {
}
}
catch (DateTimeParseException | IOException exception) {
Sentry.captureException(exception);
exception.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class PluginConfiguration implements ReloadableConfig {
@Description("# Whether the player should receive information about new plugin updates upon joining the server")
public boolean shouldReceivePluginUpdates = true;

@Description({
"# Use sentry? Allows you to send errors to the developers",
"# I strongly recommend leaving this option enabled, it helps us improve the plugin",
})
public boolean useSentry = true;

@Description({ " ", "# Database Section" })
public Database database = new Database();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.zaxxer.hikari.HikariDataSource;
import io.sentry.Sentry;

import java.io.File;
import java.sql.SQLException;
Expand Down Expand Up @@ -95,6 +96,7 @@ public void close() {
this.connectionSource.close();
}
catch (Exception exception) {
Sentry.captureException(exception);
exception.printStackTrace();
}
}
Expand All @@ -112,6 +114,7 @@ public <T, ID> Dao<T, ID> getDao(Class<T> type) {
return (Dao<T, ID>) dao;
}
catch (SQLException exception) {
Sentry.captureException(exception);
throw new RuntimeException(exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.BeanSetup;
import com.eternalcode.core.publish.Subscribe;
import io.sentry.Sentry;

import java.io.File;
import java.sql.SQLException;
Expand All @@ -22,6 +23,7 @@ DatabaseManager databaseManager(PluginConfiguration pluginConfiguration, Logger
databaseManager.connect();
}
catch (SQLException exception) {
Sentry.captureException(exception);
logger.severe("Could not connect to database! Some functions may not work properly!");
throw new RuntimeException(exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.table.DatabaseTable;
import com.j256.ormlite.table.TableUtils;
import io.sentry.Sentry;
import org.jetbrains.annotations.NotNull;
import panda.std.Blank;
import panda.std.reactive.Completable;
Expand Down Expand Up @@ -52,6 +53,7 @@ public Completable<Boolean> isIgnored(UUID by, UUID target) {
return uuids.contains(target) || uuids.contains(IGNORE_ALL);
}
catch (ExecutionException exception) {
Sentry.captureException(exception);
throw new RuntimeException(exception);
}
});
Expand All @@ -71,6 +73,7 @@ public Completable<Blank> ignore(UUID by, UUID target) {
.then(integer -> this.ignores.refresh(by));
}
catch (ExecutionException exception) {
Sentry.captureException(exception);
throw new RuntimeException(exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.bean.BeanException;
import io.sentry.Sentry;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -47,9 +48,11 @@ public Object invokeMethod(Object instance, Method method, Object... additionalD
return method.invoke(instance, parameters);
}
catch (BeanException beanException) {
Sentry.captureException(beanException);
throw new DependencyInjectorException("Failed to invoke method " + method.getName() + " in " + declaringClass.getName() + "!", beanException, declaringClass);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
Sentry.captureException(exception);
throw new DependencyInjectorException(exception, declaringClass);
}
}
Expand Down Expand Up @@ -85,9 +88,11 @@ private <T> T newInstance(Constructor<T> constructor, Object... additionalDepend
return constructor.newInstance(parameters);
}
catch (BeanException beanException) {
Sentry.captureException(beanException);
throw new DependencyInjectorException("Failed to create a new instance of " + declaringClass.getName() + "!", beanException, declaringClass);
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException exception) {
Sentry.captureException(exception);
throw new DependencyInjectorException(exception, declaringClass);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.eternalcode.core.injector.bean;

import io.sentry.Sentry;

import java.lang.reflect.Field;

public class LazyFieldBeanCandidate extends LazyBeanCandidate {
Expand All @@ -15,6 +17,7 @@ public LazyFieldBeanCandidate(Object instance, Field field) {
return field.get(instance);
}
catch (IllegalAccessException exception) {
Sentry.captureException(exception);
throw new BeanException("Cannot access field " + field.getName() + " of " + instance.getClass().getName(), exception, field.getType());
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.metrics;
package com.eternalcode.core.telemetry;

import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.publish.Subscriber;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eternalcode.core.telemetry.sentry;

import io.sentry.Sentry;

public class SentryExceptionHandler implements Thread.UncaughtExceptionHandler {

@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Sentry.captureException(throwable);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.eternalcode.core.telemetry.sentry;

import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.publish.Subscribe;
import com.eternalcode.core.publish.Subscriber;
import com.eternalcode.core.publish.event.EternalInitializeEvent;
import com.eternalcode.core.publish.event.EternalShutdownEvent;
import io.papermc.lib.PaperLib;
import io.sentry.Sentry;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;

@Controller
class SentrySetup implements Subscriber {

public static final String SENTRY_DSN = "https://4bf366d0d9f1da00162b9e629a80be52@o4505014505177088.ingest.sentry.io/4506093905051648";
private final PluginConfiguration pluginConfiguration;

@Inject
SentrySetup(PluginConfiguration pluginConfiguration) {
this.pluginConfiguration = pluginConfiguration;
}

@Subscribe(EternalInitializeEvent.class)
public void onInitialize(JavaPlugin javaPlugin) {
if (this.pluginConfiguration.useSentry) {
Sentry.init(options -> {
options.setDsn(SENTRY_DSN);
options.setTracesSampleRate(1.0);

Server server = javaPlugin.getServer();
options.setTag("serverVersion", server.getVersion());
options.setTag("serverSoftware", PaperLib.getEnvironment().getName());
});

Thread.setDefaultUncaughtExceptionHandler(new SentryExceptionHandler());

javaPlugin.getLogger().info("Sentry initialized");
}
}

@Subscribe(EternalShutdownEvent.class)
public void onShutdown() {
if (this.pluginConfiguration.useSentry) {
Sentry.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ private AdventureUtil() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.base.Preconditions;
import com.google.common.reflect.ClassPath;
import io.sentry.Sentry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -41,6 +43,7 @@ public static List<Class<?>> scanClasses(String packageToScan, ClassLoader class
return loadedClasses;
}
catch (IOException | ClassNotFoundException exception) {
Sentry.captureException(exception);
throw new RuntimeException(exception);
}
}
Expand Down

0 comments on commit 71c0803

Please sign in to comment.