Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #6 from dcramer/master

API Conformation and ability to get SENTRY_DSN from env variable.
  • Loading branch information...
commit 4a717e2833fc68e3cae5a14731d83cbe0809eb0a 2 parents 62929ad + fb1d88a
Ken Cochrane kencochrane authored
39 pom.xml
View
@@ -33,6 +33,12 @@
<version>1.2.16</version>
</dependency>
<dependency>
+ <groupId>com.googlecode.jmockit</groupId>
+ <artifactId>jmockit</artifactId>
+ <version>0.999.12</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
@@ -78,6 +84,39 @@
</executions>
</plugin>
</plugins>
+
+ <resources>
+ <resource>
+ <directory>${basedir}/src/main/java</directory>
+ <includes>
+ <include>**/*.json</include>
+ <include>**/*.yml</include>
+ </includes>
+ </resource>
+ <resource>
+ <directory>${basedir}/src/main/resources</directory>
+ <includes>
+ <include>**/*.*</include>
+ </includes>
+ </resource>
+ </resources>
+
+ <testResources>
+ <testResource>
+ <directory>${basedir}/src/test/java</directory>
+ <includes>
+ <include>**/*.json</include>
+ <include>**/*.yml</include>
+ <include>**/*.txt</include>
+ </includes>
+ </testResource>
+ <testResource>
+ <directory>${basedir}/src/test/resources</directory>
+ <includes>
+ <include>**/*.*</include>
+ </includes>
+ </testResource>
+ </testResources>
</build>
</project>
68 src/main/java/net/kencochrane/sentry/RavenClient.java
View
@@ -22,6 +22,15 @@
private static final String RAVEN_JAVA_VERSION = "Raven-Java 0.2";
private RavenConfig config;
private String sentryDSN;
+ private String lastID;
+
+ public RavenClient() {
+ this.sentryDSN = System.getenv("SENTRY_DSN");
+ if (this.sentryDSN == null || this.sentryDSN.length() == 0) {
+ throw new RuntimeException("You must provide a DSN to RavenClient");
+ }
+ this.config = new RavenConfig(this.sentryDSN);
+ }
public RavenClient(String sentryDSN) {
this.sentryDSN = sentryDSN;
@@ -49,6 +58,14 @@ public void setSentryDSN(String sentryDSN) {
this.sentryDSN = sentryDSN;
}
+ public void setLastID(String lastID) {
+ this.lastID = lastID;
+ }
+
+ public String getLastID() {
+ return lastID;
+ }
+
/**
* Build up the JSON body for the POST that is sent to sentry
*
@@ -61,7 +78,8 @@ public void setSentryDSN(String sentryDSN) {
*/
private String buildJSON(String message, String timestamp, String loggerClass, int logLevel, String culprit, Throwable exception) {
JSONObject obj = new JSONObject();
- obj.put("event_id", RavenUtils.getRandomUUID()); //Hexadecimal string representing a uuid4 value.
+ String lastID = RavenUtils.getRandomUUID();
+ obj.put("event_id", lastID); //Hexadecimal string representing a uuid4 value.
obj.put("checksum", RavenUtils.calculateChecksum(message));
if (exception == null) {
obj.put("culprit", culprit);
@@ -76,6 +94,7 @@ private String buildJSON(String message, String timestamp, String loggerClass, i
obj.put("level", logLevel);
obj.put("logger", loggerClass);
obj.put("server_name", RavenUtils.getHostname());
+ setLastID(lastID);
return obj.toJSONString();
}
@@ -243,6 +262,9 @@ private void sendMessage(String messageBody, long timestamp) {
/**
* Send the log message to the sentry server.
*
+ * This method is deprecated. You should use captureMessage instead.
+ *
+ * @deprecated
* @param theLogMessage The log message
* @param timestamp unix timestamp
* @param loggerClass The class associated with the log message
@@ -257,4 +279,48 @@ public void logMessage(String theLogMessage, long timestamp, String loggerClass,
sendMessage(message, timestamp);
}
+
+ /**
+ * Send the log message to the sentry server.
+ *
+ * @param message The log message
+ * @param timestamp unix timestamp
+ * @param loggerClass The class associated with the log message
+ * @param logLevel int value for Log level for message (DEBUG, ERROR, INFO, etc.)
+ * @param culprit Who we think caused the problem.
+ */
+ public String captureMessage(String message, long timestamp, String loggerClass, int logLevel, String culprit) {
+ String timestampDate = RavenUtils.getTimestampString(timestamp);
+
+ String body = buildMessage(message, timestampDate, loggerClass, logLevel, culprit, null);
+ sendMessage(body, timestamp);
+ return getLastID();
+ }
+
+ public String captureMessage(String message) {
+ long timestamp = System.currentTimeMillis() / 1000L;
+ return captureMessage(message, timestamp, "root", 50, null);
+ }
+
+ /**
+ * Send the log message to the sentry server.
+ *
+ * @param exception exception that occurred
+ * @param timestamp unix timestamp
+ * @param loggerClass The class associated with the log message
+ * @param logLevel int value for Log level for message (DEBUG, ERROR, INFO, etc.)
+ * @param culprit Who we think caused the problem.
+ */
+ public String captureException(Throwable exception, long timestamp, String loggerClass, int logLevel, String culprit) {
+ String timestampDate = RavenUtils.getTimestampString(timestamp);
+
+ String body = buildMessage(exception.getMessage(), timestampDate, loggerClass, logLevel, culprit, exception);
+ sendMessage(body, timestamp);
+ return getLastID();
+ }
+
+ public String captureException(Throwable exception) {
+ long timestamp = System.currentTimeMillis() / 1000L;
+ return captureException(exception, timestamp, "root", 50, null);
+ }
}
74 src/test/java/net/kencochrane/sentry/SentryClientTest.java
View
@@ -0,0 +1,74 @@
+package net.kencochrane.sentry;
+
+import mockit.Mocked;
+import mockit.Expectations;
+import mockit.Verifications;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class SentryClientTest {
+ public void triggerRuntimeException() {
+ try {
+ triggerNullPointer();
+ } catch (Exception e) {
+ throw new RuntimeException("Error triggering null pointer", e);
+ }
+ }
+
+ public String triggerNullPointer() {
+ String c = null;
+ return c.toLowerCase();
+ }
+
+ @Test(expected=RuntimeException.class)
+ public void testMissingDSN() {
+ RavenClient client = new RavenClient();
+ }
+
+ @Test
+ public void testConfigureFromDSN() {
+ RavenClient client = new RavenClient("http://public:secret@example.com/path/1");
+ assertEquals(client.getSentryDSN(), "http://public:secret@example.com/path/1");
+ }
+
+ @Test
+ public void testConfigureFromEnvironment() {
+ new Expectations()
+ {
+ @Mocked("getenv") System mockedSystem;
+
+ {
+ System.getenv("SENTRY_DSN"); returns("http://public:secret@example.com/path/1");
+ }
+ };
+ RavenClient client = new RavenClient();
+ assertEquals(client.getSentryDSN(), "http://public:secret@example.com/path/1");
+ }
+
+ @Test
+ public void testCaptureExceptionWithOnlyThrowable() {
+ RavenClient client = new RavenClient("http://public:secret@example.com/path/1");
+
+ new Expectations()
+ {
+
+ @Mocked("getRandomUUID") RavenUtils mockedRavenUtils;
+
+ {
+ RavenUtils.getRandomUUID(); returns("1234567890");
+ }
+
+ // TODO: this should be mocked, somehow
+ // RavenClient.sendMessage(); minTimes = 1; maxTimes = 1;
+ };
+
+
+ try {
+ triggerRuntimeException();
+ } catch (RuntimeException e) {
+ String ident = client.captureException(e);
+ assertEquals(ident, "1234567890");
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.