Skip to content

Commit

Permalink
fix(sentry): Don't init sentry unless requested. Fixes #100
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Jun 21, 2018
1 parent 425a275 commit 8c99a1e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 78 deletions.
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ blossom {
spongestart {
eula true
minecraft project.minecraftVersion
spongeForgeVersion "1.12.2-2611-7.1.0-BETA-3002"
spongeVanillaVersion "1.12.2-7.1.0-BETA-33"
spongeForgeVersion "1.12.2-2705-7.1.0-BETA-3206"
spongeVanillaVersion "1.12.2-7.1.0-BETA-59"
}

repositories {
Expand All @@ -47,7 +47,6 @@ configurations.all {
resolutionStrategy {
force "com.google.guava:guava:23.0"
force "org.reflections:reflections:0.9.10" // Fixes some logging issues in 0.9.11
force "net.java.dev.jna:jna:4.5.1"
}
}

Expand Down Expand Up @@ -106,7 +105,6 @@ shadowJar {
relocate "com.google.thirdparty", "valandur.webapi.shadow.com.google.thirdparty"

relocate "com.sun.research", "valandur.webapi.shadow.com.sun.research"
// relocate "com.sun.jna", "valandur.webapi.shadow.com.sun.jna"

relocate "edu.umd.cs.findbugs.annotations", "valandur.webapi.shadow.edu.umd.cs.findbugs.annotations"

Expand Down
57 changes: 11 additions & 46 deletions src/main/java/valandur/webapi/WebAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import io.sentry.Sentry;
import io.sentry.SentryClient;
import io.sentry.context.Context;
import io.swagger.converter.ModelConverters;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
Expand Down Expand Up @@ -190,34 +191,6 @@ public WebAPI() {
System.setProperty("sentry.release", Constants.VERSION.split("-")[0]);
System.setProperty("sentry.maxmessagelength", "2000");
System.setProperty("sentry.stacktrace.app.packages", WebAPI.class.getPackage().getName());

Sentry.init();

// Add our own jar to the system classloader classpath, because some external libraries don't work otherwise.
// (I'm looking at you jna)
registerInSystemClasspath(WebAPI.class);
}
private void registerInSystemClasspath(Class clazz) {
try {
CodeSource src = clazz.getProtectionDomain().getCodeSource();
if (src == null) {
throw new IOException("Could not get code source for " + clazz.getName() + "!");
}

URL jar = src.getLocation();
String str = jar.toString();
if (str.indexOf("!") > 0) {
jar = new URL(jar.toString().substring(0, str.indexOf("!") + 2));
}
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;

Method method = sysclass.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(sysloader, jar);
} catch (IOException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}

@Listener
Expand All @@ -240,7 +213,6 @@ public void onPreInitialization(GamePreInitializationEvent event) {
Files.createDirectories(configPath);
} catch (IOException e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
}
}

Expand Down Expand Up @@ -340,6 +312,10 @@ private void init(Player triggeringPlayer) {
reportErrors = false;
}

if (reportErrors) {
Sentry.init();
}

AuthenticationProvider.init();

blockService.init();
Expand Down Expand Up @@ -537,7 +513,7 @@ public static void runOnMain(Runnable runnable) throws WebApplicationException {
throw (WebApplicationException)e.getCause();

e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
throw new InternalServerErrorException(e.getMessage());
}
}
Expand All @@ -560,24 +536,13 @@ public static <T> T runOnMain(Supplier<T> supplier) throws WebApplicationExcepti
throw (WebApplicationException)e.getCause();

e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
throw new InternalServerErrorException(e.getMessage());
}
}
}

// Sentry logging
public static void sentryNewRequest(HttpServletRequest req) {
Sentry.clearContext();
Context context = Sentry.getContext();
context.addExtra("request_protocol", req.getProtocol());
context.addExtra("request_method", req.getMethod());
context.addExtra("request_uri", req.getRequestURI());
}
public static void sentryExtra(String name, Object value) {
Sentry.getContext().addExtra(name, value);
}

