-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature | #79 | @lcomment | slack webhook 구성 및 ExceptionHandler에 추가 #90
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
18 changes: 18 additions & 0 deletions
18
lovebird-api/src/main/kotlin/com/lovebird/api/config/SlackWebhookConfig.kt
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,18 @@ | ||
package com.lovebird.api.config | ||
|
||
import net.gpedro.integrations.slack.SlackApi | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class SlackWebhookConfig( | ||
@Value("\${slack.webhook.url}") | ||
private val slackWebhookUrl: String | ||
) { | ||
|
||
@Bean | ||
fun slackApi(): SlackApi { | ||
return SlackApi(slackWebhookUrl) | ||
} | ||
} |
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
lovebird-api/src/main/kotlin/com/lovebird/api/service/slack/SlackService.kt
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.lovebird.api.service.slack | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import net.gpedro.integrations.slack.SlackApi | ||
import net.gpedro.integrations.slack.SlackAttachment | ||
import net.gpedro.integrations.slack.SlackField | ||
import net.gpedro.integrations.slack.SlackMessage | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.core.env.Environment | ||
import org.springframework.stereotype.Service | ||
import java.time.LocalDateTime | ||
|
||
@Service | ||
class SlackService( | ||
private val slackApi: SlackApi, | ||
private val environment: Environment, | ||
@Value("\${slack.webhook.is-enable}") | ||
private val isEnable: Boolean | ||
) { | ||
|
||
fun sendSlackForError(exception: Exception, request: HttpServletRequest) { | ||
if (!isEnable) { | ||
return | ||
} | ||
val slackAttachment = SlackAttachment() | ||
slackAttachment.setFallback("Error") | ||
slackAttachment.setColor("danger") | ||
slackAttachment.setTitle("Error Detect") | ||
slackAttachment.setTitleLink(request.contextPath) | ||
slackAttachment.setText(exception.stackTraceToString()) | ||
slackAttachment.setFields( | ||
listOf( | ||
SlackField().setTitle("Request URL").setValue(request.requestURL.toString()), | ||
SlackField().setTitle("Request Method").setValue(request.method), | ||
SlackField().setTitle("Request Parameter").setValue(getRequestParameters(request)), | ||
SlackField().setTitle("Request Time").setValue(LocalDateTime.now().toString()), | ||
SlackField().setTitle("Request IP").setValue(request.remoteAddr), | ||
SlackField().setTitle("Request User-Agent").setValue(request.getHeader("User-Agent")) | ||
) | ||
) | ||
val profile = environment.getProperty("spring.profiles.default") | ||
val slackMessage = SlackMessage() | ||
|
||
slackMessage.setAttachments(listOf(slackAttachment)) | ||
slackMessage.setChannel("#error-log") | ||
slackMessage.setUsername("$profile API Error") | ||
slackMessage.setIcon(":alert:") | ||
slackMessage.setText("$profile api 에러 발생") | ||
|
||
slackApi.call(slackMessage) | ||
} | ||
|
||
private fun getRequestParameters(request: HttpServletRequest): String { | ||
val parameterMap = request.parameterMap | ||
val sb = StringBuilder() | ||
parameterMap.forEach { (key, value) -> | ||
sb.append("$key: ${value.joinToString(", ")}\n") | ||
} | ||
return sb.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,7 @@ jwt: | |
refresh-token: 2592000000 | ||
grant-type: Bearer | ||
|
||
slack: | ||
webhook: | ||
is-enable: true | ||
url: ${SLACK_WEBHOOK_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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,7 @@ jwt: | |
refresh-token: 2592000000 | ||
grant-type: Bearer | ||
|
||
slack: | ||
webhook: | ||
is-enable: false | ||
url: 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
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
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.
Error 전체를 응답하는 거 말고 추출해서 간략화하는 방법은은 없을까요?
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.
유의미한 error stack flow를 뽑는 것에 대한 내용은 없어서요.. 이후에 찾아봐야 할 것 같아요