Skip to content

Commit

Permalink
merge: (#672) 서버 버그 발생시 slack 으로 메세지 송신
Browse files Browse the repository at this point in the history
  • Loading branch information
zios0707 authored Aug 30, 2024
2 parents 60a8ba1 + 48b0ab3 commit c750c43
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/xquare-cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ jobs:
S3_BUCKET_NAME=${{ secrets.S3_BUCKET_NAME }}
FCM_FILE_URL=${{ secrets.FCM_FILE_URL }}
FLYWAY_ENABLED=${{ secrets.FLYWAY_ENABLED }}
BASELINE_ON_MIGRATE=${{ secrets.BASELINE_ON_MIGRATE }}
BASELINE_ON_MIGRATE=${{ secrets.BASELINE_ON_MIGRATE }}
# SLACK_WEBHOOK_URL=${{ secrets.SLACK_STAG_WEBHOOK_URL }}
3 changes: 2 additions & 1 deletion .github/workflows/xquare-cd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ jobs:
S3_BUCKET_NAME=${{ secrets.S3_BUCKET_NAME }}
FCM_FILE_URL=${{ secrets.FCM_FILE_URL }}
FLYWAY_ENABLED=${{ secrets.FLYWAY_ENABLED }}
BASELINE_ON_MIGRATE=${{ secrets.BASELINE_ON_MIGRATE }}
BASELINE_ON_MIGRATE=${{ secrets.BASELINE_ON_MIGRATE }}
SLACK_WEBHOOK_URL=${{ secrets.SLACK_PROD_WEBHOOK_URL }}
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ ENV FLYWAY_ENABLED ${FLYWAY_ENABLED}
ARG BASELINE_ON_MIGRATE
ENV BASELINE_ON_MIGRATE ${BASELINE_ON_MIGRATE}

ARG SLACK_WEBHOOK_URL
ENV SLACK_WEBHOOK_URL ${SLACK_WEBHOOK_URL}

COPY ./dms-infrastructure/build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ object Dependencies {
// flyway
const val FLYWAY = "org.flywaydb:flyway-mysql:${DependencyVersions.FLYWAY_VERSION}"

// slack
const val SLACK = "com.slack.api:slack-api-client:${DependencyVersions.SLACK_VERSION}"

}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/DependencyVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ object DependencyVersions {
const val ASPECTJ_VERSION = "1.9.7"
const val FCM_VERSION = "8.1.0"
const val FLYWAY_VERSION = "10.8.1"
const val SLACK_VERSION = "1.40.3"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import team.aliens.dms.common.annotation.Service

@Service
class BugService(
commandBugService: CommandBugService
) : CommandBugService by commandBugService
commandBugService: CommandBugService,
sendBugService: SendBugService
) : CommandBugService by commandBugService,
SendBugService by sendBugService
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.aliens.dms.domain.bug.service

import team.aliens.dms.domain.bug.model.BugReport

interface SendBugService {

fun sendBugReport(bugReport: BugReport)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package team.aliens.dms.domain.bug.service

import team.aliens.dms.common.annotation.Service
import team.aliens.dms.domain.bug.model.BugReport
import team.aliens.dms.domain.bug.spi.SendBugPort

@Service
class SendBugServiceImpl(
private val sendBugPort: SendBugPort
) : SendBugService {

override fun sendBugReport(bugReport: BugReport) {
sendBugPort.sendBugReport(bugReport)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.aliens.dms.domain.bug.spi

import team.aliens.dms.domain.bug.model.BugReport

interface SendBugPort {

fun sendBugReport(bugReport: BugReport)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class CreateBugReportUseCase(

val attachmentUrls = request.attachmentUrls ?: emptyList()

bugService.saveBugReport(
BugReport(
studentId = student.id,
content = request.content,
developmentArea = DevelopmentArea.valueOf(request.developmentArea),
createdAt = LocalDateTime.now(),
attachmentUrls = BugAttachment(attachmentUrls)
bugService.sendBugReport(
bugService.saveBugReport(
BugReport(
studentId = student.id,
content = request.content,
developmentArea = DevelopmentArea.valueOf(request.developmentArea),
createdAt = LocalDateTime.now(),
attachmentUrls = BugAttachment(attachmentUrls)
)
)
)
}
Expand Down
3 changes: 3 additions & 0 deletions dms-infrastructure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
implementation(Dependencies.AWS_SES)
implementation(Dependencies.SPRING_AWS)

// slack
implementation(Dependencies.SLACK)

// configuration
kapt(Dependencies.CONFIGURATION_PROCESSOR)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import team.aliens.dms.common.error.DmsException
import team.aliens.dms.common.error.ErrorProperty
import team.aliens.dms.global.error.ErrorResponse
import team.aliens.dms.global.error.GlobalErrorCode
import team.aliens.dms.thirdparty.slack.SlackAdapter
import java.nio.charset.StandardCharsets

class ExceptionFilter(
private val objectMapper: ObjectMapper
private val objectMapper: ObjectMapper,
private val slackAdapter: SlackAdapter
) : OncePerRequestFilter() {

override fun doFilterInternal(
Expand All @@ -37,6 +39,7 @@ class ExceptionFilter(
}
else -> {
errorToJson(GlobalErrorCode.INTERNAL_SERVER_ERROR, response)
slackAdapter.sendServerBug(request, response, e)
Sentry.captureException(e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import org.springframework.security.web.DefaultSecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.stereotype.Component
import team.aliens.dms.global.security.token.JwtParser
import team.aliens.dms.thirdparty.slack.SlackAdapter

@Component
class FilterConfig(
private val jwtParser: JwtParser,
private val objectMapper: ObjectMapper
private val objectMapper: ObjectMapper,
private val slackAdapter: SlackAdapter
) : SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> {
override fun init(builder: HttpSecurity?) {}

override fun configure(builder: HttpSecurity) {
builder.addFilterBefore(JwtFilter(jwtParser), UsernamePasswordAuthenticationFilter::class.java)
builder.addFilterBefore(ExceptionFilter(objectMapper), JwtFilter::class.java)
builder.addFilterBefore(ExceptionFilter(objectMapper, slackAdapter), JwtFilter::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package team.aliens.dms.thirdparty.slack

import com.slack.api.Slack
import com.slack.api.model.block.Blocks.divider
import com.slack.api.model.block.Blocks.header
import com.slack.api.model.block.Blocks.image
import com.slack.api.model.block.Blocks.section
import com.slack.api.model.block.LayoutBlock
import com.slack.api.model.block.composition.BlockCompositions.markdownText
import com.slack.api.model.block.composition.BlockCompositions.plainText
import com.slack.api.model.block.composition.OptionObject
import com.slack.api.model.block.element.BlockElements.staticSelect
import com.slack.api.webhook.Payload
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import team.aliens.dms.domain.bug.model.BugReport
import team.aliens.dms.domain.bug.spi.SendBugPort
import java.lang.Exception

@Component
class SlackAdapter(
@Value("\${slack.url}")
private val url: String,

) : SendBugPort {

private val slack = Slack.getInstance()

override fun sendBugReport(bugReport: BugReport) {
val blocks = toMutableBlocks(
header { it.text(plainText("버그 제보가 들어왔습니다.")) },
section { it.text(plainText("제보자 : ${bugReport.studentId}")) },
section { it.text(plainText("OS : ${bugReport.developmentArea.name}")) },
section { it.text(plainText("message : ${bugReport.content}")) }
)

blocks
.addImages(bugReport.attachmentUrls?.attachmentUrls ?: emptyList())
.addSelectProgress()
.sendBug()
}

fun sendServerBug(request: HttpServletRequest, response: HttpServletResponse, exception: Exception) {
val blocks = toMutableBlocks(
header { it.text(plainText("서버에 예외가 발생했습니다.")) },
section { it.text(plainText("requested info : <${request.method}> ${request.requestURL}")) },
section { it.text(plainText("exception class : ${exception.javaClass.name}")) },
section { it.text(plainText("exception message : ${exception.message}")) },
)

blocks
.addSelectProgress()
.sendBug()
}

private fun toMutableBlocks(vararg blocks: LayoutBlock): MutableList<LayoutBlock> {
return blocks.toMutableList()
}

private fun MutableList<LayoutBlock>.addSelectProgress(): MutableList<LayoutBlock> {
this.addAll(
listOf(
divider(),
section { section ->
section.text(markdownText("현재 버그 상황을 선택 해주세요."))
section.accessory(
staticSelect {
it.placeholder(plainText("Select a progress status"))
it.options(
listOf(
OptionObject.builder().text(plainText("미해결")).value("not-in-progress").build(),
OptionObject.builder().text(plainText("해결중")).value("in-progress").build(),
OptionObject.builder().text(plainText("해결됨")).value("solved").build(),
OptionObject.builder().text(plainText("논외")).value("out-of-topic").build(),
)
)
it.initialOption(
OptionObject.builder().text(plainText("미해결")).value("not-in-progress").build()
)
}
)
},
divider()
)
)

return this
}

private fun MutableList<LayoutBlock>.addImages(attachmentUrls: List<String>): MutableList<LayoutBlock> {
this.addAll(
attachmentUrls.map { url ->
image { it.imageUrl(url).altText("bug image") }
}
)

return this
}

private fun MutableList<LayoutBlock>.sendBug() {
slack.send(
url,
Payload.builder()
.blocks(this)
.build()
)
}
}
5 changes: 4 additions & 1 deletion dms-infrastructure/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ logging:
com:
amazonaws:
util:
EC2MetadataUtils: error
EC2MetadataUtils: error

slack:
url: ${SLACK_WEBHOOK_URL:https://hooks.slack.com/services/null/null/null}

0 comments on commit c750c43

Please sign in to comment.