From 4cdb398080e184f528a7861e0a62833fffa1c078 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 26 Jul 2017 13:01:15 +0200 Subject: [PATCH] Use stathat instead of prometheus Signed-off-by: Knut Ahlers --- main.go | 36 ++++++++++++++++++------------------ metrics.go | 19 ------------------- 2 files changed, 18 insertions(+), 37 deletions(-) delete mode 100644 metrics.go diff --git a/main.go b/main.go index 6e51c93..8dafa4c 100644 --- a/main.go +++ b/main.go @@ -2,21 +2,26 @@ package main import ( "fmt" - "net/http" "os" "time" "github.com/Luzifer/rconfig" - "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/sirupsen/logrus" + stathat "github.com/stathat/go" +) + +const ( + metricPing = "[CS] Ping" + metricThresholdRX = "[CS] Threshold RX" + metricThresholdTX = "[CS] Threshold TX" ) var ( cfg struct { Hostname string `flag:"hostname" default:"" description:"Hostname / IP of the sparkyfish server" validate:"nonzero"` Interval time.Duration `flag:"interval" default:"15m" description:"Interval to execute test in"` - Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on"` LogLevel string `flag:"log-level" default:"info" description:"Set log level (debug, info, warning, error)"` + StatHatEZKey string `flag:"stathat-ezkey" default:"" description:"Key to post metrics to" validate:"nonzero"` Port int `flag:"port" default:"7121" description:"Port the sparkyfish server is running on"` VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"` } @@ -42,21 +47,16 @@ func init() { } func main() { - go func() { + if err := updateStats(execTest()); err != nil { + log.Error(err.Error()) + } + + for range time.Tick(cfg.Interval) { if err := updateStats(execTest()); err != nil { log.Error(err.Error()) + continue } - - for range time.Tick(cfg.Interval) { - if err := updateStats(execTest()); err != nil { - log.Error(err.Error()) - continue - } - } - }() - - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(cfg.Listen, nil) + } } func updateStats(t *testResult, err error) error { @@ -64,9 +64,9 @@ func updateStats(t *testResult, err error) error { return err } - pingAvg.Set(t.Ping.Avg) - thresholdAvg.WithLabelValues("recv").Set(t.Receive.Avg) - thresholdAvg.WithLabelValues("send").Set(t.Send.Avg) + stathat.PostEZValue(metricPing, cfg.StatHatEZKey, t.Ping.Avg) + stathat.PostEZValue(metricThresholdRX, cfg.StatHatEZKey, t.Receive.Avg) + stathat.PostEZValue(metricThresholdTX, cfg.StatHatEZKey, t.Send.Avg) return nil } diff --git a/metrics.go b/metrics.go deleted file mode 100644 index ceca6e0..0000000 --- a/metrics.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import "github.com/prometheus/client_golang/prometheus" - -var ( - pingAvg = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "sparkyfish_ping_avg", - Help: "Average ping of the test run (ms)", - }) - thresholdAvg = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "sparkyfish_threshold_avg", - Help: "Average threshold of the test run (bps)", - }, []string{"direction"}) -) - -func init() { - prometheus.MustRegister(pingAvg) - prometheus.MustRegister(thresholdAvg) -}