Skip to content

Commit

Permalink
Added detection of server in startup phase
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Jan 26, 2020
1 parent d790b80 commit 45e6077
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func (c *statusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interf
return subcommands.ExitFailure
}

// While server is starting up it will answer pings, but respond with empty JSON object.
// As such, we'll sanity check the max players value to see if a zero-value has been
// provided for info.
if info.Players.Max == 0 {
_, _ = fmt.Fprintf(os.Stderr, "server not ready %s:%d", c.Host, c.Port)
return subcommands.ExitFailure
}

fmt.Printf("%s:%d : version=%s online=%d max=%d motd='%s'",
c.Host, c.Port,
info.Version.Name, info.Players.Online, info.Players.Max, info.Description.Text)
Expand Down
12 changes: 6 additions & 6 deletions telegraf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
mcpinger "github.com/Raqbit/mc-pinger"
lpsender "github.com/itzg/line-protocol-sender"
"go.uber.org/zap"
Expand Down Expand Up @@ -34,10 +35,9 @@ func (g *TelegrafGatherer) Gather() {
elapsed := time.Now().Sub(startTime)

if err != nil {
err := g.sendFailedMetrics(err, elapsed)
if err != nil {
log.Printf("failed to send metrics: %s", err)
}
g.sendFailedMetrics(err, elapsed)
} else if info.Players.Max == 0 {
g.sendFailedMetrics(errors.New("server not ready"), elapsed)
} else {
err := g.sendInfoMetrics(info, elapsed)
if err != nil {
Expand All @@ -62,7 +62,7 @@ func (g *TelegrafGatherer) sendInfoMetrics(info *mcpinger.ServerInfo, elapsed ti
return nil
}

func (g *TelegrafGatherer) sendFailedMetrics(err error, elapsed time.Duration) error {
func (g *TelegrafGatherer) sendFailedMetrics(err error, elapsed time.Duration) {
m := lpsender.NewSimpleMetric(MetricName)

m.AddTag(TagHost, g.host)
Expand All @@ -74,5 +74,5 @@ func (g *TelegrafGatherer) sendFailedMetrics(err error, elapsed time.Duration) e

g.lpClient.Send(m)

return nil
return
}

0 comments on commit 45e6077

Please sign in to comment.