Skip to content

Commit

Permalink
Merge pull request #10 from Cubxity/dev
Browse files Browse the repository at this point in the history
Release 0.1.2
  • Loading branch information
Cubxity authored Feb 22, 2021
2 parents 4095e33 + e73ec04 commit b031d89
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 42 deletions.
Binary file modified .github/assets/grafana.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This project is licensed under [GNU AGPLv3](LICENSE)
- Velocity

## Features
- Server metrics collection (TPS, Players, Plugins)
- Server metrics collection (TPS, MSPT, Players, Plugins)
- World metrics collection (Entities, Chunks)
- Events metrics collection (Player flow, chat, pings)
- JVM metrics collection (Memory, CPU Load, Threads, Uptime)
Expand Down Expand Up @@ -43,18 +43,22 @@ val api = UnifiedMetricsProvider.get()
Currently, [InfluxDB](https://www.influxdata.com/) is required to collect metrics.
We recommend using an internal network for InfluxDB.

**config.toml**
```toml
[server]
server = "main"

[metrics]
enabled = true
driver = "influx"
```

[metrics.influx]
**driver/influx.toml**
```toml
[influx]
url = "http://influxdb:8086"
bucket = "unifiedmetrics"
influx = "influx"
username = "influx"
password = "influx"
interval = 10 # Interval in seconds
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plugins {
allprojects {
group = "dev.cubxity.plugins"
description = "Fully featured metrics collector agent for Minecraft servers."
version = "0.1.1"
version = "0.1.2"

repositories {
mavenCentral()
Expand Down
4 changes: 2 additions & 2 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import org.apache.tools.ant.filters.ReplaceTokens

repositories {
maven("https://hub.spigotmc.org/nexus/content/groups/public")
maven("https://papermc.io/repo/repository/maven-public")
}

dependencies {
api(project(":unifiedmetrics-core"))
compileOnly("org.spigotmc", "spigot-api", "1.8-R0.1-SNAPSHOT")
compileOnly("com.destroystokyo.paper", "paper-api", "1.16.5-R0.1-SNAPSHOT")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.bukkit.metric.EventsMetric
import dev.cubxity.plugins.metrics.bukkit.metric.ServerMetric
import dev.cubxity.plugins.metrics.bukkit.metric.TPSMetric
import dev.cubxity.plugins.metrics.bukkit.metric.tps.TPSMetric
import dev.cubxity.plugins.metrics.bukkit.metric.WorldMetric
import dev.cubxity.plugins.metrics.core.plugin.CoreUnifiedMetricsPlugin
import org.bukkit.Bukkit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.metric.tps

import dev.cubxity.plugins.metrics.bukkit.util.getNMSClass
import dev.cubxity.plugins.metrics.bukkit.util.getStaticMethodHandle

class NMSTPSProvider : TPSProvider {
private val minecraftServerClass = getNMSClass("MinecraftServer")
private val getServerMethod = minecraftServerClass.getStaticMethodHandle("getServer", minecraftServerClass)
private val recentTpsField = minecraftServerClass.getDeclaredField("recentTps")
private val recentTickTimes = minecraftServerClass.getDeclaredField("h")

override val tps: Double
get() {
val server = getServerMethod.invoke()
val recentTps = recentTpsField.get(server) as DoubleArray
return recentTps[0]
}
override val mspt: Long
get() {
val server = getServerMethod.invoke()
val recentMspt = recentTickTimes.get(server) as LongArray
return recentMspt[0]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.metric.tps

import org.bukkit.Bukkit

class PaperTPSProvider : TPSProvider {
override val tps: Double
get() = Bukkit.getServer().tps[0]
override val mspt: Long
get() = Bukkit.getServer().tickTimes[0]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,29 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.metric
package dev.cubxity.plugins.metrics.bukkit.metric.tps

import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.api.metric.Metric
import dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
import dev.cubxity.plugins.metrics.bukkit.util.Environment
import dev.cubxity.plugins.metrics.common.measurement.TPSMeasurement
import org.bukkit.Bukkit

class TPSMetric(private val bootstrap: UnifiedMetricsBukkitBootstrap) : Metric<TPSMeasurement>, Runnable {
private var taskId = -1
private var currentSec: Long = 0
private var ticks = 0
private var tps = 20
class TPSMetric(private val bootstrap: UnifiedMetricsBukkitBootstrap) : Metric<TPSMeasurement> {
private val provider = if (Environment.majorMinecraftVersion >= 15 && Environment.isPaper) {
PaperTPSProvider()
} else {
NMSTPSProvider()
}

override val isSync: Boolean
get() = false
get() = true

override fun initialize() {
taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(bootstrap, this, 0, 1)
bootstrap.logger.info("TPSMetric: Using ${provider.javaClass.name}")
}

override fun getMeasurements(api: UnifiedMetrics): List<TPSMeasurement> {
return listOf(TPSMeasurement(tps))
}

override fun dispose() {
Bukkit.getScheduler().cancelTask(taskId)
}

override fun run() {
val sec = System.currentTimeMillis() / 1000
if (currentSec == sec) {
ticks++
} else {
currentSec = sec
tps = ticks + 1
ticks = 0
}
return listOf(TPSMeasurement(provider.tps, provider.mspt))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.metric.tps

interface TPSProvider {
/**
* Ticks per second
*/
val tps: Double

/**
* Tick time in nanoseconds
*/
val mspt: Long
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.util

import org.bukkit.Bukkit

object Environment {
val isPaper: Boolean = classExists("com.destroystokyo.paper.PaperConfig")
val serverApiVersion: String = Bukkit.getServer().javaClass.`package`.name.split('.')[3]
val majorMinecraftVersion: Int = serverApiVersion.split('_')[1].toInt()

private fun classExists(name: String): Boolean {
try {
Class.forName(name)
return true
} catch (ignored: ClassNotFoundException) {
// Ignore
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.util

import java.lang.invoke.MethodHandle
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType

private val lookup = MethodHandles.publicLookup()

fun Class<*>.getStaticMethodHandle(name: String, returnType: Class<*>, vararg parameters: Class<*>): MethodHandle {
val type = MethodType.methodType(returnType, parameters)
return lookup.findStatic(this, name, type)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* UnifiedMetrics is a fully-featured metrics collection plugin for Minecraft servers.
* Copyright (C) 2021 Cubxity
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.bukkit.util

fun getNMSClass(name: String): Class<*> =
Class.forName("net.minecraft.server.${Environment.serverApiVersion}.$name")
1 change: 1 addition & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: UnifiedMetrics
main: dev.cubxity.plugins.metrics.bukkit.bootstrap.UnifiedMetricsBukkitBootstrap
description: "Fully-featured metrics plugin for Minecraft servers"
api-version: 1.16
author: Cubxity
version: @version@
load: STARTUP
Loading

0 comments on commit b031d89

Please sign in to comment.