flags.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. gethmetrics "github.com/ethereum/go-ethereum/metrics"
  21. "github.com/ethereum/go-ethereum/metrics/influxdb"
  22. "github.com/ethereum/go-ethereum/swarm/log"
  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. MetricsEnableInfluxDBAccountingExportFlag = cli.BoolFlag{
  31. Name: "metrics.influxdb.accounting",
  32. Usage: "Enable accounting 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. // Flags holds all command-line flags required for metrics collection.
  65. var Flags = []cli.Flag{
  66. utils.MetricsEnabledFlag,
  67. MetricsEnableInfluxDBExportFlag,
  68. MetricsEnableInfluxDBAccountingExportFlag,
  69. MetricsInfluxDBEndpointFlag,
  70. MetricsInfluxDBDatabaseFlag,
  71. MetricsInfluxDBUsernameFlag,
  72. MetricsInfluxDBPasswordFlag,
  73. MetricsInfluxDBHostTagFlag,
  74. }
  75. func Setup(ctx *cli.Context) {
  76. if gethmetrics.Enabled {
  77. log.Info("Enabling swarm metrics collection")
  78. var (
  79. enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
  80. enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.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. // Start system runtime metrics collection
  88. go gethmetrics.CollectProcessMetrics(2 * time.Second)
  89. if enableExport {
  90. log.Info("Enabling swarm metrics export to InfluxDB")
  91. go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
  92. "host": hosttag,
  93. })
  94. }
  95. if enableAccountingExport {
  96. log.Info("Exporting accounting metrics to InfluxDB")
  97. go influxdb.InfluxDBWithTags(gethmetrics.AccountingRegistry, 10*time.Second, endpoint, database, username, password, "accounting.", map[string]string{
  98. "host": hosttag,
  99. })
  100. }
  101. }
  102. }