github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/metrics/flags.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package metrics
    13  
    14  import (
    15  	"time"
    16  
    17  	"github.com/Sberex/go-sberex/cmd/utils"
    18  	"github.com/Sberex/go-sberex/log"
    19  	gethmetrics "github.com/Sberex/go-sberex/metrics"
    20  	"github.com/Sberex/go-sberex/metrics/influxdb"
    21  	"gopkg.in/urfave/cli.v1"
    22  )
    23  
    24  var (
    25  	metricsEnableInfluxDBExportFlag = cli.BoolFlag{
    26  		Name:  "metrics.influxdb.export",
    27  		Usage: "Enable metrics export/push to an external InfluxDB database",
    28  	}
    29  	metricsInfluxDBEndpointFlag = cli.StringFlag{
    30  		Name:  "metrics.influxdb.endpoint",
    31  		Usage: "Metrics InfluxDB endpoint",
    32  		Value: "http://127.0.0.1:8086",
    33  	}
    34  	metricsInfluxDBDatabaseFlag = cli.StringFlag{
    35  		Name:  "metrics.influxdb.database",
    36  		Usage: "Metrics InfluxDB database",
    37  		Value: "metrics",
    38  	}
    39  	metricsInfluxDBUsernameFlag = cli.StringFlag{
    40  		Name:  "metrics.influxdb.username",
    41  		Usage: "Metrics InfluxDB username",
    42  		Value: "",
    43  	}
    44  	metricsInfluxDBPasswordFlag = cli.StringFlag{
    45  		Name:  "metrics.influxdb.password",
    46  		Usage: "Metrics InfluxDB password",
    47  		Value: "",
    48  	}
    49  	// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
    50  	// It is used so that we can group all nodes and average a measurement across all of them, but also so
    51  	// that we can select a specific node and inspect its measurements.
    52  	// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
    53  	metricsInfluxDBHostTagFlag = cli.StringFlag{
    54  		Name:  "metrics.influxdb.host.tag",
    55  		Usage: "Metrics InfluxDB `host` tag attached to all measurements",
    56  		Value: "localhost",
    57  	}
    58  )
    59  
    60  // Flags holds all command-line flags required for metrics collection.
    61  var Flags = []cli.Flag{
    62  	utils.MetricsEnabledFlag,
    63  	metricsEnableInfluxDBExportFlag,
    64  	metricsInfluxDBEndpointFlag, metricsInfluxDBDatabaseFlag, metricsInfluxDBUsernameFlag, metricsInfluxDBPasswordFlag, metricsInfluxDBHostTagFlag,
    65  }
    66  
    67  func Setup(ctx *cli.Context) {
    68  	if gethmetrics.Enabled {
    69  		log.Info("Enabling swarm metrics collection")
    70  		var (
    71  			enableExport = ctx.GlobalBool(metricsEnableInfluxDBExportFlag.Name)
    72  			endpoint     = ctx.GlobalString(metricsInfluxDBEndpointFlag.Name)
    73  			database     = ctx.GlobalString(metricsInfluxDBDatabaseFlag.Name)
    74  			username     = ctx.GlobalString(metricsInfluxDBUsernameFlag.Name)
    75  			password     = ctx.GlobalString(metricsInfluxDBPasswordFlag.Name)
    76  			hosttag      = ctx.GlobalString(metricsInfluxDBHostTagFlag.Name)
    77  		)
    78  
    79  		if enableExport {
    80  			log.Info("Enabling swarm metrics export to InfluxDB")
    81  			go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
    82  				"host": hosttag,
    83  			})
    84  		}
    85  	}
    86  }