private static void addDefaultContext() {
Context context = Sentry.getContext();
context.addTag("full_release", Constants.VERSION);
Expand All @@ -603,15 +568,15 @@ private static void addDefaultContext() {
context.addExtra("plugins", pluginList);
}
public static void sentryCapture(Exception e) {
if (!reportErrors())
return;
addDefaultContext();
Sentry.capture(e);
}
public static void sentryCapture(Throwable t) {
if (!reportErrors())
return;
addDefaultContext();
Sentry.capture(t);
}
public static void sentryCapture(String msg) {
addDefaultContext();
Sentry.capture(msg);
}
}
2 changes: 1 addition & 1 deletion src/main/java/valandur/webapi/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void stop() {
server = null;
} catch (Exception e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/valandur/webapi/handler/AssetHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AssetHandler(String folderPath) {
this.contentType = guessContentType(folderPath);
} catch (IOException e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
}
} else {
this.folderPath = folderPath;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/valandur/webapi/hook/WebHookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private void notifyHook(WebHook hook, WebHookType eventType, String source, Obje
stringData = hook.isForm() ? "body=" + URLEncoder.encode(stringData, "UTF-8") : stringData;
} catch (Exception e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
}
}
final String finalData = stringData;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/valandur/webapi/server/ServerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void init() {
}
} catch (IOException e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
}

newProperties.putAll(properties);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/valandur/webapi/servlet/CmdServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private ExecuteCommandResponse runCommand(SecurityContext context, ExecuteComman
return new ExecuteCommandResponse(req.command, src.getLines());
} catch (InterruptedException e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
return new ExecuteCommandResponse(req.command, e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void registerServlet(Class<? extends BaseServlet> servlet) {
} catch (NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
if (WebAPI.reportErrors()) WebAPI.sentryCapture(e);
WebAPI.sentryCapture(e);
}

servletClasses.put(basePath, servlet);
Expand Down
44 changes: 22 additions & 22 deletions webapi_main.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: co.aikar:minecraft-timings:1.0.4" level="project" />
<orderEntry type="library" name="Gradle: com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.4" level="project" />
<orderEntry type="library" name="Gradle: io.sentry:sentry:1.6.4" level="project" />
<orderEntry type="module-library" scope="RUNTIME">
<library>
<CLASSES>
Expand All @@ -30,6 +29,7 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="Gradle: io.sentry:sentry:1.6.4" level="project" />
<orderEntry type="library" name="Gradle: com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.4" level="project" />
<orderEntry type="library" name="Gradle: com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.9.4" level="project" />
<orderEntry type="library" name="Gradle: com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.9.4" level="project" />
Expand Down Expand Up @@ -86,31 +86,31 @@
<orderEntry type="library" name="Gradle: org.glassfish.hk2:hk2-api:2.5.0-b42" level="project" />
<orderEntry type="library" name="Gradle: org.jvnet.mimepull:mimepull:1.9.6" level="project" />
<orderEntry type="library" name="Gradle: org.eclipse.jetty:jetty-util:9.4.8.v20171121" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-core:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :UniversalMarket-1.12.2-v1.1:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :RedProtect-7.3.0-b68-Universal:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :HuskyCrates-v1.8.0PRE2-API7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :Nucleus-1.3.3-S7.0-api:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :CmdScheduler-s7.1-v1.1.0:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :MMCTickets-1.4.1-API-7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.spongepowered:spongeapi:7.1.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-yaml:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.inject:guice:4.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.findbugs:jsr305:3.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :WebBooks:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.github.ben-manes.caffeine:caffeine:2.5.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.github.ben-manes.caffeine:guava:2.5.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.spongepowered:plugin-meta:0.4.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-hocon:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :MMCRestrict-1.4.2-API-7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.typesafe:config:1.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.ow2.asm:asm:5.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.flowpowered:flow-noise:1.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.flowpowered:flow-math:1.0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.flowpowered:flow-noise:1.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.ow2.asm:asm:5.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.typesafe:config:1.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :MMCRestrict-1.4.2-API-7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :ActiveTime-s7.1-v1.4.0:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-gson:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.spongepowered:plugin-meta:0.4.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.github.ben-manes.caffeine:guava:2.5.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.github.ben-manes.caffeine:caffeine:2.5.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :WebBooks:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-core:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.inject:guice:4.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.findbugs:jsr305:3.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: ninja.leaping.configurate:configurate-yaml:3.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.spongepowered:spongeapi:7.1.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :MMCTickets-1.4.1-API-7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :CmdScheduler-s7.1-v1.1.0:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :Nucleus-1.3.3-S7.0-api:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :HuskyCrates-v1.8.0PRE2-API7:" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: :RedProtect-7.3.0-b68-Universal:" level="project" />
</component>
</module>

0 comments on commit 8c99a1e

Please sign in to comment.