feat: added more metrics

This commit is contained in:
Andrey Terentev 2024-11-30 01:17:54 +07:00
parent 515c9886b0
commit 502ffbadb3
2 changed files with 233 additions and 327 deletions

View File

@ -1,9 +1,14 @@
package ru.dragonestia.msb3.api.module; package ru.dragonestia.msb3.api.module;
import io.prometheus.metrics.core.metrics.Counter;
import io.prometheus.metrics.core.metrics.GaugeWithCallback;
import io.prometheus.metrics.exporter.httpserver.HTTPServer; import io.prometheus.metrics.exporter.httpserver.HTTPServer;
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics; import io.prometheus.metrics.instrumentation.jvm.JvmMetrics;
import io.prometheus.metrics.instrumentation.jvm.JvmNativeMemoryMetrics;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.player.AsyncPlayerPreLoginEvent;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -28,16 +33,26 @@ public class PrometheusMetricsModule {
private static void run(InetSocketAddress address) throws Exception { private static void run(InetSocketAddress address) throws Exception {
JvmMetrics.builder().register(); JvmMetrics.builder().register();
JvmNativeMemoryMetrics.builder().register();
server = HTTPServer.builder() server = HTTPServer.builder()
.inetAddress(address.getAddress()) .inetAddress(address.getAddress())
.port(address.getPort()) .port(address.getPort())
.buildAndStart(); .buildAndStart();
registerDefaultMetrics(); registerDefaultMetrics();
} }
private static void registerDefaultMetrics() { private static void registerDefaultMetrics() {
GaugeWithCallback.builder()
.name("msb3_online_players")
.callback(c -> c.call(MinecraftServer.getConnectionManager().getOnlinePlayerCount()))
.register();
var joinedPlayers = Counter.builder().name("msb3_joined_players").register();
MinecraftServer.getGlobalEventHandler().addListener(AsyncPlayerPreLoginEvent.class, event -> {
joinedPlayers.inc();
});
} }
} }

View File

