BAEL 5974 - Guide to Sentry (#13244)
* [BAEL-4849] Article code * [BAEL-4968] Article code * [BAEL-4968] Article code * [BAEL-4968] Article code * [BAEL-4968] Remove extra comments * [BAEL-5258] Article Code * [BAEL-2765] PKCE Support for Secret Clients * [BAEL-5698] Article code * [BAEL-5698] Article code * [BAEL-5900] Initial commit * [BAEL-5974] Article Code * [BAEL-5974] Article Code
This commit is contained in:
parent
962ed719ac
commit
0ce4cfcfda
|
@ -20,6 +20,7 @@
|
|||
<module>stripe</module>
|
||||
<module>twilio</module>
|
||||
<module>twitter4j</module>
|
||||
<module>sentry-servlet</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>saas-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>sentry-servlet</artifactId>
|
||||
<name>sentry-servlet</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<sentry.version>6.11.0</sentry.version>
|
||||
<cargo.version>1.10.4</cargo.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.sentry</groupId>
|
||||
<artifactId>sentry-servlet</artifactId>
|
||||
<version>${sentry.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven3-plugin</artifactId>
|
||||
<version>${cargo.version}</version>
|
||||
<configuration>
|
||||
<container>
|
||||
<containerId>tomcat9x</containerId>
|
||||
<type>embedded</type>
|
||||
</container>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.sentry.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet(urlPatterns = "/fault", loadOnStartup = 1)
|
||||
public class FaultyServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
|
||||
String op = req.getParameter("op");
|
||||
if( "fault".equals(op) ) {
|
||||
resp.sendError(500, "Something bad happened !");
|
||||
}
|
||||
else if ( "exception".equals(op) ) {
|
||||
throw new IllegalArgumentException("Internal error");
|
||||
}
|
||||
else {
|
||||
resp.setStatus(200);
|
||||
resp.setContentType("text/plain");
|
||||
resp.getWriter().println("OK");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.sentry.support;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
import io.sentry.Sentry;
|
||||
import io.sentry.SentryLevel;
|
||||
|
||||
@WebListener
|
||||
public class SentryContextListener implements ServletContextListener {
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
|
||||
// Besides standard supported locations, let's also allow the DSN to be
|
||||
// passed using servlet container managed parameters. This can be useful if your app
|
||||
// is hosted in a shared application server.
|
||||
ServletContext context = sce.getServletContext();
|
||||
String sentryDsn = context.getInitParameter("sentry.dsn");
|
||||
|
||||
if ( sentryDsn != null ) {
|
||||
context.log("[I21] sentry.dsn init parameter found. Configuring Sentry SDK...");
|
||||
Sentry.init(sentryDsn);
|
||||
}
|
||||
else {
|
||||
context.log("[I25] sentry.dsn init parameter not found. Configuring Sentry SDK with defaults");
|
||||
Sentry.init();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
sce.getServletContext().log("[I34] shutting down context");
|
||||
Sentry.captureMessage("[I35] contextDestroyed", SentryLevel.INFO);
|
||||
Sentry.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.sentry.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import io.sentry.Sentry;
|
||||
import io.sentry.SentryLevel;
|
||||
|
||||
@WebFilter(urlPatterns = "/*")
|
||||
public class SentryFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
try {
|
||||
chain.doFilter(request, response);
|
||||
int rc = ((HttpServletResponse) response).getStatus();
|
||||
if (rc/100 == 5) {
|
||||
Sentry.captureMessage("Application error: code=" + rc, SentryLevel.ERROR);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
Sentry.captureException(t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
# Sentry configuration file
|
||||
# put your DSN here
|
||||
dsn=https://xxxxxxxxxxxxxxxx@zzzzzzz.ingest.sentry.io/wwww
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
|
||||
<!--
|
||||
<context-param>
|
||||
<param-name>sentry.dsn</param-name>
|
||||
<param-value>https://093dbb121a584893b17677bd87c5ec1f@o75061.ingest.sentry.io/4504352731627520</param-value>
|
||||
</context-param>
|
||||
-->
|
||||
</web-app>
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.sentry.servlet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FaultyServletLiveTest {
|
||||
|
||||
|
||||
@Test
|
||||
void testGivenFaultyRequestWithNoQueryString_thenSuccess() throws Exception {
|
||||
|
||||
//int port = getServerPort();
|
||||
URL u = new URL("http://localhost:8080/sentry-servlet/fault");
|
||||
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
|
||||
int rc = conn.getResponseCode();
|
||||
assertThat(rc)
|
||||
.isEqualTo(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGivenFaultyRequestWithFaultString_thenFail() throws Exception {
|
||||
|
||||
//int port = getServerPort();
|
||||
URL u = new URL("http://localhost:8080/sentry-servlet/fault?fault=true");
|
||||
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
|
||||
int rc = conn.getResponseCode();
|
||||
assertThat(rc)
|
||||
.isEqualTo(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGivenFaultyRequestWithExceptionString_thenFail() throws Exception {
|
||||
|
||||
//int port = getServerPort();
|
||||
URL u = new URL("http://localhost:8080/sentry-servlet/fault?exception=true");
|
||||
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
|
||||
int rc = conn.getResponseCode();
|
||||
assertThat(rc)
|
||||
.isEqualTo(500);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue