You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package stats
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/prometheus/client_golang/prometheus/push"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. )
  11. var (
  12. FilerGather = prometheus.NewRegistry()
  13. VolumeServerGather = prometheus.NewRegistry()
  14. FilerRequestCounter = prometheus.NewCounterVec(
  15. prometheus.CounterOpts{
  16. Namespace: "SeaweedFS",
  17. Subsystem: "filer",
  18. Name: "request_total",
  19. Help: "Counter of filer requests.",
  20. }, []string{"type"})
  21. FilerRequestHistogram = prometheus.NewHistogramVec(
  22. prometheus.HistogramOpts{
  23. Namespace: "SeaweedFS",
  24. Subsystem: "filer",
  25. Name: "request_seconds",
  26. Help: "Bucketed histogram of filer request processing time.",
  27. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  28. }, []string{"type"})
  29. FilerStoreCounter = prometheus.NewCounterVec(
  30. prometheus.CounterOpts{
  31. Namespace: "SeaweedFS",
  32. Subsystem: "filerStore",
  33. Name: "request_total",
  34. Help: "Counter of filer store requests.",
  35. }, []string{"store", "type"})
  36. FilerStoreHistogram = prometheus.NewHistogramVec(
  37. prometheus.HistogramOpts{
  38. Namespace: "SeaweedFS",
  39. Subsystem: "filerStore",
  40. Name: "request_seconds",
  41. Help: "Bucketed histogram of filer store request processing time.",
  42. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  43. }, []string{"store", "type"})
  44. VolumeServerRequestCounter = prometheus.NewCounterVec(
  45. prometheus.CounterOpts{
  46. Namespace: "SeaweedFS",
  47. Subsystem: "volumeServer",
  48. Name: "request_total",
  49. Help: "Counter of volume server requests.",
  50. }, []string{"type"})
  51. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  52. prometheus.HistogramOpts{
  53. Namespace: "SeaweedFS",
  54. Subsystem: "volumeServer",
  55. Name: "request_seconds",
  56. Help: "Bucketed histogram of volume server request processing time.",
  57. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  58. }, []string{"type"})
  59. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  60. prometheus.GaugeOpts{
  61. Namespace: "SeaweedFS",
  62. Subsystem: "volumeServer",
  63. Name: "volumes",
  64. Help: "Number of volumes or shards.",
  65. }, []string{"collection", "type"})
  66. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  67. prometheus.GaugeOpts{
  68. Namespace: "SeaweedFS",
  69. Subsystem: "volumeServer",
  70. Name: "max_volumes",
  71. Help: "Maximum number of volumes.",
  72. })
  73. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  74. prometheus.GaugeOpts{
  75. Namespace: "SeaweedFS",
  76. Subsystem: "volumeServer",
  77. Name: "total_disk_size",
  78. Help: "Actual disk size used by volumes.",
  79. }, []string{"collection", "type"})
  80. )
  81. func init() {
  82. FilerGather.MustRegister(FilerRequestCounter)
  83. FilerGather.MustRegister(FilerRequestHistogram)
  84. FilerGather.MustRegister(FilerStoreCounter)
  85. FilerGather.MustRegister(FilerStoreHistogram)
  86. FilerGather.MustRegister(prometheus.NewGoCollector())
  87. VolumeServerGather.MustRegister(VolumeServerRequestCounter)
  88. VolumeServerGather.MustRegister(VolumeServerRequestHistogram)
  89. VolumeServerGather.MustRegister(VolumeServerVolumeCounter)
  90. VolumeServerGather.MustRegister(VolumeServerMaxVolumeCounter)
  91. VolumeServerGather.MustRegister(VolumeServerDiskSizeGauge)
  92. }
  93. func LoopPushingMetric(name, instance string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
  94. if addr == "" || intervalSeconds == 0 {
  95. return
  96. }
  97. pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
  98. for {
  99. err := pusher.Push()
  100. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  101. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  102. }
  103. if intervalSeconds <= 0 {
  104. intervalSeconds = 15
  105. }
  106. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  107. }
  108. }
  109. func SourceName(port uint32) string {
  110. hostname, err := os.Hostname()
  111. if err != nil {
  112. return "unknown"
  113. }
  114. return fmt.Sprintf("%s:%d", hostname, port)
  115. }