@ -96,7 +96,7 @@
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
"decimals": 1, "decimals": 0,
"mappings": [ "mappings": [
{ {
"options": { "options": {
@ -108,6 +108,7 @@
"type": "special" "type": "special"
} }
], ],
"min": 0,
"thresholds": { "thresholds": {
"mode": "absolute", "mode": "absolute",
"steps": [ "steps": [
@ -121,13 +122,13 @@
} }
] ]
}, },
"unit": "s" "unit": "none"
}, },
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 3, "h": 4,
"w": 6, "w": 2,
"x": 0, "x": 0,
"y": 1 "y": 1
}, },
@ -135,8 +136,8 @@
"maxDataPoints": 100, "maxDataPoints": 100,
"options": { "options": {
"colorMode": "value", "colorMode": "value",
"graphMode": "none", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "center",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard", "percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
@ -157,16 +158,96 @@
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "${DS_PROMETHEUS}"
}, },
"expr": "process_uptime_seconds{application=\"$application\", instance=\"$instance\"}", "disableTextWrap": false,
"editorMode": "builder",
"expr": "msb3_online_players{application=\"$application\", instance=\"$instance\"}",
"format": "time_series", "format": "time_series",
"fullMetaSearch": false,
"includeNullMetadata": true,
"intervalFactor": 2, "intervalFactor": 2,
"legendFormat": "", "legendFormat": "",
"metric": "", "metric": "",
"range": true,
"refId": "A", "refId": "A",
"step": 14400 "step": 14400,
"useBackend": false
} }
], ],
"title": "Uptime", "title": "Online players",
"type": "stat"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"decimals": 0,
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "yellow",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 4,
"w": 2,
"x": 2,
"y": 1
},
"id": 146,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "11.3.1",
"targets": [
{
"disableTextWrap": false,
"editorMode": "builder",
"expr": "msb3_joined_players_total",
"fullMetaSearch": false,
"includeNullMetadata": true,
"legendFormat": "__auto",
"range": true,
"refId": "A",
"useBackend": false,
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
}
}
],
"title": "Joined players",
"type": "stat" "type": "stat"
}, },
{ {
@ -200,12 +281,12 @@
} }
] ]
}, },
"unit": "dateTimeAsIso" "unit": "dateTimeAsLocal"
}, },
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 3, "h": 4,
"w": 6, "w": 6,
"x": 6, "x": 6,
"y": 1 "y": 1
@ -250,6 +331,83 @@
"title": "Start time", "title": "Start time",
"type": "stat" "type": "stat"
}, },
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"mappings": [
{
"options": {
"match": "null",
"result": {
"text": "N/A"
}
},
"type": "special"
}
],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "blue",
"value": null
}
]
},
"unit": "dateTimeFromNow"
},
"overrides": []
},
"gridPos": {
"h": 4,
"w": 6,
"x": 12,
"y": 1
},
"id": 147,
"maxDataPoints": 100,
"options": {
"colorMode": "value",
"graphMode": "none",
"justifyMode": "auto",
"orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showPercentChange": false,
"textMode": "auto",
"wideLayout": true
},
"pluginVersion": "11.3.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "process_start_time_seconds{application=\"$application\", instance=\"$instance\"}*1000",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "",
"metric": "",
"range": true,
"refId": "A",
"step": 14400
}
],
"title": "Started",
"type": "stat"
},
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
@ -293,9 +451,9 @@
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 3, "h": 4,
"w": 6, "w": 3,
"x": 12, "x": 18,
"y": 1 "y": 1
}, },
"id": 65, "id": 65,
@ -388,9 +546,9 @@
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 3, "h": 4,
"w": 6, "w": 3,
"x": 18, "x": 21,
"y": 1 "y": 1
}, },
"id": 75, "id": 75,
@ -438,7 +596,7 @@
"h": 1, "h": 1,
"w": 24, "w": 24,
"x": 0, "x": 0,
"y": 4 "y": 5
}, },
"id": 141, "id": 141,
"panels": [], "panels": [],
@ -508,10 +666,10 @@
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 7, "h": 9,
"w": 6, "w": 8,
"x": 0, "x": 0,
"y": 5 "y": 6
}, },
"id": 24, "id": 24,
"options": { "options": {
@ -520,8 +678,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -635,10 +793,10 @@
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 7, "h": 9,
"w": 6, "w": 8,
"x": 6, "x": 8,
"y": 5 "y": 6
}, },
"id": 25, "id": 25,
"options": { "options": {
@ -647,8 +805,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -763,10 +921,10 @@
"overrides": [] "overrides": []
}, },
"gridPos": { "gridPos": {
"h": 7, "h": 9,
"w": 6, "w": 8,
"x": 12, "x": 16,
"y": 5 "y": 6
}, },
"id": 26, "id": 26,
"options": { "options": {
@ -775,8 +933,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -827,286 +985,19 @@
"title": "JVM Total", "title": "JVM Total",
"type": "timeseries" "type": "timeseries"
}, },
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 10,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "bytes"
},
"overrides": []
},
"gridPos": {
"h": 7,
"w": 6,
"x": 18,
"y": 5
},
"id": 86,
"options": {
"legend": {
"calcs": [
"lastNotNull",
"max"
],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "multi",
"sort": "none"
}
},
"pluginVersion": "11.3.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "process_memory_vss_bytes{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "vss",
"metric": "",
"refId": "A",
"step": 2400
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "process_memory_rss_bytes{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "rss",
"refId": "B"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "process_memory_swap_bytes{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "swap",
"refId": "C"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "process_memory_rss_bytes{application=\"$application\", instance=\"$instance\"} + process_memory_swap_bytes{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "total",
"refId": "D"
}
],
"title": "JVM Process Memory",
"type": "timeseries"
},
{ {
"collapsed": false, "collapsed": false,
"gridPos": { "gridPos": {
"h": 1, "h": 1,
"w": 24, "w": 24,
"x": 0, "x": 0,
"y": 12 "y": 15
}, },
"id": 142, "id": 142,
"panels": [], "panels": [],
"title": "JVM Misc", "title": "JVM Misc",
"type": "row" "type": "row"
}, },
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 10,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"decimals": 1,
"mappings": [],
"max": 1,
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "percentunit"
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 6,
"x": 0,
"y": 13
},
"id": 106,
"options": {
"legend": {
"calcs": [
"lastNotNull",
"max"
],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "multi",
"sort": "none"
}
},
"pluginVersion": "11.3.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "system_cpu_usage{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"hide": false,
"intervalFactor": 1,
"legendFormat": "system",
"metric": "",
"refId": "A",
"step": 2400
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "process_cpu_usage{application=\"$application\", instance=\"$instance\"}",
"format": "time_series",
"hide": false,
"intervalFactor": 1,
"legendFormat": "process",
"refId": "B"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "avg_over_time(process_cpu_usage{application=\"$application\", instance=\"$instance\"}[15m])",
"format": "time_series",
"hide": false,
"intervalFactor": 1,
"legendFormat": "process-15m",
"refId": "C"
}
],
"title": "CPU Usage",
"type": "timeseries"
},
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
@ -1174,8 +1065,8 @@
"gridPos": { "gridPos": {
"h": 9, "h": 9,
"w": 6, "w": 6,
"x": 6, "x": 0,
"y": 13 "y": 16
}, },
"id": 93, "id": 93,
"options": { "options": {
@ -1277,9 +1168,9 @@
}, },
"gridPos": { "gridPos": {
"h": 9, "h": 9,
"w": 6, "w": 9,
"x": 12, "x": 6,
"y": 13 "y": 16
}, },
"id": 32, "id": 32,
"options": { "options": {
@ -1288,8 +1179,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -1539,9 +1430,9 @@
}, },
"gridPos": { "gridPos": {
"h": 9, "h": 9,
"w": 6, "w": 9,
"x": 18, "x": 15,
"y": 13 "y": 16
}, },
"id": 124, "id": 124,
"options": { "options": {
@ -1550,8 +1441,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -1584,7 +1475,7 @@
"h": 1, "h": 1,
"w": 24, "w": 24,
"x": 0, "x": 0,
"y": 22 "y": 25
}, },
"id": 143, "id": 143,
"panels": [], "panels": [],
@ -1658,7 +1549,7 @@
"h": 9, "h": 9,
"w": 12, "w": 12,
"x": 0, "x": 0,
"y": 23 "y": 26
}, },
"id": 3, "id": 3,
"maxPerRow": 3, "maxPerRow": 3,
@ -1668,8 +1559,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -1796,7 +1687,7 @@
"h": 9, "h": 9,
"w": 12, "w": 12,
"x": 12, "x": 12,
"y": 23 "y": 26
}, },
"id": 78, "id": 78,
"maxPerRow": 3, "maxPerRow": 3,
@ -1806,8 +1697,8 @@
"lastNotNull", "lastNotNull",
"max" "max"
], ],
"displayMode": "list", "displayMode": "table",
"placement": "bottom", "placement": "right",
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
@ -1874,7 +1765,7 @@
"h": 1, "h": 1,
"w": 24, "w": 24,
"x": 0, "x": 0,
"y": 32 "y": 35
}, },
"id": 145, "id": 145,
"panels": [], "panels": [],
@ -1947,7 +1838,7 @@
"h": 7, "h": 7,
"w": 8, "w": 8,
"x": 0, "x": 0,
"y": 33 "y": 36
}, },
"id": 98, "id": 98,
"options": { "options": {
@ -2046,7 +1937,7 @@
"h": 7, "h": 7,
"w": 8, "w": 8,
"x": 8, "x": 8,
"y": 33 "y": 36
}, },
"id": 101, "id": 101,
"options": { "options": {
@ -2159,7 +2050,7 @@
"h": 7, "h": 7,
"w": 8, "w": 8,
"x": 16, "x": 16,
"y": 33 "y": 36
}, },
"id": 99, "id": 99,
"options": { "options": {
@ -2301,6 +2192,6 @@
"timezone": "browser", "timezone": "browser",
"title": "JVM", "title": "JVM",
"uid": "ae5ew9zzh8cu8e", "uid": "ae5ew9zzh8cu8e",
"version": 13, "version": 20,
"weekStart": "" "weekStart": ""
} }