-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Divide Sentry environment to dev and prod * feat: Advance Sentry captureException and Extract it as SentryGateway component * feat: Add Sentry capture at JWT token exception handler that is not handled from SpringWebExceptionHandler(JwtAuthenticationEntryPoint, JwtAccessDeniedHandler)
- Loading branch information
Showing
9 changed files
with
99 additions
and
25 deletions.
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
61 changes: 61 additions & 0 deletions
61
gateway/src/main/java/com/oing/component/SentryGateway.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,61 @@ | ||
package com.oing.component; | ||
|
||
import io.sentry.Sentry; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SentryGateway { | ||
|
||
public static void captureException(HttpServletRequest request, Throwable exception) { | ||
// Add extra data to future events in this scope. | ||
Sentry.withScope(scope -> { | ||
scope.setExtra("method", request.getMethod()); | ||
|
||
if (!request.getQueryString().isEmpty()) { | ||
scope.setExtra("queryString", request.getQueryString()); | ||
} | ||
if (!request.getRequestURI().isEmpty()) { | ||
scope.setExtra("requestURI", request.getRequestURI()); | ||
} | ||
if (!request.getRequestURL().isEmpty()) { | ||
scope.setExtra("requestURL", request.getRequestURL().toString()); | ||
} | ||
if (!request.getParameterMap().isEmpty()) { | ||
for (String key : request.getParameterMap().keySet()) { | ||
scope.setExtra(key, request.getParameter(key)); | ||
} | ||
} | ||
if (request.getContentLength() > 0) { // if request body is not empty | ||
scope.setExtra("requestBody", getBody(request)); | ||
} | ||
|
||
Sentry.captureException(exception); | ||
}); | ||
|
||
} | ||
|
||
private static String getBody(HttpServletRequest request) { | ||
try ( | ||
InputStream inputStream = request.getInputStream(); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)) | ||
) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
while (bufferedReader.ready()) { | ||
stringBuilder.append(bufferedReader.readLine()); | ||
} | ||
|
||
return stringBuilder.toString(); | ||
|
||
} catch (IOException e) { | ||
return "Error reading the request body"; | ||
} | ||
} | ||
} |
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
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