Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

108 строки
3.1 KiB

  1. // Copyright 2021 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. //go:build !go1.17
  14. // +build !go1.17
  15. package prometheus
  16. import (
  17. "runtime"
  18. "sync"
  19. "time"
  20. )
  21. type goCollector struct {
  22. base baseGoCollector
  23. // ms... are memstats related.
  24. msLast *runtime.MemStats // Previously collected memstats.
  25. msLastTimestamp time.Time
  26. msMtx sync.Mutex // Protects msLast and msLastTimestamp.
  27. msMetrics memStatsMetrics
  28. msRead func(*runtime.MemStats) // For mocking in tests.
  29. msMaxWait time.Duration // Wait time for fresh memstats.
  30. msMaxAge time.Duration // Maximum allowed age of old memstats.
  31. }
  32. // NewGoCollector is the obsolete version of collectors.NewGoCollector.
  33. // See there for documentation.
  34. //
  35. // Deprecated: Use collectors.NewGoCollector instead.
  36. func NewGoCollector() Collector {
  37. return &goCollector{
  38. base: newBaseGoCollector(),
  39. msLast: &runtime.MemStats{},
  40. msRead: runtime.ReadMemStats,
  41. msMaxWait: time.Second,
  42. msMaxAge: 5 * time.Minute,
  43. msMetrics: goRuntimeMemStats(),
  44. }
  45. }
  46. // Describe returns all descriptions of the collector.
  47. func (c *goCollector) Describe(ch chan<- *Desc) {
  48. c.base.Describe(ch)
  49. for _, i := range c.msMetrics {
  50. ch <- i.desc
  51. }
  52. }
  53. // Collect returns the current state of all metrics of the collector.
  54. func (c *goCollector) Collect(ch chan<- Metric) {
  55. var (
  56. ms = &runtime.MemStats{}
  57. done = make(chan struct{})
  58. )
  59. // Start reading memstats first as it might take a while.
  60. go func() {
  61. c.msRead(ms)
  62. c.msMtx.Lock()
  63. c.msLast = ms
  64. c.msLastTimestamp = time.Now()
  65. c.msMtx.Unlock()
  66. close(done)
  67. }()
  68. // Collect base non-memory metrics.
  69. c.base.Collect(ch)
  70. timer := time.NewTimer(c.msMaxWait)
  71. select {
  72. case <-done: // Our own ReadMemStats succeeded in time. Use it.
  73. timer.Stop() // Important for high collection frequencies to not pile up timers.
  74. c.msCollect(ch, ms)
  75. return
  76. case <-timer.C: // Time out, use last memstats if possible. Continue below.
  77. }
  78. c.msMtx.Lock()
  79. if time.Since(c.msLastTimestamp) < c.msMaxAge {
  80. // Last memstats are recent enough. Collect from them under the lock.
  81. c.msCollect(ch, c.msLast)
  82. c.msMtx.Unlock()
  83. return
  84. }
  85. // If we are here, the last memstats are too old or don't exist. We have
  86. // to wait until our own ReadMemStats finally completes. For that to
  87. // happen, we have to release the lock.
  88. c.msMtx.Unlock()
  89. <-done
  90. c.msCollect(ch, ms)
  91. }
  92. func (c *goCollector) msCollect(ch chan<- Metric, ms *runtime.MemStats) {
  93. for _, i := range c.msMetrics {
  94. ch <- MustNewConstMetric(i.desc, i.valType, i.eval(ms))
  95. }
  96. }