github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/swarm/metrics/flags.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/cmd/utils"
    23  	"github.com/ethereum/go-ethereum/log"
    24  	gethmetrics "github.com/ethereum/go-ethereum/metrics"
    25  	"github.com/ethereum/go-ethereum/metrics/influxdb"
    26  	"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  	metricsInfluxDBEndpointFlag = cli.StringFlag{
    35  		Name:  "metrics.influxdb.endpoint",
    36  		Usage: "Metrics InfluxDB endpoint",
    37  		Value: "http://127.0.0.1:8086",
    38  	}
    39  	metricsInfluxDBDatabaseFlag = cli.StringFlag{
    40  		Name:  "metrics.influxdb.database",
    41  		Usage: "Metrics InfluxDB database",
    42  		Value: "metrics",
    43  	}
    44  	metricsInfluxDBUsernameFlag = cli.StringFlag{
    45  		Name:  "metrics.influxdb.username",
    46  		Usage: "Metrics InfluxDB username",
    47  		Value: "",
    48  	}
    49  	metricsInfluxDBPasswordFlag = cli.StringFlag{
    50  		Name:  "metrics.influxdb.password",
    51  		Usage: "Metrics InfluxDB password",
    52  		Value: "",
    53  	}
    54  	// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
    55  	// It is used so that we can group all nodes and average a measurement across all of them, but also so
    56  	// that we can select a specific node and inspect its measurements.
    57  	// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
    58  	metricsInfluxDBHostTagFlag = cli.StringFlag{
    59  		Name:  "metrics.influxdb.host.tag",
    60  		Usage: "Metrics InfluxDB `host` tag attached to all measurements",
    61  		Value: "localhost",
    62  	}
    63  )
    64  
    65  // Flags holds all command-line flags required for metrics collection.
    66  var Flags = []cli.Flag{
    67  	utils.MetricsEnabledFlag,
    68  	metricsEnableInfluxDBExportFlag,
    69  	metricsInfluxDBEndpointFlag, metricsInfluxDBDatabaseFlag, metricsInfluxDBUsernameFlag, metricsInfluxDBPasswordFlag, metricsInfluxDBHostTagFlag,
    70  }
    71  
    72  func Setup(ctx *cli.Context) {
    73  	if gethmetrics.Enabled {
    74  		log.Info("Enabling swarm metrics collection")
    75  		var (
    76  			enableExport = ctx.GlobalBool(metricsEnableInfluxDBExportFlag.Name)
    77  			endpoint     = ctx.GlobalString(metricsInfluxDBEndpointFlag.Name)
    78  			database     = ctx.GlobalString(metricsInfluxDBDatabaseFlag.Name)
    79  			username     = ctx.GlobalString(metricsInfluxDBUsernameFlag.Name)
    80  			password     = ctx.GlobalString(metricsInfluxDBPasswordFlag.Name)
    81  			hosttag      = ctx.GlobalString(metricsInfluxDBHostTagFlag.Name)
    82  		)
    83  
    84  		if enableExport {
    85  			log.Info("Enabling swarm metrics export to InfluxDB")
    86  			go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
    87  				"host": hosttag,
    88  			})
    89  		}
    90  	}
    91  }