Skip to content

Commit

Permalink
Merge branch 'feature/DRAW-241' into sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
comforest committed Aug 24, 2024
2 parents f6e926f + 47312bf commit 5865fdc
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation(project(":app:websocket"))
implementation(project(":core"))
implementation(project(":support:logging"))
implementation(project(":support:metric"))
implementation(project(":support:yaml"))

implementation("org.springframework.boot:spring-boot-starter-web:${Versions.SPRING_BOOT}")
Expand Down
3 changes: 2 additions & 1 deletion app/websocket/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ dependencies {
implementation(project(":app:support:auth"))
implementation(project(":app:support:exception"))
implementation(project(":core"))
implementation(project(":support:yaml"))
implementation(project(":support:logging"))
implementation(project(":support:metric"))
implementation(project(":support:yaml"))

implementation("org.springframework.boot:spring-boot-starter-web:${Versions.SPRING_BOOT}")
implementation("org.springframework.boot:spring-boot-starter-websocket:${Versions.SPRING_BOOT}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xorker.draw.websocket

import com.xorker.draw.exception.XorkerException
import com.xorker.draw.support.metric.MetricManager
import com.xorker.draw.websocket.exception.WebSocketExceptionHandler
import com.xorker.draw.websocket.log.WebSocketLogger
import com.xorker.draw.websocket.parser.WebSocketRequestParser
Expand All @@ -18,9 +19,11 @@ class MainWebSocketHandler(
private val sessionEventListener: List<SessionEventListener>,
private val webSocketExceptionHandler: WebSocketExceptionHandler,
private val webSocketLogger: WebSocketLogger,
private val metricManager: MetricManager,
) : TextWebSocketHandler() {
override fun afterConnectionEstablished(session: WebSocketSession) {
webSocketLogger.afterConnectionEstablished(session)
metricManager.increaseWebsocket()
}

override fun handleTextMessage(session: WebSocketSession, message: TextMessage) {
Expand All @@ -36,7 +39,9 @@ class MainWebSocketHandler(
}

override fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) {
metricManager.decreaseWebsocket()
webSocketLogger.afterConnectionClosed(session, status)

val sessionDto = sessionUseCase.getSession(SessionId(session.id)) ?: return // TODO error logging

when (status) {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ object Versions {
const val WEBMVC_UI = "2.5.0"
const val COROUTINES = "1.6.4"
const val LOGBACK = "0.1.5"
const val MICROMETER = "1.12.9"
}
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
implementation(project(":adapter:oauth"))
implementation(project(":adapter:rdb"))
implementation(project(":support:jwt"))
implementation(project(":support:metric"))
implementation(project(":support:time"))

implementation("org.springframework.boot:spring-boot-starter:${Versions.SPRING_BOOT}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.xorker.draw.mafia.MafiaPhase
import com.xorker.draw.mafia.MafiaPhaseMessenger
import com.xorker.draw.mafia.assertIs
import com.xorker.draw.mafia.event.JobWithStartTime
import com.xorker.draw.support.metric.MetricManager
import com.xorker.draw.timer.TimerRepository
import org.springframework.stereotype.Component

Expand All @@ -17,6 +18,7 @@ internal class MafiaPhaseEndGameProcessor(
private val timerRepository: TimerRepository,
private val mafiaPhaseMessenger: MafiaPhaseMessenger,
private val mafiaGameMessenger: MafiaGameMessenger,
private val metricManager: MetricManager,
) {

// TODO 게임 결과 DB 저장
Expand Down Expand Up @@ -58,6 +60,8 @@ internal class MafiaPhaseEndGameProcessor(
val phase = gameInfo.phase
assertIs<MafiaPhase.End>(phase)

metricManager.decreaseGameCount()

val room = gameInfo.room

val players = room.players
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.xorker.draw.mafia.MafiaGameMessenger
import com.xorker.draw.mafia.MafiaGameRepository
import com.xorker.draw.mafia.MafiaPhase
import com.xorker.draw.mafia.assertIs
import com.xorker.draw.support.metric.MetricManager
import com.xorker.draw.timer.TimerRepository
import org.springframework.stereotype.Component

Expand All @@ -13,12 +14,15 @@ internal class MafiaPhasePlayGameProcessor(
private val timerRepository: TimerRepository,
private val mafiaGameMessenger: MafiaGameMessenger,
private val mafiaGameRepository: MafiaGameRepository,
private val metricManager: MetricManager,
) {

internal fun playMafiaGame(gameInfo: MafiaGameInfo, nextStep: () -> Unit): MafiaPhase.Playing {
val phase = gameInfo.phase
assertIs<MafiaPhase.Ready>(phase)

metricManager.increaseGameCount()

val gameOption = gameInfo.gameOption

var time = gameOption.turnTime
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include(
"domain",
"core",
"support:jwt",
"support:metric",
"support:logging",
"support:time",
"support:yaml",
Expand Down
11 changes: 11 additions & 0 deletions support/metric/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.springframework.boot.gradle.tasks.bundling.BootJar

dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator:${Versions.SPRING_BOOT}")
implementation("io.micrometer:micrometer-registry-prometheus:${Versions.MICROMETER}")
}

tasks {
withType<Jar> { enabled = true }
withType<BootJar> { enabled = false }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.xorker.draw.support.metric

import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import java.util.concurrent.atomic.AtomicInteger
import org.springframework.stereotype.Component

@Component
class MetricManager(
private val metric: MeterRegistry,
) {
private val connectedWebSocketCount: AtomicInteger = AtomicInteger()
private val playingPlayingRoomCount: AtomicInteger = AtomicInteger()

init {
Gauge
.builder("connect_websocket_gauge", connectedWebSocketCount) { it.toDouble() }
.register(metric)

Gauge
.builder("playing_game_room_gauge", playingPlayingRoomCount) { it.toDouble() }
.register(metric)

}

fun increaseWebsocket() {
connectedWebSocketCount.incrementAndGet()
}

fun decreaseWebsocket() {
connectedWebSocketCount.decrementAndGet()
}

fun increaseGameCount(){
playingPlayingRoomCount.incrementAndGet()
}

fun decreaseGameCount(){
playingPlayingRoomCount.decrementAndGet()
}
}
11 changes: 11 additions & 0 deletions support/metric/src/main/resources/application-metric.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
management:
endpoints:
web:
exposure:
include: prometheus
endpoint:
prometheus:
enabled: true
# metrics:
# tags:
# application: ${spring.application.name}

0 comments on commit 5865fdc

Please sign in to comment.