From 8f76212fb1a5d4c5ccf278d71ff5c5c8765ace08 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 14 Mar 2021 00:58:19 +0100 Subject: Better metrics for connected players --- .../server/config/StompDisconnectionListener.kt | 26 ++++++++++++++++++---- .../server/repositories/PlayerRepository.kt | 12 +++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt index a33584e0..82e6bf55 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt @@ -1,24 +1,42 @@ package org.luxons.sevenwonders.server.config +import io.micrometer.core.instrument.MeterRegistry import org.luxons.sevenwonders.server.controllers.GameController import org.luxons.sevenwonders.server.controllers.LobbyController import org.luxons.sevenwonders.server.repositories.PlayerRepository -import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.context.ApplicationListener import org.springframework.stereotype.Component +import org.springframework.web.socket.messaging.AbstractSubProtocolEvent +import org.springframework.web.socket.messaging.SessionConnectedEvent import org.springframework.web.socket.messaging.SessionDisconnectEvent +import java.util.concurrent.atomic.AtomicInteger @Component internal class StompDisconnectionListener( private val lobbyController: LobbyController, private val playerRepository: PlayerRepository, private val gameController: GameController, -) : ApplicationListener { + meterRegistry: MeterRegistry, +) : ApplicationListener { - private val logger: Logger = LoggerFactory.getLogger(StompDisconnectionListener::class.java) + private val logger = LoggerFactory.getLogger(StompDisconnectionListener::class.java) - override fun onApplicationEvent(event: SessionDisconnectEvent) { + private val activeConnections: AtomicInteger = meterRegistry.gauge("clients.connected", AtomicInteger(0))!! + + override fun onApplicationEvent(event: AbstractSubProtocolEvent) { + when (event) { + is SessionConnectedEvent -> onConnected() + is SessionDisconnectEvent -> onDisconnect(event) + } + } + + private fun onConnected() { + activeConnections.incrementAndGet() + } + + private fun onDisconnect(event: SessionDisconnectEvent) { + activeConnections.decrementAndGet() val principal = event.user if (principal == null) { logger.error("Received session disconnect event without a principal (sessionId = ${event.sessionId})") 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 d1d5b211..212f7a0e 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,6 +1,7 @@ package org.luxons.sevenwonders.server.repositories import io.micrometer.core.instrument.MeterRegistry +import io.micrometer.core.instrument.Tag import org.luxons.sevenwonders.model.api.actions.Icon import org.luxons.sevenwonders.server.ApiMisuseException import org.luxons.sevenwonders.server.lobby.Player @@ -11,7 +12,16 @@ import java.util.concurrent.ConcurrentHashMap class PlayerRepository( meterRegistry: MeterRegistry, ) { - private val players = meterRegistry.gaugeMapSize("players.count", emptyList(), ConcurrentHashMap())!! + private val players = ConcurrentHashMap() + + init { + meterRegistry.gauge("players.count", listOf(Tag.of("type", "human")), this) { + players.count { it.value.isHuman }.toDouble() + } + meterRegistry.gauge("players.count", listOf(Tag.of("type", "robot")), this) { + players.count { !it.value.isHuman }.toDouble() + } + } operator fun contains(username: String): Boolean = players.containsKey(username) -- cgit