-
-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sentry-servlet-jakarta module #1987
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class io/sentry/servlet/jakarta/SentryServletContainerInitializer : jakarta/servlet/ServletContainerInitializer { | ||
public fun <init> ()V | ||
public fun onStartup (Ljava/util/Set;Ljakarta/servlet/ServletContext;)V | ||
} | ||
|
||
public class io/sentry/servlet/jakarta/SentryServletRequestListener : jakarta/servlet/ServletRequestListener { | ||
public fun <init> ()V | ||
public fun <init> (Lio/sentry/IHub;)V | ||
public fun requestDestroyed (Ljakarta/servlet/ServletRequestEvent;)V | ||
public fun requestInitialized (Ljakarta/servlet/ServletRequestEvent;)V | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension | ||
import net.ltgt.gradle.errorprone.errorprone | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
import org.springframework.boot.gradle.plugin.SpringBootPlugin | ||
|
||
plugins { | ||
`java-library` | ||
kotlin("jvm") | ||
jacoco | ||
id(Config.QualityPlugins.errorProne) | ||
id(Config.QualityPlugins.gradleVersions) | ||
id(Config.BuildPlugins.springBoot) version Config.springBootVersion apply false | ||
id(Config.BuildPlugins.springDependencyManagement) version Config.BuildPlugins.springDependencyManagementVersion | ||
} | ||
|
||
the<DependencyManagementExtension>().apply { | ||
imports { | ||
mavenBom(SpringBootPlugin.BOM_COORDINATES) | ||
} | ||
} | ||
|
||
configure<JavaPluginExtension> { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
tasks.withType<KotlinCompile>().configureEach { | ||
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
kotlinOptions.languageVersion = Config.springKotlinCompatibleLanguageVersion | ||
} | ||
|
||
dependencies { | ||
api(projects.sentry) | ||
compileOnly(Config.Libs.servletApiJakarta) | ||
|
||
compileOnly(Config.CompileOnly.nopen) | ||
errorprone(Config.CompileOnly.nopenChecker) | ||
errorprone(Config.CompileOnly.errorprone) | ||
errorprone(Config.CompileOnly.errorProneNullAway) | ||
compileOnly(Config.CompileOnly.jetbrainsAnnotations) | ||
|
||
// tests | ||
testImplementation(Config.Libs.servletApiJakarta) | ||
testImplementation(projects.sentryTestSupport) | ||
testImplementation(kotlin(Config.kotlinStdLib)) | ||
testImplementation(Config.TestLibs.kotlinTestJunit) | ||
testImplementation(Config.TestLibs.mockitoKotlin) | ||
testImplementation(Config.TestLibs.awaitility) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pulling in spring-boot-starter-test:3.0.0-M2 would have required Java 17 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
|
||
configure<SourceSetContainer> { | ||
test { | ||
java.srcDir("src/test/java") | ||
} | ||
} | ||
|
||
jacoco { | ||
toolVersion = Config.QualityPlugins.Jacoco.version | ||
} | ||
|
||
tasks.jacocoTestReport { | ||
reports { | ||
xml.required.set(true) | ||
html.required.set(false) | ||
} | ||
} | ||
|
||
tasks { | ||
jacocoTestCoverageVerification { | ||
violationRules { | ||
rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } } | ||
} | ||
} | ||
check { | ||
dependsOn(jacocoTestCoverageVerification) | ||
dependsOn(jacocoTestReport) | ||
} | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
val compileKotlin: KotlinCompile by tasks | ||
compileKotlin.kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
languageVersion = Config.springKotlinCompatibleLanguageVersion | ||
} | ||
val compileTestKotlin: KotlinCompile by tasks | ||
compileTestKotlin.kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
languageVersion = Config.springKotlinCompatibleLanguageVersion | ||
} | ||
|
||
tasks.withType<JavaCompile>().configureEach { | ||
options.errorprone { | ||
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR) | ||
option("NullAway:AnnotatedPackages", "io.sentry") | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rta/src/main/java/io/sentry/servlet/jakarta/SentryRequestHttpServletRequestProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.sentry.servlet.jakarta; | ||
|
||
import io.sentry.EventProcessor; | ||
import io.sentry.SentryEvent; | ||
import io.sentry.protocol.Request; | ||
import io.sentry.util.Objects; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** Attaches information about HTTP request to {@link SentryEvent}. */ | ||
final class SentryRequestHttpServletRequestProcessor implements EventProcessor { | ||
private static final List<String> SENSITIVE_HEADERS = | ||
Arrays.asList("X-FORWARDED-FOR", "AUTHORIZATION", "COOKIE"); | ||
|
||
private final @NotNull HttpServletRequest httpRequest; | ||
|
||
public SentryRequestHttpServletRequestProcessor(@NotNull HttpServletRequest httpRequest) { | ||
this.httpRequest = Objects.requireNonNull(httpRequest, "httpRequest is required"); | ||
} | ||
|
||
// httpRequest.getRequestURL() returns StringBuffer which is considered an obsolete class. | ||
@SuppressWarnings("JdkObsolete") | ||
@Override | ||
public @NotNull SentryEvent process( | ||
@NotNull SentryEvent event, @Nullable Map<String, Object> hint) { | ||
final Request sentryRequest = new Request(); | ||
sentryRequest.setMethod(httpRequest.getMethod()); | ||
sentryRequest.setQueryString(httpRequest.getQueryString()); | ||
sentryRequest.setUrl(httpRequest.getRequestURL().toString()); | ||
sentryRequest.setHeaders(resolveHeadersMap(httpRequest)); | ||
|
||
event.setRequest(sentryRequest); | ||
return event; | ||
} | ||
|
||
private @NotNull Map<String, String> resolveHeadersMap( | ||
final @NotNull HttpServletRequest request) { | ||
final Map<String, String> headersMap = new HashMap<>(); | ||
for (String headerName : Collections.list(request.getHeaderNames())) { | ||
// do not copy personal information identifiable headers | ||
if (!SENSITIVE_HEADERS.contains(headerName.toUpperCase(Locale.ROOT))) { | ||
headersMap.put(headerName, toString(request.getHeaders(headerName))); | ||
} | ||
} | ||
return headersMap; | ||
} | ||
|
||
private static @Nullable String toString(final @Nullable Enumeration<String> enumeration) { | ||
return enumeration != null ? String.join(",", Collections.list(enumeration)) : null; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...et-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletContainerInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.sentry.servlet.jakarta; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import jakarta.servlet.ServletContainerInitializer; | ||
import jakarta.servlet.ServletContext; | ||
import jakarta.servlet.ServletException; | ||
import java.util.Set; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Servlet container initializer used to add the {@link SentryServletRequestListener} to the {@link | ||
* ServletContext}. | ||
*/ | ||
@Open | ||
public class SentryServletContainerInitializer implements ServletContainerInitializer { | ||
@Override | ||
public void onStartup(@Nullable Set<Class<?>> c, @NotNull ServletContext ctx) | ||
throws ServletException { | ||
ctx.addListener(SentryServletRequestListener.class); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...servlet-jakarta/src/main/java/io/sentry/servlet/jakarta/SentryServletRequestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.sentry.servlet.jakarta; | ||
|
||
import static io.sentry.TypeCheckHint.SERVLET_REQUEST; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import io.sentry.Breadcrumb; | ||
import io.sentry.HubAdapter; | ||
import io.sentry.IHub; | ||
import io.sentry.util.Objects; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletRequestEvent; | ||
import jakarta.servlet.ServletRequestListener; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* This request listener pushes a new scope into sentry that enriches a Sentry event with the | ||
* details about the current request upon sending. | ||
*/ | ||
@Open | ||
public class SentryServletRequestListener implements ServletRequestListener { | ||
|
||
private final IHub hub; | ||
|
||
public SentryServletRequestListener(@NotNull IHub hub) { | ||
this.hub = Objects.requireNonNull(hub, "hub is required"); | ||
} | ||
|
||
public SentryServletRequestListener() { | ||
this(HubAdapter.getInstance()); | ||
} | ||
|
||
@Override | ||
public void requestDestroyed(@NotNull ServletRequestEvent servletRequestEvent) { | ||
hub.popScope(); | ||
} | ||
|
||
@Override | ||
public void requestInitialized(@NotNull ServletRequestEvent servletRequestEvent) { | ||
hub.pushScope(); | ||
|
||
final ServletRequest servletRequest = servletRequestEvent.getServletRequest(); | ||
if (servletRequest instanceof HttpServletRequest) { | ||
final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; | ||
|
||
final Map<String, Object> hintMap = new HashMap<>(); | ||
hintMap.put(SERVLET_REQUEST, httpRequest); | ||
|
||
hub.addBreadcrumb( | ||
Breadcrumb.http(httpRequest.getRequestURI(), httpRequest.getMethod()), hintMap); | ||
|
||
hub.configureScope( | ||
scope -> { | ||
scope.addEventProcessor(new SentryRequestHttpServletRequestProcessor(httpRequest)); | ||
}); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...-jakarta/src/main/resources/META-INF/services/jakarta.servlet.ServletContainerInitializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.sentry.servlet.jakarta.SentryServletContainerInitializer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing it on this PR will fail the publish release since it does not exist under https://github.com/getsentry/sentry-release-registry yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a PR for adding it there: getsentry/sentry-release-registry#69