-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from AdultOfNineteen/feature/issue-70
500์๋ฌ ์ฌ๋ ์๋ฆผ ๊ตฌํ
- Loading branch information
Showing
13 changed files
with
183 additions
and
32 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
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
21 changes: 21 additions & 0 deletions
21
Winey-Common/src/main/java/com/example/wineycommon/config/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 com.example.wineycommon.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.methods.MethodsClient; | ||
|
||
@Configuration | ||
public class SlackApiConfig { | ||
|
||
@Value("${slack.token}") | ||
private String token; | ||
|
||
@Bean | ||
public MethodsClient getClient() { | ||
Slack slackClient = Slack.getInstance(); | ||
return slackClient.methods(token); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Winey-Common/src/main/java/com/example/wineycommon/constants/SlackStatic.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,24 @@ | ||
package com.example.wineycommon.constants; | ||
|
||
public class SlackStatic { | ||
public static final String EXCEPTION_CLASS_LABEL = "exception class : "; | ||
public static final String CODE_BLOCK_START = "```"; | ||
public static final String CODE_BLOCK_END = "```"; | ||
public static final String USER_LABEL = "์ฌ์ฉ์ : "; | ||
public static final String PROFILE_LABEL = "์คํ์ค์ธ ํ๊ฒฝ : "; | ||
public static final String SERVER_LABEL = "์คํ์ค์ธ ์๋ฒ : "; | ||
public static final String URI_LABEL = "์์ฒญ URI : "; | ||
public static final String METHOD_LABEL = "request method : "; | ||
public static final String QUERY_STRING_LABEL = "request query string : "; | ||
public static final String REMOTE_ADDR_LABEL = "request remote addr : "; | ||
public static final String REMOTE_HOST_LABEL = "request remote host : "; | ||
public static final String REMOTE_PORT_LABEL = "request remote port : "; | ||
public static final String SERVER_PORT_LABEL = "request server port : "; | ||
public static final String SERVLET_PATH_LABEL = "request servlet path : "; | ||
public static final String EXCEPTION_LABEL = "request exception : "; | ||
public static final String EXCEPTION_CLASS_MESSAGE_VALUE = "Error occurred in class : %s - at line : %s"; | ||
public static final String UNKNOWN_EXCEPTION_CLASS_VALUE = "Unknown Exception"; | ||
public static final String SLACK_IMG_URL = "https://avatars.slack-edge.com/2024-01-11/6460804813748_57623dce3b29216a9aa8_48.png"; | ||
public static final String SLACK_USER_NAME = "์๋ฒ ์๋ฌ ์๋ฆผ"; | ||
public static final String UNKNOWN_HOST_EXCEPTION_MESSAGE = "์ฌ๋ฐ๋ฅธ ์ฌ๋ URL์ด ์๋๋๋ค."; | ||
} |
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
52 changes: 52 additions & 0 deletions
52
Winey-Common/src/main/java/com/example/wineycommon/service/SlackConverter.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,52 @@ | ||
package com.example.wineycommon.service; | ||
|
||
import static com.example.wineycommon.constants.SlackStatic.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.slack.api.webhook.Payload; | ||
|
||
public class SlackConverter { | ||
|
||
public static String errorToSlackMessage(String user, HttpServletRequest request, Exception exception, String profile) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(CODE_BLOCK_START); | ||
appendLabelAndValue(sb, USER_LABEL, user); | ||
appendLabelAndValue(sb, PROFILE_LABEL, profile); | ||
appendLabelAndValue(sb, SERVER_LABEL, request.getServerName()); | ||
appendLabelAndValue(sb, URI_LABEL, request.getRequestURI()); | ||
appendLabelAndValue(sb, METHOD_LABEL, request.getMethod()); | ||
appendLabelAndValue(sb, QUERY_STRING_LABEL, request.getQueryString()); | ||
appendLabelAndValue(sb, EXCEPTION_CLASS_LABEL, getErrorOccurredClassName(exception)); | ||
appendLabelAndValue(sb, REMOTE_ADDR_LABEL, request.getRemoteAddr()); | ||
appendLabelAndValue(sb, REMOTE_HOST_LABEL, request.getRemoteHost()); | ||
appendLabelAndValue(sb, REMOTE_PORT_LABEL, Integer.toString(request.getRemotePort())); | ||
appendLabelAndValue(sb, SERVER_PORT_LABEL, Integer.toString(request.getServerPort())); | ||
appendLabelAndValue(sb, SERVLET_PATH_LABEL, request.getServletPath()); | ||
appendLabelAndValue(sb, EXCEPTION_LABEL, exception.getMessage()); | ||
sb.append(CODE_BLOCK_END); | ||
return sb.toString(); | ||
} | ||
|
||
private static void appendLabelAndValue(StringBuilder sb, String label, String value) { | ||
sb.append(label).append(value).append("\n"); | ||
} | ||
|
||
private static String getErrorOccurredClassName(Exception exception) { | ||
StackTraceElement[] stackTrace = exception.getStackTrace(); | ||
if (stackTrace.length > 0) { | ||
StackTraceElement firstStackTraceElement = stackTrace[0]; | ||
return String.format(EXCEPTION_CLASS_MESSAGE_VALUE, firstStackTraceElement.getClassName(), firstStackTraceElement.getLineNumber()); | ||
} | ||
return UNKNOWN_EXCEPTION_CLASS_VALUE; | ||
} | ||
|
||
public static Payload convertToPayload(String message) { | ||
return Payload | ||
.builder() | ||
.text(message) | ||
.username(SLACK_USER_NAME) | ||
.iconUrl(SLACK_IMG_URL) | ||
.build(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Winey-Common/src/main/java/com/example/wineycommon/service/SlackService.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,46 @@ | ||
package com.example.wineycommon.service; | ||
|
||
|
||
import static com.example.wineycommon.constants.SlackStatic.*; | ||
|
||
import java.io.IOException; | ||
import java.net.UnknownHostException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.webhook.Payload; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Service | ||
@Slf4j | ||
public class SlackService { | ||
@Value("${slack.webhook-url}") | ||
private String webhookUrl; | ||
|
||
@Value("${spring.config.activate.on-profile}") | ||
private String profile; | ||
|
||
|
||
@Async("slack-notification") | ||
public void sendMessage(String user, Exception exception, HttpServletRequest request){ | ||
final Slack slack = Slack.getInstance(); | ||
final String message = SlackConverter.errorToSlackMessage(user, request, exception, profile); | ||
final Payload payload = SlackConverter.convertToPayload(message); | ||
try { | ||
String responseBody = slack.send(webhookUrl, payload).getBody(); | ||
if (!StringUtils.equals(responseBody, "ok")) { | ||
throw new UnknownHostException(UNKNOWN_HOST_EXCEPTION_MESSAGE); | ||
} | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -33,3 +33,6 @@ spring: | |
host: ${REDIS_HOST} | ||
port: 6379 | ||
|
||
slack: | ||
webhook-url: ${SLACK_WEBHOOK_URL} | ||
token: ${SLACK_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
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