github.com/susy-go/susy-graviton@v0.0.0-20190614130430-36cddae42305/swarm/metrics/flags.go (about)

     1  // Copyleft 2018 The susy-graviton Authors
     2  // This file is part of the susy-graviton library.
     3  //
     4  // The susy-graviton library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The susy-graviton library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MSRCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the susy-graviton library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/susy-go/susy-graviton/cmd/utils"
    23  	gravitonmetrics "github.com/susy-go/susy-graviton/metrics"
    24  	"github.com/susy-go/susy-graviton/metrics/influxdb"
    25  	"github.com/susy-go/susy-graviton/swarm/log"
    26  	cli "gopkg.in/urfave/cli.v1"
    27  )
    28  
    29  var (
    30  	MetricsEnableInfluxDBExportFlag = cli.BoolFlag{
    31  		Name:  "metrics.influxdb.export",
    32  		Usage: "Enable metrics export/push to an external InfluxDB database",
    33  	}
    34  	MetricsEnableInfluxDBAccountingExportFlag = cli.BoolFlag{
    35  		Name:  "metrics.influxdb.accounting",
    36  		Usage: "Enable accounting metrics export/push to an external InfluxDB database",
    37  	}
    38  	MetricsInfluxDBEndpointFlag = cli.StringFlag{
    39  		Name:  "metrics.influxdb.endpoint",
    40  		Usage: "Metrics InfluxDB endpoint",
    41  		Value: "http://127.0.0.1:8086",
    42  	}
    43  	MetricsInfluxDBDatabaseFlag = cli.StringFlag{
    44  		Name:  "metrics.influxdb.database",
    45  		Usage: "Metrics InfluxDB database",
    46  		Value: "metrics",
    47  	}
    48  	MetricsInfluxDBUsernameFlag = cli.StringFlag{
    49  		Name:  "metrics.influxdb.username",
    50  		Usage: "Metrics InfluxDB username",
    51  		Value: "",
    52  	}
    53  	MetricsInfluxDBPasswordFlag = cli.StringFlag{
    54  		Name:  "metrics.influxdb.password",
    55  		Usage: "Metrics InfluxDB password",
    56  		Value: "",
    57  	}
    58  	// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
    59  	// For example `host` tag could be used so that we can group all nodes and average a measurement
    60  	// across all of them, but also so that we can select a specific node and inspect its measurements.
    61  	// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
    62  	MetricsInfluxDBTagsFlag = cli.StringFlag{
    63  		Name:  "metrics.influxdb.tags",
    64  		Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
    65  		Value: "host=localhost",
    66  	}
    67  )
    68  
    69  // Flags holds all command-line flags required for metrics collection.
    70  var Flags = []cli.Flag{
    71  	utils.MetricsEnabledFlag,
    72  	MetricsEnableInfluxDBExportFlag,
    73  	MetricsEnableInfluxDBAccountingExportFlag,
    74  	MetricsInfluxDBEndpointFlag,
    75  	MetricsInfluxDBDatabaseFlag,
    76  	MetricsInfluxDBUsernameFlag,
    77  	MetricsInfluxDBPasswordFlag,
    78  	MetricsInfluxDBTagsFlag,
    79  }
    80  
    81  func Setup(ctx *cli.Context) {
    82  	if gravitonmetrics.Enabled {
    83  		log.Info("Enabling swarm metrics collection")
    84  		var (
    85  			endpoint               = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
    86  			database               = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
    87  			username               = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
    88  			password               = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
    89  			enableExport           = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
    90  			enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
    91  		)
    92  
    93  		// Start system runtime metrics collection
    94  		go gravitonmetrics.CollectProcessMetrics(2 * time.Second)
    95  
    96  		tagsMap := utils.SplitTagsFlag(ctx.GlobalString(MetricsInfluxDBTagsFlag.Name))
    97  
    98  		if enableExport {
    99  			log.Info("Enabling swarm metrics export to InfluxDB")
   100  			go influxdb.InfluxDBWithTags(gravitonmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", tagsMap)
   101  		}
   102  
   103  		if enableAccountingExport {
   104  			log.Info("Exporting swarm accounting metrics to InfluxDB")
   105  			go influxdb.InfluxDBWithTags(gravitonmetrics.AccountingRegistry, 10*time.Second, endpoint, database, username, password, "accounting.", tagsMap)
   106  		}
   107  	}
   108  }