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

View file

@ -1,6 +1,7 @@
package influxdb package influxdb
import ( import (
"errors"
"fmt" "fmt"
uurl "net/url" uurl "net/url"
"time" "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 // 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) u, err := uurl.Parse(url)
if err != nil { if err != nil {
log.Warn("Unable to parse InfluxDB", "url", url, "err", err) return errors.New(fmt.Sprintf("Unable to parse InfluxDB. url: %s, err: %v", url, err))
return
} }
rep := &reporter{ rep := &reporter{
@ -77,13 +77,14 @@ func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password,
cache: make(map[string]int64), cache: make(map[string]int64),
} }
if err := rep.makeClient(); err != nil { if err := rep.makeClient(); err != nil {
log.Warn("Unable to make InfluxDB client", "err", err) return errors.New(fmt.Sprintf("Unable to make InfluxDB client. err: %v", err))
return
} }
if err := rep.send(); err != nil { 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) { func (r *reporter) makeClient() (err error) {