-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: JooQ 버전 수정 3.19.9 -> 3.19.10 * refactor: JooQ 의존성 버전 지정 * refactor: jooq starter 의존성 사용 가능으로 중복 선언 의존성 삭제 * feat: ExceptionTranslator 생성 * feat: NativeSQLLogger 생성 * feat: PerformanceListener 생성 * feat: JooqConfig 생성 * feat: SlowQueryException 생성 * refactor: SlowQueryException 적용 * feat: SlowQuery 훅 설정 * refactor: 이벤트 발행 방식으로 수정 * refactor: ExceptionTranslator로 인한 발생 예외 변경 반영 * chore: 코드 줄바꿈 린트 적용 * refactor: 변수로 분리 * refactor: 비동기 동작 코드가 MDC 컨텍스트 공유할 수 있도록 수정 * refactor: PerformanceListener watch 생성 구현 수정 * refactor: ExceptionTranslator 구현 수정 * refactor: IntegrityConstraintViolationException로 반환 수정 * refactor: DuplicateKeyException로 반환 수정
- Loading branch information
1 parent
ffaf871
commit ceae194
Showing
16 changed files
with
239 additions
and
23 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
17 changes: 17 additions & 0 deletions
17
api-repo/src/main/kotlin/com/few/api/repo/common/ExceptionTranslator.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,17 @@ | ||
package com.few.api.repo.common | ||
|
||
import org.jooq.ExecuteContext | ||
import org.jooq.ExecuteListener | ||
import org.springframework.jdbc.support.SQLExceptionTranslator | ||
|
||
class ExceptionTranslator( | ||
private val translator: SQLExceptionTranslator, | ||
) : ExecuteListener { | ||
|
||
override fun exception(context: ExecuteContext) { | ||
context.exception( | ||
translator | ||
.translate("Access database using Jooq", context.sql(), context.sqlException()!!) | ||
) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
api-repo/src/main/kotlin/com/few/api/repo/common/NativeSQLLogger.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,16 @@ | ||
package com.few.api.repo.common | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.jooq.ExecuteContext | ||
import org.jooq.ExecuteListener | ||
import org.jooq.impl.DSL | ||
|
||
class NativeSQLLogger : ExecuteListener { | ||
private val log = KotlinLogging.logger {} | ||
|
||
override fun executeEnd(ctx: ExecuteContext) { | ||
ctx.query()?.let { | ||
log.debug { "Query executed: ${DSL.using(ctx.configuration()).renderInlined(ctx.query())}" } | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
api-repo/src/main/kotlin/com/few/api/repo/common/PerformanceListener.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,30 @@ | ||
package com.few.api.repo.common | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.jooq.ExecuteContext | ||
import org.jooq.ExecuteListener | ||
import org.jooq.tools.StopWatch | ||
import org.springframework.context.ApplicationEventPublisher | ||
|
||
class PerformanceListener( | ||
private val applicationEventPublisher: ApplicationEventPublisher, | ||
private var watch: StopWatch = StopWatch(), | ||
) : ExecuteListener { | ||
private val log = KotlinLogging.logger {} | ||
|
||
companion object { | ||
const val SLOW_QUERY_STANDARD = 5000000000L // 5 seconds | ||
} | ||
override fun executeStart(ctx: ExecuteContext) { | ||
super.executeStart(ctx) | ||
watch = StopWatch() | ||
} | ||
|
||
override fun executeEnd(ctx: ExecuteContext) { | ||
super.executeEnd(ctx) | ||
if (watch.split() > SLOW_QUERY_STANDARD) { | ||
log.warn { "Slow Query Detected: \n${ctx.query()}" } | ||
applicationEventPublisher.publishEvent(SlowQueryEvent(ctx.query().toString())) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api-repo/src/main/kotlin/com/few/api/repo/common/SlowQueryEvent.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,5 @@ | ||
package com.few.api.repo.common | ||
|
||
data class SlowQueryEvent( | ||
val slowQuery: String, | ||
) |
41 changes: 41 additions & 0 deletions
41
api-repo/src/main/kotlin/com/few/api/repo/config/JooqConfig.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,41 @@ | ||
package com.few.api.repo.config | ||
|
||
import com.few.api.repo.common.ExceptionTranslator | ||
import com.few.api.repo.common.NativeSQLLogger | ||
import com.few.api.repo.common.PerformanceListener | ||
import org.jooq.SQLDialect | ||
import org.jooq.impl.DataSourceConnectionProvider | ||
import org.jooq.impl.DefaultConfiguration | ||
import org.jooq.impl.DefaultDSLContext | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator | ||
import javax.sql.DataSource | ||
|
||
@Configuration | ||
class JooqConfig( | ||
private val dataSource: DataSource, | ||
private val applicationEventPublisher: ApplicationEventPublisher, | ||
) { | ||
@Bean | ||
fun dsl(): DefaultDSLContext { | ||
return DefaultDSLContext(configuration()) | ||
} | ||
|
||
@Bean | ||
fun configuration(): DefaultConfiguration { | ||
val jooqConfiguration = DefaultConfiguration() | ||
jooqConfiguration.set(connectionProvider()) | ||
val translator = | ||
SQLErrorCodeSQLExceptionTranslator(SQLDialect.MYSQL.name) | ||
jooqConfiguration.set(ExceptionTranslator(translator), NativeSQLLogger(), PerformanceListener(applicationEventPublisher)) | ||
jooqConfiguration.set(SQLDialect.MYSQL) | ||
return jooqConfiguration | ||
} | ||
|
||
@Bean | ||
fun connectionProvider(): DataSourceConnectionProvider { | ||
return DataSourceConnectionProvider(dataSource) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.few.api.client.repo | ||
|
||
import com.few.api.client.config.properties.DiscordBodyProperty | ||
import com.few.api.client.config.properties.Embed | ||
import com.few.api.client.repo.dto.RepoAlterArgs | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Service | ||
class RepoClient( | ||
private val restTemplate: RestTemplate, | ||
@Value("\${webhook.discord}") private val discordWebhook: String, | ||
) { | ||
private val log = KotlinLogging.logger {} | ||
|
||
fun announceRepoAlter(args: RepoAlterArgs) { | ||
val embedsList = ArrayList<Embed>() | ||
args.let { | ||
embedsList.add( | ||
Embed( | ||
title = "Exception", | ||
description = "Slow Query Detected" | ||
) | ||
) | ||
it.requestURL.let { requestURL -> | ||
embedsList.add( | ||
Embed( | ||
title = "Request URL", | ||
description = requestURL | ||
) | ||
) | ||
} | ||
it.query?.let { query -> | ||
embedsList.add( | ||
Embed( | ||
title = "Slow Query Detected", | ||
description = query | ||
) | ||
) | ||
} | ||
} | ||
args.let { | ||
DiscordBodyProperty( | ||
content = "😭 DB 이상 발생", | ||
embeds = embedsList | ||
) | ||
}.let { body -> | ||
restTemplate.exchange( | ||
discordWebhook, | ||
HttpMethod.POST, | ||
HttpEntity(body), | ||
String::class.java | ||
).let { res -> | ||
log.info { "Discord webhook response: ${res.statusCode}" } | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
api/src/main/kotlin/com/few/api/client/repo/dto/RepoAlterArgs.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,6 @@ | ||
package com.few.api.client.repo.dto | ||
|
||
data class RepoAlterArgs( | ||
val requestURL: String, | ||
val query: String?, | ||
) |
27 changes: 27 additions & 0 deletions
27
api/src/main/kotlin/com/few/api/client/repo/event/SlowQueryEventListener.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,27 @@ | ||
package com.few.api.client.repo.event | ||
|
||
import com.few.api.client.repo.RepoClient | ||
import com.few.api.client.repo.dto.RepoAlterArgs | ||
import com.few.api.config.ApiThreadPoolConfig.Companion.DISCORD_HOOK_EVENT_POOL | ||
import com.few.api.repo.common.SlowQueryEvent | ||
import org.slf4j.MDC | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.scheduling.annotation.Async | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SlowQueryEventListener( | ||
private val repoClient: RepoClient, | ||
) { | ||
|
||
@Async(value = DISCORD_HOOK_EVENT_POOL) | ||
@EventListener | ||
fun handleSlowQueryEvent(event: SlowQueryEvent) { | ||
RepoAlterArgs( | ||
requestURL = MDC.get("Request-URL") ?: "", | ||
query = event.slowQuery | ||
).let { | ||
repoClient.announceRepoAlter(it) | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
api/src/main/kotlin/com/few/api/config/ClonedTaskDecorator.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,16 @@ | ||
package com.few.api.config | ||
|
||
import org.slf4j.MDC | ||
import org.springframework.core.task.TaskDecorator | ||
|
||
class ClonedTaskDecorator : TaskDecorator { | ||
override fun decorate(runnable: Runnable): Runnable { | ||
val contextMap = MDC.getCopyOfContextMap() | ||
return Runnable { | ||
if (contextMap != null) { | ||
MDC.setContextMap(contextMap) | ||
} | ||
runnable.run() | ||
} | ||
} | ||
} |
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