diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-15 23:40:55 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-16 02:24:33 +0100 |
commit | 3dbd5a3a742ad4d998764d39dcdaf07a4a0791cd (patch) | |
tree | 9224f20fa90485079a91a0c62437952cf88a3890 | |
parent | Fix player icon size in GameBrowser (diff) | |
download | seven-wonders-3dbd5a3a742ad4d998764d39dcdaf07a4a0791cd.tar.gz seven-wonders-3dbd5a3a742ad4d998764d39dcdaf07a4a0791cd.tar.bz2 seven-wonders-3dbd5a3a742ad4d998764d39dcdaf07a4a0791cd.zip |
Add basic metrics
13 files changed, 83 insertions, 30 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt index 796dc719..a4aceb96 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/AutoGameController.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.MeterRegistry import kotlinx.coroutines.withTimeout import org.luxons.sevenwonders.bot.connectBot import org.luxons.sevenwonders.bot.connectBots @@ -12,7 +13,9 @@ import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController import java.security.Principal +import kotlin.time.measureTimedValue import kotlin.time.minutes +import kotlin.time.toJavaDuration /** * Handles actions in the game's lobby. The lobby is the place where players gather before a game. @@ -20,6 +23,7 @@ import kotlin.time.minutes @RestController class AutoGameController( @Value("\${server.port}") private val serverPort: String, + private val meterRegistry: MeterRegistry, ) { @PostMapping("/autoGame") suspend fun autoGame(@RequestBody action: AutoGameAction, principal: Principal): AutoGameResult { @@ -28,16 +32,20 @@ class AutoGameController( val serverUrl = "ws://localhost:$serverPort" val lastTurn = withTimeout(5.minutes) { - val otherBotNames = List(action.nbPlayers - 1) { "JoinerBot${it + 1}" } - val owner = client.connectBot(serverUrl, "OwnerBot", action.config) - val joiners = client.connectBots(serverUrl, otherBotNames, action.config) + val (lastTurn, duration) = measureTimedValue { + val otherBotNames = List(action.nbPlayers - 1) { "JoinerBot${it + 1}" } + val owner = client.connectBot(serverUrl, "OwnerBot", action.config) + val joiners = client.connectBots(serverUrl, otherBotNames, action.config) - owner.createGameWithBotFriendsAndAutoPlay( - gameName = action.gameName, - otherBots = joiners, - customWonders = action.customWonders, - customSettings = action.customSettings, - ) + owner.createGameWithBotFriendsAndAutoPlay( + gameName = action.gameName, + otherBots = joiners, + customWonders = action.customWonders, + customSettings = action.customSettings, + ) + } + meterRegistry.timer("autogame.duration").record(duration.toJavaDuration()) + lastTurn } val scoreBoard = lastTurn.scoreBoard ?: error("Last turn info doesn't have scoreboard") diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt index 47e01876..cff3aea0 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.MeterRegistry import org.luxons.sevenwonders.engine.Game import org.luxons.sevenwonders.model.api.GameListEvent import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction @@ -7,6 +8,7 @@ import org.luxons.sevenwonders.model.api.wrap import org.luxons.sevenwonders.model.cards.PreparedCard import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.api.toDTO +import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player import org.luxons.sevenwonders.server.repositories.LobbyRepository import org.luxons.sevenwonders.server.repositories.PlayerRepository @@ -24,6 +26,7 @@ class GameController( private val template: SimpMessagingTemplate, private val playerRepository: PlayerRepository, private val lobbyRepository: LobbyRepository, + private val meterRegistry: MeterRegistry, ) { private val Principal.player get() = playerRepository.get(name) @@ -99,9 +102,7 @@ class GameController( game.playTurn() sendTurnInfo(player.lobby.getPlayers(), game, hideHands = lobby.settings.askForReadiness) if (game.endOfGameReached()) { - logger.info("Game {}: end of game, displaying score board", game.id) - player.lobby.setEndOfGame() - template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobby.toDTO()).wrap()) + handleEndOfGame(game, player, lobby) } } else { template.convertAndSendToUser(player.username, "/queue/game/preparedMove", action.move) @@ -109,6 +110,13 @@ class GameController( } } + private fun handleEndOfGame(game: Game, player: Player, lobby: Lobby) { + meterRegistry.counter("games.finished").increment() + logger.info("Game {}: end of game, displaying score board", game.id) + player.lobby.setEndOfGame() + template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobby.toDTO()).wrap()) + } + @MessageMapping("/game/unprepareMove") fun unprepareMove(principal: Principal) { val player = principal.player diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt index b2fc122a..230623d8 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.MeterRegistry import org.luxons.sevenwonders.model.api.ConnectedPlayer import org.luxons.sevenwonders.model.api.PlayerDTO import org.luxons.sevenwonders.model.api.actions.ChooseNameAction @@ -17,8 +18,8 @@ import java.security.Principal @Controller class HomeController( private val playerRepository: PlayerRepository, + private val meterRegistry: MeterRegistry, ) { - /** * Creates/updates the player's name (for the user's session). * @@ -33,6 +34,7 @@ class HomeController( val username = principal.name val player = playerRepository.createOrUpdate(username, action.playerName, action.isHuman, action.icon) + meterRegistry.counter("players.connections").increment() logger.info("Player '{}' chose the name '{}'", username, player.displayName) return ConnectedPlayer(username, player.displayName, player.isHuman, player.icon) } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt index 2edf7fee..70ac50e7 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.MeterRegistry import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -16,6 +17,7 @@ import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player +import org.luxons.sevenwonders.server.metrics.playerCountsTags import org.luxons.sevenwonders.server.repositories.LobbyRepository import org.luxons.sevenwonders.server.repositories.PlayerRepository import org.slf4j.LoggerFactory @@ -36,6 +38,7 @@ class LobbyController( private val playerRepository: PlayerRepository, private val template: SimpMessagingTemplate, @Value("\${server.port}") private val serverPort: String, + private val meterRegistry: MeterRegistry, ) { private val Principal.player: Player get() = playerRepository.get(name) @@ -80,6 +83,7 @@ class LobbyController( } logger.info("Player {} disbanded game '{}'", player, lobby.name) deleteLobby(lobby) + meterRegistry.counter("games.disbanded", lobby.playerCountsTags()).increment() } } @@ -163,6 +167,7 @@ class LobbyController( val timeoutDuration = action.globalBotTimeoutMillis.milliseconds logger.error("Bot {} timed out after {}", action.botDisplayName, timeoutDuration) bot.disconnect() + meterRegistry.counter("bot.timeout", lobby.playerCountsTags()).increment() } } } @@ -177,6 +182,7 @@ class LobbyController( val lobby = principal.player.ownedLobby val game = lobby.startGame() + meterRegistry.counter("games.started").increment() logger.info("Game {} ('{}') successfully started", game.id, lobby.name) val currentTurnInfo = game.getCurrentTurnInfo().let { if (lobby.settings.askForReadiness) it.hideHandsAndWaitForReadiness() else it diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/metrics/MetricsUtils.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/metrics/MetricsUtils.kt new file mode 100644 index 00000000..6b91428e --- /dev/null +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/metrics/MetricsUtils.kt @@ -0,0 +1,13 @@ +package org.luxons.sevenwonders.server.metrics + +import io.micrometer.core.instrument.Tag +import org.luxons.sevenwonders.server.lobby.Lobby + +fun Lobby.playerCountsTags(): List<Tag> { + val players = getPlayers() + return listOf( + Tag.of("nPlayers", players.size.toString()), + Tag.of("nHumans", players.filter { it.isHuman }.size.toString()), + Tag.of("nBots", players.filterNot { it.isHuman }.size.toString()), + ) +} diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt index e45fb5e9..5b32c514 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.repositories +import io.micrometer.core.instrument.MeterRegistry import org.luxons.sevenwonders.engine.data.GameDefinition import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player @@ -8,9 +9,10 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicLong @Repository -class LobbyRepository { - - private val lobbies = ConcurrentHashMap<Long, Lobby>() +class LobbyRepository( + meterRegistry: MeterRegistry, +) { + private val lobbies = meterRegistry.gaugeMapSize("games.count", emptyList(), ConcurrentHashMap<Long, Lobby>())!! private var lastGameId: AtomicLong = AtomicLong(0) diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt index d2d8f967..d1d5b211 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.repositories +import io.micrometer.core.instrument.MeterRegistry import org.luxons.sevenwonders.model.api.actions.Icon import org.luxons.sevenwonders.server.ApiMisuseException import org.luxons.sevenwonders.server.lobby.Player @@ -7,9 +8,10 @@ import org.springframework.stereotype.Repository import java.util.concurrent.ConcurrentHashMap @Repository -class PlayerRepository { - - private val players = ConcurrentHashMap<String, Player>() +class PlayerRepository( + meterRegistry: MeterRegistry, +) { + private val players = meterRegistry.gaugeMapSize("players.count", emptyList(), ConcurrentHashMap<String, Player>())!! operator fun contains(username: String): Boolean = players.containsKey(username) diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserControllerTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserControllerTest.kt index df2e02ee..0f6e031b 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserControllerTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserControllerTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.model.api.GameListEvent @@ -23,10 +24,11 @@ class GameBrowserControllerTest { @Before fun setUp() { - playerRepository = PlayerRepository() - val lobbyRepository = LobbyRepository() + val meterRegistry = SimpleMeterRegistry() + playerRepository = PlayerRepository(meterRegistry) + val lobbyRepository = LobbyRepository(meterRegistry) val template = mockSimpMessagingTemplate() - val lobbyController = LobbyController(lobbyRepository, playerRepository, template, "UNUSED") + val lobbyController = LobbyController(lobbyRepository, playerRepository, template, "UNUSED", meterRegistry) gameBrowserController = GameBrowserController(lobbyController, lobbyRepository, playerRepository, template) } diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt index f820621a..c73fba10 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Test import org.luxons.sevenwonders.model.api.actions.ChooseNameAction import org.luxons.sevenwonders.model.api.actions.Icon @@ -10,8 +11,9 @@ class HomeControllerTest { @Test fun chooseName() { - val playerRepository = PlayerRepository() - val homeController = HomeController(playerRepository) + val meterRegistry = SimpleMeterRegistry() + val playerRepository = PlayerRepository(meterRegistry) + val homeController = HomeController(playerRepository, meterRegistry) val action = ChooseNameAction("Test User", Icon("person"), isHuman = true) val principal = TestPrincipal("testuser") diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt index 351c2e9e..7d9db01d 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.controllers +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.model.Settings @@ -27,10 +28,11 @@ class LobbyControllerTest { @Before fun setUp() { + val meterRegistry = SimpleMeterRegistry() val template = mockSimpMessagingTemplate() - playerRepository = PlayerRepository() - lobbyRepository = LobbyRepository() - lobbyController = LobbyController(lobbyRepository, playerRepository, template, "UNUSED") + playerRepository = PlayerRepository(meterRegistry) + lobbyRepository = LobbyRepository(meterRegistry) + lobbyController = LobbyController(lobbyRepository, playerRepository, template, "UNUSED", meterRegistry) } @Test diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepositoryTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepositoryTest.kt index 442edac2..eb80d909 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepositoryTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepositoryTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.repositories +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.server.lobby.Player @@ -15,7 +16,8 @@ class LobbyRepositoryTest { @Before fun setUp() { - repository = LobbyRepository() + val meterRegistry = SimpleMeterRegistry() + repository = LobbyRepository(meterRegistry) } @Test diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepositoryTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepositoryTest.kt index 8e554546..231cf610 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepositoryTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepositoryTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.repositories +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Before import org.junit.Test import kotlin.test.assertEquals @@ -14,7 +15,8 @@ class PlayerRepositoryTest { @Before fun setUp() { - repository = PlayerRepository() + val meterRegistry = SimpleMeterRegistry() + repository = PlayerRepository(meterRegistry) } @Test diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/validation/DestinationAccessValidatorTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/validation/DestinationAccessValidatorTest.kt index c5ef3f44..0bcb45f3 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/validation/DestinationAccessValidatorTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/validation/DestinationAccessValidatorTest.kt @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.validation +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.server.lobby.Lobby @@ -18,7 +19,8 @@ class DestinationAccessValidatorTest { @Before fun setup() { - lobbyRepository = LobbyRepository() + val meterRegistry = SimpleMeterRegistry() + lobbyRepository = LobbyRepository(meterRegistry) destinationAccessValidator = DestinationAccessValidator(lobbyRepository) } |