flags.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package metrics
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/cmd/utils"
  20. "github.com/ethereum/go-ethereum/log"
  21. gethmetrics "github.com/ethereum/go-ethereum/metrics"
  22. "github.com/ethereum/go-ethereum/metrics/influxdb"
  23. "gopkg.in/urfave/cli.v1"
  24. )
  25. var (
  26. metricsEnableInfluxDBExportFlag = cli.BoolFlag{
  27. Name: "metrics.influxdb.export",
  28. Usage: "Enable metrics export/push to an external InfluxDB database",
  29. }
  30. metricsInfluxDBEndpointFlag = cli.StringFlag{
  31. Name: "metrics.influxdb.endpoint",
  32. Usage: "Metrics InfluxDB endpoint",
  33. Value: "http://127.0.0.1:8086",
  34. }
  35. metricsInfluxDBDatabaseFlag = cli.StringFlag{
  36. Name: "metrics.influxdb.database",
  37. Usage: "Metrics InfluxDB database",
  38. Value: "metrics",
  39. }
  40. metricsInfluxDBUsernameFlag = cli.StringFlag{
  41. Name: "metrics.influxdb.username",
  42. Usage: "Metrics InfluxDB username",
  43. Value: "",
  44. }
  45. metricsInfluxDBPasswordFlag = cli.StringFlag{
  46. Name: "metrics.influxdb.password",
  47. Usage: "Metrics InfluxDB password",
  48. Value: "",
  49. }
  50. // The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
  51. // It is used so that we can group all nodes and average a measurement across all of them, but also so
  52. // that we can select a specific node and inspect its measurements.
  53. // https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
  54. metricsInfluxDBHostTagFlag = cli.StringFlag{
  55. Name: "metrics.influxdb.host.tag",
  56. Usage: "Metrics InfluxDB `host` tag attached to all measurements",
  57. Value: "localhost",
  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. func Setup(ctx *cli.Context) {
  67. if gethmetrics.Enabled {
  68. log.Info("Enabling swarm metrics collection")
  69. var (
  70. enableExport = ctx.GlobalBool(metricsEnableInfluxDBExportFlag.Name)
  71. endpoint = ctx.GlobalString(metricsInfluxDBEndpointFlag.Name)
  72. database = ctx.GlobalString(metricsInfluxDBDatabaseFlag.Name)
  73. username = ctx.GlobalString(metricsInfluxDBUsernameFlag.Name)
  74. password = ctx.GlobalString(metricsInfluxDBPasswordFlag.Name)
  75. hosttag = ctx.GlobalString(metricsInfluxDBHostTagFlag.Name)
  76. )
  77. if enableExport {
  78. log.Info("Enabling swarm metrics export to InfluxDB")
  79. go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
  80. "host": hosttag,
  81. })
  82. }
  83. }
  84. }