address PR comments

This commit is contained in:
Elad Nachmias 2018-12-05 12:32:16 +05:30
parent 99a1c3095f
commit cb72c5764a
2 changed files with 10 additions and 14 deletions

View file

@ -141,12 +141,8 @@ func main() {
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Before = func(ctx *cli.Context) error {
return nil
}
app.After = func(ctx *cli.Context) error {
emitMetrics(ctx)
return nil
return emitMetrics(ctx)
}
err := app.Run(os.Args)
@ -157,7 +153,7 @@ func main() {
}
}
func emitMetrics(ctx *cli.Context) {
func emitMetrics(ctx *cli.Context) error {
if gethmetrics.Enabled {
var (
endpoint = ctx.GlobalString(swarmmetrics.MetricsInfluxDBEndpointFlag.Name)
@ -166,8 +162,7 @@ func emitMetrics(ctx *cli.Context) {
password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
hosttag = ctx.GlobalString(swarmmetrics.MetricsInfluxDBHostTagFlag.Name)
)
influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
"host": hosttag,
"version": gitCommit,
"filesize": fmt.Sprintf("%v", filesize),

View file

@ -1,6 +1,7 @@
package influxdb
import (
"errors"
"fmt"
uurl "net/url"
"time"
@ -59,11 +60,10 @@ func InfluxDBWithTags(r metrics.Registry, d time.Duration, url, database, userna
}
// InfluxDBWithTagsOnce runs once an InfluxDB reporter and post the given metrics.Registry with the specified tags
func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) {
func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error {
u, err := uurl.Parse(url)
if err != nil {
log.Warn("Unable to parse InfluxDB", "url", url, "err", err)
return
return errors.New(fmt.Sprintf("Unable to parse InfluxDB. url: %s, err: %v", url, err))
}
rep := &reporter{
@ -77,13 +77,14 @@ func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password,
cache: make(map[string]int64),
}
if err := rep.makeClient(); err != nil {
log.Warn("Unable to make InfluxDB client", "err", err)
return
return errors.New(fmt.Sprintf("Unable to make InfluxDB client. err: %v", err))
}
if err := rep.send(); err != nil {
log.Warn("Unable to send to InfluxDB", "err", err)
return errors.New(fmt.Sprintf("Unable to send to InfluxDB. err: %v", err))
}
return nil
}
func (r *reporter) makeClient() (err error) {