-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat : 서버에러 슬랙 알림 추가 #235
Merged
Merged
feat : 서버에러 슬랙 알림 추가 #235
Changes from all commits
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
109 changes: 109 additions & 0 deletions
109
DuDoong-Api/src/main/java/band/gosrock/api/config/slack/SlackApiProvider.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,109 @@ | ||
package band.gosrock.api.config.slack; | ||
|
||
import static com.slack.api.model.block.Blocks.divider; | ||
import static com.slack.api.model.block.Blocks.section; | ||
import static com.slack.api.model.block.composition.BlockCompositions.plainText; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.slack.api.methods.MethodsClient; | ||
import com.slack.api.methods.SlackApiException; | ||
import com.slack.api.methods.request.chat.ChatPostMessageRequest; | ||
import com.slack.api.model.block.Blocks; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.MarkdownTextObject; | ||
import com.slack.api.model.block.composition.TextObject; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SlackApiProvider { | ||
private final MethodsClient methodsClient; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Value("${spring.profiles.active}") | ||
private String ACTIVE_PROFILE; | ||
|
||
@Value("${slack.webhook.id}") | ||
private String CHANNEL_ID; | ||
|
||
private final int MAX_LEN = 500; | ||
|
||
@Async | ||
public void sendError(ContentCachingRequestWrapper cachingRequest, Exception e, Long userId) | ||
throws IOException { | ||
if (Arrays.asList("staging", "prod").contains(ACTIVE_PROFILE)) { | ||
executeSendError(cachingRequest, e, userId); | ||
} | ||
} | ||
|
||
private void executeSendError( | ||
ContentCachingRequestWrapper cachingRequest, Exception e, Long userId) | ||
throws IOException { | ||
final String url = cachingRequest.getRequestURL().toString(); | ||
final String method = cachingRequest.getMethod(); | ||
final String body = | ||
objectMapper.readTree(cachingRequest.getContentAsByteArray()).toString(); | ||
final String exceptionAsString = Arrays.toString(e.getStackTrace()); | ||
final int cutLength = Math.min(exceptionAsString.length(), MAX_LEN); | ||
|
||
final String errorMessage = e.getMessage(); | ||
final String errorStack = exceptionAsString.substring(0, cutLength); | ||
final String errorUserIP = cachingRequest.getRemoteAddr(); | ||
|
||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
layoutBlocks.add( | ||
Blocks.header( | ||
headerBlockBuilder -> | ||
headerBlockBuilder.text(plainText("Error Detection")))); | ||
layoutBlocks.add(divider()); | ||
|
||
MarkdownTextObject errorUserIdMarkdown = | ||
MarkdownTextObject.builder().text("* User Id :*\n" + userId).build(); | ||
MarkdownTextObject errorUserIpMarkdown = | ||
MarkdownTextObject.builder().text("* User IP :*\n" + errorUserIP).build(); | ||
layoutBlocks.add( | ||
section( | ||
section -> | ||
section.fields(List.of(errorUserIdMarkdown, errorUserIpMarkdown)))); | ||
|
||
MarkdownTextObject methodMarkdown = | ||
MarkdownTextObject.builder() | ||
.text("* Request Addr :*\n" + method + " : " + url) | ||
.build(); | ||
MarkdownTextObject bodyMarkdown = | ||
MarkdownTextObject.builder().text("* Request Body :*\n" + body).build(); | ||
List<TextObject> fields = List.of(methodMarkdown, bodyMarkdown); | ||
layoutBlocks.add(section(section -> section.fields(fields))); | ||
|
||
layoutBlocks.add(divider()); | ||
|
||
MarkdownTextObject errorNameMarkdown = | ||
MarkdownTextObject.builder().text("* Message :*\n" + errorMessage).build(); | ||
MarkdownTextObject errorStackMarkdown = | ||
MarkdownTextObject.builder().text("* Stack Trace :*\n" + errorStack).build(); | ||
layoutBlocks.add( | ||
section(section -> section.fields(List.of(errorNameMarkdown, errorStackMarkdown)))); | ||
|
||
ChatPostMessageRequest chatPostMessageRequest = | ||
ChatPostMessageRequest.builder() | ||
.channel(CHANNEL_ID) | ||
.text("") | ||
.blocks(layoutBlocks) | ||
.build(); | ||
try { | ||
methodsClient.chatPostMessage(chatPostMessageRequest); | ||
} catch (SlackApiException slackApiException) { | ||
log.error(slackApiException.toString()); | ||
} | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...Infrastructure/src/main/java/band/gosrock/infrastructure/config/slack/SlackApiConfig.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,21 @@ | ||
package band.gosrock.infrastructure.config.slack; | ||
|
||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.methods.MethodsClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SlackApiConfig { | ||
|
||
@Value("${slack.webhook.token}") | ||
private String token; | ||
|
||
@Bean | ||
public MethodsClient getClient() { | ||
Slack slackClient = Slack.getInstance(); | ||
return slackClient.methods(token); | ||
} | ||
} |
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
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.
환경변수 없데이트 카톡으로 확인했슴둥