-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
prom_collector.go
226 lines (189 loc) · 6.54 KB
/
prom_collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
mcpinger "github.com/Raqbit/mc-pinger"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"net"
"strconv"
"time"
)
const (
promLabelHost = "server_host"
promLabelPort = "server_port"
promLabelEdition = "server_edition"
promLabelVersion = "server_version"
)
var (
promVariableLabels = []string{promLabelHost, promLabelPort, promLabelEdition, promLabelVersion}
promDescHealthy = prometheus.NewDesc("minecraft_status_healthy",
"Indicates if the server is healthy (1) or not (0)",
promVariableLabels, nil)
promDescResponseTime = prometheus.NewDesc("minecraft_status_response_time_seconds",
"Amount of time it took for server to respond",
promVariableLabels, nil)
promDescPlayersOnline = prometheus.NewDesc("minecraft_status_players_online_count",
"Number of players currently online",
promVariableLabels, nil)
promDescPlayersMax = prometheus.NewDesc("minecraft_status_players_max_count",
"Maximum number of players allowed by the server",
promVariableLabels, nil)
)
type pingOptions interface {
GetHost() string
GetPort() uint16
GetTimeout() time.Duration
}
func pingJavaServer(opt pingOptions) (*mcpinger.ServerInfo, error) {
var opts []mcpinger.McPingerOption
if t := opt.GetTimeout(); t > 0 {
opts = append(opts, mcpinger.WithTimeout(t))
}
pinger := mcpinger.New(opt.GetHost(), opt.GetPort(), opts...)
return pinger.Ping()
}
type specificPromCollector interface {
Collect(metrics chan<- prometheus.Metric)
SetTimeout(t time.Duration)
}
type promCollectors []specificPromCollector
func (promCollectors) Describe(descs chan<- *prometheus.Desc) {
descs <- promDescHealthy
descs <- promDescResponseTime
descs <- promDescPlayersOnline
descs <- promDescPlayersMax
}
func (c promCollectors) Collect(metrics chan<- prometheus.Metric) {
for _, entry := range c {
entry.Collect(metrics)
}
}
func newPromCollectors(servers []string, bedrockServers []string, logger *zap.Logger) (promCollectors, error) {
var collectors []specificPromCollector
javaCollectors, err := createPromCollectors(servers, JavaEdition, logger)
if err != nil {
return nil, err
}
collectors = append(collectors, javaCollectors...)
bedrockCollectors, err := createPromCollectors(bedrockServers, BedrockEdition, logger)
if err != nil {
return nil, err
}
collectors = append(collectors, bedrockCollectors...)
return collectors, nil
}
func createPromCollectors(servers []string, edition ServerEdition, logger *zap.Logger) (collectors []specificPromCollector, err error) {
for _, server := range servers {
switch edition {
case JavaEdition:
host, port, err := SplitHostPort(server, DefaultJavaPort)
if err != nil {
return nil, fmt.Errorf("failed to process server entry '%s': %w", server, err)
}
collectors = append(collectors, newPromJavaCollector(host, port, logger))
case BedrockEdition:
host, port, err := SplitHostPort(server, DefaultBedrockPort)
if err != nil {
return nil, fmt.Errorf("failed to process server entry '%s': %w", server, err)
}
collectors = append(collectors, newPromBedrockCollector(host, port, logger))
}
}
return
}
func newPromJavaCollector(host string, port uint16, logger *zap.Logger) specificPromCollector {
return &promJavaCollector{
host: host,
port: port,
logger: logger,
}
}
type promJavaCollector struct {
host string
port uint16
logger *zap.Logger
timeout time.Duration
}
func (c *promJavaCollector) GetHost() string {
return c.host
}
func (c *promJavaCollector) GetPort() uint16 {
return c.port
}
func (c *promJavaCollector) GetTimeout() time.Duration {
return c.timeout
}
func (c *promJavaCollector) SetTimeout(t time.Duration) {
c.timeout = t
}
func (c *promJavaCollector) Collect(metrics chan<- prometheus.Metric) {
c.logger.Debug("pinging", zap.String("host", c.host), zap.String("port", strconv.Itoa(int(c.port))))
startTime := time.Now()
info, err := pingJavaServer(c)
elapsed := time.Now().Sub(startTime)
if err != nil {
c.sendMetric(metrics, promDescHealthy, "", 0)
} else {
c.sendMetric(metrics, promDescResponseTime, info.Version.Name, elapsed.Seconds())
if info.Players.Max == 0 { // when server responds to ping but is not fully ready
c.sendMetric(metrics, promDescHealthy, info.Version.Name, 0)
} else {
c.sendMetric(metrics, promDescHealthy, info.Version.Name, 1)
c.sendMetric(metrics, promDescPlayersOnline, info.Version.Name, float64(info.Players.Online))
c.sendMetric(metrics, promDescPlayersMax, info.Version.Name, float64(info.Players.Max))
}
}
}
func (c *promJavaCollector) sendMetric(metrics chan<- prometheus.Metric, desc *prometheus.Desc,
version string, value float64) {
metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, value,
c.host, strconv.Itoa(int(c.port)), string(JavaEdition), version)
if err != nil {
c.logger.Error("failed to build metric", zap.Error(err), zap.String("name", desc.String()))
} else {
metrics <- metric
}
}
type promBedrockCollector struct {
host string
port uint16
logger *zap.Logger
timeout time.Duration
}
func (c *promBedrockCollector) GetHost() string {
return c.host
}
func (c *promBedrockCollector) GetPort() uint16 {
return c.port
}
func (c *promBedrockCollector) GetTimeout() time.Duration {
return c.timeout
}
func (c *promBedrockCollector) SetTimeout(t time.Duration) {
c.timeout = t
}
func newPromBedrockCollector(host string, port uint16, logger *zap.Logger) *promBedrockCollector {
return &promBedrockCollector{host: host, port: port, logger: logger}
}
func (c *promBedrockCollector) Collect(metrics chan<- prometheus.Metric) {
c.logger.Debug("pinging", zap.String("host", c.host), zap.String("port", strconv.Itoa(int(c.port))))
info, err := PingBedrockServer(net.JoinHostPort(c.host, strconv.Itoa(int(c.port))), c.timeout)
if err != nil {
c.sendMetric(metrics, promDescHealthy, "", 0)
} else {
c.sendMetric(metrics, promDescResponseTime, info.Version, info.Rtt.Seconds())
c.sendMetric(metrics, promDescHealthy, info.Version, 1)
c.sendMetric(metrics, promDescPlayersOnline, info.Version, float64(info.Players))
c.sendMetric(metrics, promDescPlayersMax, info.Version, float64(info.MaxPlayers))
}
}
func (c *promBedrockCollector) sendMetric(metrics chan<- prometheus.Metric,
desc *prometheus.Desc, version string, value float64) {
metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, value,
c.host, strconv.Itoa(int(c.port)), string(BedrockEdition), version)
if err != nil {
c.logger.Error("failed to build metric", zap.Error(err), zap.String("name", desc.String()))
} else {
metrics <- metric
}
}