github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/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/ShyftNetwork/go-empyrean/cmd/utils" 23 gethmetrics "github.com/ShyftNetwork/go-empyrean/metrics" 24 "github.com/ShyftNetwork/go-empyrean/metrics/influxdb" 25 "github.com/ShyftNetwork/go-empyrean/swarm/log" 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, 70 MetricsInfluxDBDatabaseFlag, 71 MetricsInfluxDBUsernameFlag, 72 MetricsInfluxDBPasswordFlag, 73 MetricsInfluxDBHostTagFlag, 74 } 75 76 func Setup(ctx *cli.Context) { 77 if gethmetrics.Enabled { 78 log.Info("Enabling swarm metrics collection") 79 var ( 80 enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name) 81 endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name) 82 database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name) 83 username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name) 84 password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name) 85 hosttag = ctx.GlobalString(MetricsInfluxDBHostTagFlag.Name) 86 ) 87 88 // Start system runtime metrics collection 89 go gethmetrics.CollectProcessMetrics(2 * time.Second) 90 91 if enableExport { 92 log.Info("Enabling swarm metrics export to InfluxDB") 93 go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{ 94 "host": hosttag, 95 }) 96 } 97 } 98 }