flags.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "os"
  19. "path/filepath"
  20. "time"
  21. "github.com/ethereum/go-ethereum/cmd/utils"
  22. "github.com/ethereum/go-ethereum/metrics"
  23. gethmetrics "github.com/ethereum/go-ethereum/metrics"
  24. "github.com/ethereum/go-ethereum/metrics/influxdb"
  25. "github.com/ethereum/go-ethereum/swarm/log"
  26. cli "gopkg.in/urfave/cli.v1"
  27. )
  28. var (
  29. MetricsEnableInfluxDBExportFlag = cli.BoolFlag{
  30. Name: "metrics.influxdb.export",
  31. Usage: "Enable metrics export/push to an external InfluxDB database",
  32. }
  33. MetricsEnableInfluxDBAccountingExportFlag = cli.BoolFlag{
  34. Name: "metrics.influxdb.accounting",
  35. Usage: "Enable accounting metrics export/push to an external InfluxDB database",
  36. }
  37. MetricsInfluxDBEndpointFlag = cli.StringFlag{
  38. Name: "metrics.influxdb.endpoint",
  39. Usage: "Metrics InfluxDB endpoint",
  40. Value: "http://127.0.0.1:8086",
  41. }
  42. MetricsInfluxDBDatabaseFlag = cli.StringFlag{
  43. Name: "metrics.influxdb.database",
  44. Usage: "Metrics InfluxDB database",
  45. Value: "metrics",
  46. }
  47. MetricsInfluxDBUsernameFlag = cli.StringFlag{
  48. Name: "metrics.influxdb.username",
  49. Usage: "Metrics InfluxDB username",
  50. Value: "",
  51. }
  52. MetricsInfluxDBPasswordFlag = cli.StringFlag{
  53. Name: "metrics.influxdb.password",
  54. Usage: "Metrics InfluxDB password",
  55. Value: "",
  56. }
  57. // Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
  58. // For example `host` tag could be used so that we can group all nodes and average a measurement
  59. // across all of them, but also so that we can select a specific node and inspect its measurements.
  60. // https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
  61. MetricsInfluxDBTagsFlag = cli.StringFlag{
  62. Name: "metrics.influxdb.tags",
  63. Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
  64. Value: "host=localhost",
  65. }
  66. )
  67. // Flags holds all command-line flags required for metrics collection.
  68. var Flags = []cli.Flag{
  69. utils.MetricsEnabledFlag,
  70. MetricsEnableInfluxDBExportFlag,
  71. MetricsEnableInfluxDBAccountingExportFlag,
  72. MetricsInfluxDBEndpointFlag,
  73. MetricsInfluxDBDatabaseFlag,
  74. MetricsInfluxDBUsernameFlag,
  75. MetricsInfluxDBPasswordFlag,
  76. MetricsInfluxDBTagsFlag,
  77. }
  78. func Setup(ctx *cli.Context) {
  79. if gethmetrics.Enabled {
  80. log.Info("Enabling swarm metrics collection")
  81. var (
  82. endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
  83. database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
  84. username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
  85. password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
  86. enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
  87. enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
  88. datadir = ctx.GlobalString("datadir")
  89. )
  90. // Start system runtime metrics collection
  91. go gethmetrics.CollectProcessMetrics(4 * time.Second)
  92. // Start collecting disk metrics
  93. go datadirDiskUsage(datadir, 4*time.Second)
  94. gethmetrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
  95. go gethmetrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, 4*time.Second)
  96. tagsMap := utils.SplitTagsFlag(ctx.GlobalString(MetricsInfluxDBTagsFlag.Name))
  97. if enableExport {
  98. log.Info("Enabling swarm metrics export to InfluxDB")
  99. go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", tagsMap)
  100. }
  101. if enableAccountingExport {
  102. log.Info("Exporting swarm accounting metrics to InfluxDB")
  103. go influxdb.InfluxDBWithTags(gethmetrics.AccountingRegistry, 10*time.Second, endpoint, database, username, password, "accounting.", tagsMap)
  104. }
  105. }
  106. }
  107. func datadirDiskUsage(path string, d time.Duration) {
  108. for range time.Tick(d) {
  109. bytes, err := dirSize(path)
  110. if err != nil {
  111. log.Warn("cannot get disk space", "err", err)
  112. }
  113. metrics.GetOrRegisterGauge("datadir.usage", nil).Update(bytes)
  114. }
  115. }
  116. func dirSize(path string) (int64, error) {
  117. var size int64
  118. err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
  119. if err != nil {
  120. return err
  121. }
  122. if !info.IsDir() {
  123. size += info.Size()
  124. }
  125. return err
  126. })
  127. return size, err
  128. }