Skip to content

Commit

Permalink
Enhanced prometheus exporter to support both java and bedrock in one
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Mar 21, 2020
1 parent 13c87a0 commit e91a99d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ When using the `export-for-prometheus` subcommand, mc-monitor will serve a Prome

The sub-command accepts the following arguments, which can also be viewed using `--help`:
```
-edition string
The edition of Minecraft server, java or bedrock (env EXPORT_EDITION) (default "java")
-bedrock-servers host:port
one or more host:port addresses of Bedrock servers to monitor, when port is omitted 19132 is used (env EXPORT_BEDROCK_SERVERS)
-port int
HTTP port where Prometheus metrics are exported (env EXPORT_PORT) (default 8080)
-servers host:port
one or more host:port addresses of servers to monitor, when port is omitted 19132 is used (env EXPORT_SERVERS)
one or more host:port addresses of Java servers to monitor, when port is omitted 25565 is used (env EXPORT_SERVERS)
```

The following metrics are exported
Expand Down
16 changes: 6 additions & 10 deletions prom_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
const promExportPath = "/metrics"

type exportPrometheusCmd struct {
Servers []string `usage:"one or more [host:port] addresses of servers to monitor, when port is omitted 19132 is used"`
Port int `usage:"HTTP port where Prometheus metrics are exported" default:"8080"`
Edition string `usage:"The edition of Minecraft server, java or bedrock" default:"java"`
logger *zap.Logger
Servers []string `usage:"one or more [host:port] addresses of Java servers to monitor, when port is omitted 25565 is used"`
BedrockServers []string `usage:"one or more [host:port] addresses of Bedrock servers to monitor, when port is omitted 19132 is used"`
Port int `usage:"HTTP port where Prometheus metrics are exported" default:"8080"`
logger *zap.Logger
}

func (c *exportPrometheusCmd) Name() string {
Expand Down Expand Up @@ -47,14 +47,10 @@ func (c *exportPrometheusCmd) Execute(_ context.Context, _ *flag.FlagSet, args .
printUsageError("requires at least one server")
return subcommands.ExitUsageError
}
if !ValidEdition(c.Edition) {
printUsageError("invalid edition")
return subcommands.ExitUsageError
}

logger := args[0].(*zap.Logger)

collectors, err := newPromCollectors(c.Servers, ServerEdition(c.Edition), logger)
collectors, err := newPromCollectors(c.Servers, c.BedrockServers, logger)
if err != nil {
log.Fatal(err)
}
Expand All @@ -69,7 +65,7 @@ func (c *exportPrometheusCmd) Execute(_ context.Context, _ *flag.FlagSet, args .
logger.Info("exporting metrics for prometheus",
zap.String("address", exportAddress),
zap.String("path", promExportPath),
zap.Strings("servers", c.Servers))
)

http.Handle(promExportPath, promhttp.Handler())
log.Fatal(http.ListenAndServe(exportAddress, nil))
Expand Down
15 changes: 12 additions & 3 deletions prom_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@ func (c promCollectors) Collect(metrics chan<- prometheus.Metric) {
}
}

func newPromCollectors(servers []string, edition ServerEdition, logger *zap.Logger) (promCollectors, error) {
collectors, err := createPromCollectors(servers, edition, logger)
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
}
Expand Down Expand Up @@ -154,7 +163,7 @@ func (c *promBedrockCollector) Collect(metrics chan<- prometheus.Metric) {

func (c *promBedrockCollector) sendMetric(metrics chan<- prometheus.Metric, desc *prometheus.Desc, value float64) {
metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, value,
c.host, c.port, string(JavaEdition))
c.host, c.port, string(BedrockEdition))
if err != nil {
c.logger.Error("failed to build metric", zap.Error(err), zap.String("name", desc.String()))
} else {
Expand Down

0 comments on commit e91a99d

Please sign in to comment.