Skip to content

Commit

Permalink
Convert plugin code to Java as it is suggested by guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzaxak committed Oct 3, 2023
1 parent cf0759d commit 7d1ee4b
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 67 deletions.
40 changes: 34 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@

<groupId>io.jenkins.plugins</groupId>
<artifactId>sentry</artifactId>
<version>6.30.0</version>
<version>6.30.4</version>
<packaging>hpi</packaging>
<description>Integrates Jenkins with the Sentry error reporting service</description>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Sentry+Plugin</url>


<properties>
<jenkins.version>2.387.3</jenkins.version>
</properties>

<name>Sentry plugin</name>

Expand Down Expand Up @@ -73,6 +76,18 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.387.x</artifactId>
<version>2483.v3b_22f030990a_</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
Expand All @@ -87,7 +102,20 @@
</pluginRepository>
</pluginRepositories>

<properties>
<jenkins.version>2.401.3</jenkins.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
61 changes: 0 additions & 61 deletions src/main/groovy/io/jenkins/plugins/sentry/SentryPlugin.groovy

This file was deleted.

81 changes: 81 additions & 0 deletions src/main/java/io/jenkins/plugins/sentry/SentryPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.jenkins.plugins.sentry;

import hudson.Plugin;
import io.sentry.Sentry;
import io.sentry.jul.SentryHandler;

import java.util.Enumeration;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class SentryPlugin extends Plugin {
private static final Logger LOG = Logger.getLogger(SentryPlugin.class.getName());

public void postInitialize() {
if (System.getenv("SENTRY_DSN") == null) {
LOG.warning("The `SENTRY_DSN` environment variable is not defined, not configuring Sentry");
return;
}

Sentry.init();
LOG.info("Sentry initialized");

if (System.getenv("SENTRY_NO_LOGWATCHER") == null) {
LOG.info("Running Sentry addLogWatcher");
addLogWatcher();
}
}

public void addLogWatcher() {
final SentryHandler sentry = new SentryHandler();
/* Default everything to the warning level, no need for INFO */
sentry.setMinimumEventLevel(Level.WARNING);

new Thread(() -> {
LOG.info("Waiting for 2 minutes for Jenkins to bootstrap before configuring Sentry");
try {
Thread.sleep(120_000); // Converted seconds to milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}

while (true) {
Enumeration<String> enum_ = LogManager.getLogManager().getLoggerNames();
LOG.fine("Iterating over loggers");
while (enum_.hasMoreElements()) {
String loggerName = enum_.nextElement();
/* Avoid excessive warnings */
/* https://issues.jenkins-ci.org/browse/JENKINS-46404 */
if (!loggerName.equals("org.jenkinsci.plugins.durabletask.ProcessLiveness")) {
LOG.fine("Checking logger: " + loggerName);
Logger manager = LogManager.getLogManager().getLogger(loggerName);
if (manager != null) {
boolean found = false;
for (Handler handler : manager.getHandlers()) {
if (handler instanceof SentryHandler) {
found = true;
break;
}
}

if (!found) {
LOG.info("Adding Sentry to " + loggerName);
manager.addHandler((Handler) sentry);
}
}
}
}

// Sleep for five minutes in this thread, to make sure we are always
// adding Sentry to new loggers should they appear
try {
Thread.sleep(300_000); // Converted 5 minutes to milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}

0 comments on commit 7d1ee4b

Please sign in to comment.