-
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.
- Loading branch information
Showing
11 changed files
with
245 additions
and
2 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
27 changes: 27 additions & 0 deletions
27
Core/src/main/java/allchive/server/core/event/events/slack/SlackAsyncErrorEvent.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,27 @@ | ||
package allchive.server.core.event.events.slack; | ||
|
||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SlackAsyncErrorEvent { | ||
private String name; | ||
private Throwable throwable; | ||
private Object[] params; | ||
|
||
@Builder | ||
private SlackAsyncErrorEvent(String name, Throwable throwable, Object[] params) { | ||
this.name = name; | ||
this.throwable = throwable; | ||
this.params = params; | ||
} | ||
|
||
public static SlackAsyncErrorEvent of(String name, Throwable throwable, Object[] params) { | ||
return SlackAsyncErrorEvent.builder() | ||
.name(name) | ||
.throwable(throwable) | ||
.params(params) | ||
.build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Core/src/main/java/allchive/server/core/event/events/slack/SlackErrorEvent.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,19 @@ | ||
package allchive.server.core.event.events.slack; | ||
|
||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SlackErrorEvent { | ||
private Exception exception; | ||
|
||
@Builder | ||
private SlackErrorEvent(Exception exception) { | ||
this.exception = exception; | ||
} | ||
|
||
public static SlackErrorEvent from(Exception exception) { | ||
return SlackErrorEvent.builder().exception(exception).build(); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
Infrastructure/src/main/java/allchive/server/infrastructure/slack/SlackHelper.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,31 @@ | ||
package allchive.server.infrastructure.slack; | ||
|
||
|
||
import allchive.server.core.helper.SpringEnvironmentHelper; | ||
import com.slack.api.Slack; | ||
import com.slack.api.webhook.Payload; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SlackHelper { | ||
private final SpringEnvironmentHelper springEnvironmentHelper; | ||
|
||
@Value("${slack.webhook.slackUrl}") | ||
String slackUrl; | ||
|
||
public void sendErrorNotification(Payload payload) { | ||
final Slack slack = Slack.getInstance(); | ||
|
||
try { | ||
if (springEnvironmentHelper.isProdProfile()) { | ||
slack.send(slackUrl, payload); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
Infrastructure/src/main/java/allchive/server/infrastructure/slack/SlackMessageGenerater.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,116 @@ | ||
package allchive.server.infrastructure.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 allchive.server.core.event.events.slack.SlackAsyncErrorEvent; | ||
import allchive.server.core.event.events.slack.SlackErrorEvent; | ||
import com.slack.api.model.block.Blocks; | ||
import com.slack.api.model.block.DividerBlock; | ||
import com.slack.api.model.block.HeaderBlock; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.MarkdownTextObject; | ||
import com.slack.api.model.block.composition.TextObject; | ||
import com.slack.api.webhook.Payload; | ||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SlackMessageGenerater { | ||
private final int MAX_LEN = 500; | ||
|
||
public Payload generateErrorMsg(SlackErrorEvent event) throws IOException { | ||
final Exception e = event.getException(); | ||
|
||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
// 제목 | ||
layoutBlocks.add(HeaderBlock.builder().text(plainText("에러 알림")).build()); | ||
// 구분선 | ||
layoutBlocks.add(new DividerBlock()); | ||
// timeØ | ||
layoutBlocks.add(getTime()); | ||
// IP + Method, Addr | ||
layoutBlocks.add(makeSection(getErrMessage(e), getErrStack(e))); | ||
|
||
return Payload.builder().text("에러 알림").blocks(layoutBlocks).build(); | ||
} | ||
|
||
private LayoutBlock getTime() { | ||
MarkdownTextObject timeObj = | ||
MarkdownTextObject.builder().text("* Time :*\n" + LocalDateTime.now()).build(); | ||
return Blocks.section(section -> section.fields(List.of(timeObj))); | ||
} | ||
|
||
private LayoutBlock makeSection(TextObject first, TextObject second) { | ||
return Blocks.section(section -> section.fields(List.of(first, second))); | ||
} | ||
|
||
private MarkdownTextObject getErrMessage(Exception e) { | ||
final String errorMessage = e.getMessage(); | ||
return MarkdownTextObject.builder().text("* Message :*\n" + errorMessage).build(); | ||
} | ||
|
||
private MarkdownTextObject getErrStack(Throwable throwable) { | ||
String exceptionAsString = Arrays.toString(throwable.getStackTrace()); | ||
int cutLength = Math.min(exceptionAsString.length(), MAX_LEN); | ||
String errorStack = exceptionAsString.substring(0, cutLength); | ||
return MarkdownTextObject.builder().text("* Stack Trace :*\n" + errorStack).build(); | ||
} | ||
|
||
public Payload generateAsyncErrorMsg(SlackAsyncErrorEvent event) { | ||
String name = event.getName(); | ||
Throwable throwable = event.getThrowable(); | ||
Object[] params = event.getParams(); | ||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
layoutBlocks.add( | ||
Blocks.header( | ||
headerBlockBuilder -> headerBlockBuilder.text(plainText("비동기 에러 알림")))); | ||
layoutBlocks.add(divider()); | ||
|
||
MarkdownTextObject errorUserIdMarkdown = | ||
MarkdownTextObject.builder().text("* 메소드 이름 :*\n" + name).build(); | ||
MarkdownTextObject errorUserIpMarkdown = | ||
MarkdownTextObject.builder() | ||
.text("* 요청 파라미터 :*\n" + getParamsToString(params)) | ||
.build(); | ||
layoutBlocks.add( | ||
section( | ||
section -> | ||
section.fields(List.of(errorUserIdMarkdown, errorUserIpMarkdown)))); | ||
|
||
layoutBlocks.add(divider()); | ||
layoutBlocks.add(getTime()); | ||
String errorStack = getErrorStack(throwable); | ||
String message = throwable.toString(); | ||
MarkdownTextObject errorNameMarkdown = | ||
MarkdownTextObject.builder().text("* Message :*\n" + message).build(); | ||
MarkdownTextObject errorStackMarkdown = | ||
MarkdownTextObject.builder().text("* Stack Trace :*\n" + errorStack).build(); | ||
layoutBlocks.add( | ||
section(section -> section.fields(List.of(errorNameMarkdown, errorStackMarkdown)))); | ||
return Payload.builder().text("비동기 에러 알림").blocks(layoutBlocks).build(); | ||
} | ||
|
||
private String getParamsToString(Object[] params) { | ||
StringBuilder paramToString = new StringBuilder(); | ||
for (Object param : params) { | ||
paramToString.append(param.toString()); | ||
} | ||
return paramToString.toString(); | ||
} | ||
|
||
private String getErrorStack(Throwable throwable) { | ||
String exceptionAsString = Arrays.toString(throwable.getStackTrace()); | ||
int cutLength = Math.min(exceptionAsString.length(), MAX_LEN); | ||
return exceptionAsString.substring(0, cutLength); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...structure/src/main/java/allchive/server/infrastructure/slack/SlackSendMessageHandler.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,34 @@ | ||
package allchive.server.infrastructure.slack; | ||
|
||
|
||
import allchive.server.core.event.events.slack.SlackAsyncErrorEvent; | ||
import allchive.server.core.event.events.slack.SlackErrorEvent; | ||
import com.slack.api.webhook.Payload; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SlackSendMessageHandler { | ||
private final SlackMessageGenerater slackMessageGenerater; | ||
private final SlackHelper slackHelper; | ||
|
||
@Async(value = "slackTaskExecutor") | ||
@EventListener(SlackErrorEvent.class) | ||
public void HandleError(SlackErrorEvent event) throws IOException { | ||
Payload payload = slackMessageGenerater.generateErrorMsg(event); | ||
slackHelper.sendErrorNotification(payload); | ||
} | ||
|
||
@Async(value = "slackTaskExecutor") | ||
@EventListener(SlackAsyncErrorEvent.class) | ||
public void HandleAsyncError(SlackAsyncErrorEvent event) throws IOException { | ||
Payload payload = slackMessageGenerater.generateAsyncErrorMsg(event); | ||
slackHelper.sendErrorNotification(payload); | ||
} | ||
} |
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