Skip to content

Commit

Permalink
feat: constants for metric names
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Aug 8, 2021
1 parent 97cf85d commit ce39520
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.common.metric

object Metrics {
object Events {
const val Login = "minecraft_events_login_total"
const val Join = "minecraft_events_join_total"
const val Quit = "minecraft_events_quit_total"
const val Chat = "minecraft_events_chat_total"
const val Ping = "minecraft_events_ping_total"
}

object Server {
const val Plugins = "minecraft_plugins"
const val PlayersCount = "minecraft_players_count"
const val PlayersMax = "minecraft_players_max"
const val TickDurationSeconds = "minecraft_tick_duration_seconds"
const val WorldEntitiesCount = "minecraft_world_entities_count"
const val WorldPlayersCount = "minecraft_world_players_count"
const val WorldLoadedChunks = "minecraft_world_loaded_chunks"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
import dev.cubxity.plugins.metrics.api.metric.collector.Counter
import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.common.metric.Metrics
import org.bukkit.event.EventHandler
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
Expand All @@ -35,11 +36,11 @@ import org.bukkit.event.server.ServerListPingEvent

@Suppress("UNUSED_PARAMETER")
class EventsCollection(private val bootstrap: UnifiedMetricsBukkitBootstrap) : CollectorCollection, Listener {
private val loginCounter = Counter("minecraft_events_login_total")
private val joinCounter = Counter("minecraft_events_join_total", valueStoreFactory = VolatileDoubleStore)
private val quitCounter = Counter("minecraft_events_quit_total", valueStoreFactory = VolatileDoubleStore)
private val chatCounter = Counter("minecraft_events_chat_total")
private val pingCounter = Counter("minecraft_events_ping_total") // TODO: is this async?
private val loginCounter = Counter(Metrics.Events.Login)
private val joinCounter = Counter(Metrics.Events.Join, valueStoreFactory = VolatileDoubleStore)
private val quitCounter = Counter(Metrics.Events.Quit, valueStoreFactory = VolatileDoubleStore)
private val chatCounter = Counter(Metrics.Events.Chat)
private val pingCounter = Counter(Metrics.Events.Ping) // TODO: is this async?

override val collectors: List<Collector> =
listOf(loginCounter, joinCounter, quitCounter, chatCounter, pingCounter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
import dev.cubxity.plugins.metrics.api.metric.data.Metric
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.common.metric.Metrics

class ServerCollector(private val bootstrap: UnifiedMetricsBukkitBootstrap) : Collector {
override fun collect(): List<Metric> {
val server = bootstrap.server
return listOf(
GaugeMetric("minecraft_plugins", value = server.pluginManager.plugins.size),
GaugeMetric("minecraft_players_count", value = server.onlinePlayers.size),
GaugeMetric("minecraft_players_max", value = server.maxPlayers)
GaugeMetric(Metrics.Server.Plugins, value = server.pluginManager.plugins.size),
GaugeMetric(Metrics.Server.PlayersCount, value = server.onlinePlayers.size),
GaugeMetric(Metrics.Server.PlayersMax, value = server.maxPlayers)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore
import dev.cubxity.plugins.metrics.api.metric.store.VolatileLongStore
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.bukkit.util.classExists
import dev.cubxity.plugins.metrics.common.metric.Metrics

class TickCollection(bootstrap: UnifiedMetricsBukkitBootstrap) : CollectorCollection {
private val reporter = if (classExists("com.destroystokyo.paper.event.server.ServerTickStartEvent")) {
Expand All @@ -34,7 +35,7 @@ class TickCollection(bootstrap: UnifiedMetricsBukkitBootstrap) : CollectorCollec

// The callback is called from a single thread
private val tickDuration = Histogram(
"minecraft_tick_duration_seconds",
Metrics.Server.TickDurationSeconds,
sumStoreFactory = VolatileDoubleStore,
countStoreFactory = VolatileLongStore
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
import dev.cubxity.plugins.metrics.api.metric.data.Metric
import dev.cubxity.plugins.metrics.api.util.fastForEach
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.common.metric.Metrics

class BukkitWorldCollector(private val bootstrap: UnifiedMetricsBukkitBootstrap) : Collector {
override fun collect(): List<Metric> {
Expand All @@ -30,9 +31,9 @@ class BukkitWorldCollector(private val bootstrap: UnifiedMetricsBukkitBootstrap)

worlds.fastForEach { world ->
val tags = mapOf("world" to world.name)
samples.add(GaugeMetric("minecraft_world_entities_count", tags, world.entities.size))
samples.add(GaugeMetric("minecraft_world_players_count", tags, world.players.size))
samples.add(GaugeMetric("minecraft_world_loaded_chunks", tags, world.loadedChunks.size))
samples.add(GaugeMetric(Metrics.Server.WorldEntitiesCount, tags, world.entities.size))
samples.add(GaugeMetric(Metrics.Server.WorldPlayersCount, tags, world.players.size))
samples.add(GaugeMetric(Metrics.Server.WorldLoadedChunks, tags, world.loadedChunks.size))
}

return samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
import dev.cubxity.plugins.metrics.api.metric.data.Metric
import dev.cubxity.plugins.metrics.api.util.fastForEach
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.common.metric.Metrics

class PaperWorldCollector(private val bootstrap: UnifiedMetricsBukkitBootstrap) : Collector {
override fun collect(): List<Metric> {
Expand All @@ -30,9 +31,9 @@ class PaperWorldCollector(private val bootstrap: UnifiedMetricsBukkitBootstrap)

worlds.fastForEach { world ->
val tags = mapOf("world" to world.name)
samples.add(GaugeMetric("minecraft_world_entities_count", tags, world.entityCount))
samples.add(GaugeMetric("minecraft_world_players_count", tags, world.playerCount))
samples.add(GaugeMetric("minecraft_world_loaded_chunks", tags, world.chunkCount))
samples.add(GaugeMetric(Metrics.Server.WorldEntitiesCount, tags, world.entityCount))
samples.add(GaugeMetric(Metrics.Server.WorldPlayersCount, tags, world.playerCount))
samples.add(GaugeMetric(Metrics.Server.WorldLoadedChunks, tags, world.chunkCount))
}

return samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

package dev.cubxity.plugins.metrics.minestom.metric.tick

import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
import dev.cubxity.plugins.metrics.api.metric.collector.Histogram
import dev.cubxity.plugins.metrics.api.metric.collector.MILLISECONDS_PER_SECOND
import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore
import dev.cubxity.plugins.metrics.api.metric.store.VolatileLongStore
import dev.cubxity.plugins.metrics.common.metric.Metrics
import net.minestom.server.MinecraftServer
import net.minestom.server.monitoring.TickMonitor
import java.util.function.Consumer

class TickCollection : Consumer<TickMonitor>, CollectorCollection {
// The callback is called from a single thread
private val tickDuration = Histogram(
"minecraft_tick_duration_seconds",
Metrics.Server.TickDurationSeconds,
sumStoreFactory = VolatileDoubleStore,
countStoreFactory = VolatileLongStore
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dev.cubxity.plugins.metrics.minestom.metric.world
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
import dev.cubxity.plugins.metrics.api.metric.data.Metric
import dev.cubxity.plugins.metrics.common.metric.Metrics
import net.minestom.server.MinecraftServer

class WorldCollector : Collector {
Expand All @@ -29,9 +30,9 @@ class WorldCollector : Collector {

for (instance in instances) {
val tags = mapOf("world" to instance.uniqueId.toString())
samples.add(GaugeMetric("minecraft_world_entities_count", tags, instance.entities.size))
samples.add(GaugeMetric("minecraft_world_players_count", tags, instance.players.size))
samples.add(GaugeMetric("minecraft_world_loaded_chunks", tags, instance.chunks.size))
samples.add(GaugeMetric(Metrics.Server.WorldEntitiesCount, tags, instance.entities.size))
samples.add(GaugeMetric(Metrics.Server.WorldPlayersCount, tags, instance.players.size))
samples.add(GaugeMetric(Metrics.Server.WorldLoadedChunks, tags, instance.chunks.size))
}

return samples
Expand Down

0 comments on commit ce39520

Please sign in to comment.