您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

282 行
8.4 KiB

  1. // Copyright 2018 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. package prometheus
  14. import (
  15. "runtime"
  16. "runtime/debug"
  17. "time"
  18. )
  19. // goRuntimeMemStats provides the metrics initially provided by runtime.ReadMemStats.
  20. // From Go 1.17 those similar (and better) statistics are provided by runtime/metrics, so
  21. // while eval closure works on runtime.MemStats, the struct from Go 1.17+ is
  22. // populated using runtime/metrics.
  23. func goRuntimeMemStats() memStatsMetrics {
  24. return memStatsMetrics{
  25. {
  26. desc: NewDesc(
  27. memstatNamespace("alloc_bytes"),
  28. "Number of bytes allocated and still in use.",
  29. nil, nil,
  30. ),
  31. eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
  32. valType: GaugeValue,
  33. }, {
  34. desc: NewDesc(
  35. memstatNamespace("alloc_bytes_total"),
  36. "Total number of bytes allocated, even if freed.",
  37. nil, nil,
  38. ),
  39. eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
  40. valType: CounterValue,
  41. }, {
  42. desc: NewDesc(
  43. memstatNamespace("sys_bytes"),
  44. "Number of bytes obtained from system.",
  45. nil, nil,
  46. ),
  47. eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
  48. valType: GaugeValue,
  49. }, {
  50. desc: NewDesc(
  51. memstatNamespace("lookups_total"),
  52. "Total number of pointer lookups.",
  53. nil, nil,
  54. ),
  55. eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
  56. valType: CounterValue,
  57. }, {
  58. desc: NewDesc(
  59. memstatNamespace("mallocs_total"),
  60. "Total number of mallocs.",
  61. nil, nil,
  62. ),
  63. eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
  64. valType: CounterValue,
  65. }, {
  66. desc: NewDesc(
  67. memstatNamespace("frees_total"),
  68. "Total number of frees.",
  69. nil, nil,
  70. ),
  71. eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
  72. valType: CounterValue,
  73. }, {
  74. desc: NewDesc(
  75. memstatNamespace("heap_alloc_bytes"),
  76. "Number of heap bytes allocated and still in use.",
  77. nil, nil,
  78. ),
  79. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
  80. valType: GaugeValue,
  81. }, {
  82. desc: NewDesc(
  83. memstatNamespace("heap_sys_bytes"),
  84. "Number of heap bytes obtained from system.",
  85. nil, nil,
  86. ),
  87. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
  88. valType: GaugeValue,
  89. }, {
  90. desc: NewDesc(
  91. memstatNamespace("heap_idle_bytes"),
  92. "Number of heap bytes waiting to be used.",
  93. nil, nil,
  94. ),
  95. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
  96. valType: GaugeValue,
  97. }, {
  98. desc: NewDesc(
  99. memstatNamespace("heap_inuse_bytes"),
  100. "Number of heap bytes that are in use.",
  101. nil, nil,
  102. ),
  103. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
  104. valType: GaugeValue,
  105. }, {
  106. desc: NewDesc(
  107. memstatNamespace("heap_released_bytes"),
  108. "Number of heap bytes released to OS.",
  109. nil, nil,
  110. ),
  111. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
  112. valType: GaugeValue,
  113. }, {
  114. desc: NewDesc(
  115. memstatNamespace("heap_objects"),
  116. "Number of allocated objects.",
  117. nil, nil,
  118. ),
  119. eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
  120. valType: GaugeValue,
  121. }, {
  122. desc: NewDesc(
  123. memstatNamespace("stack_inuse_bytes"),
  124. "Number of bytes in use by the stack allocator.",
  125. nil, nil,
  126. ),
  127. eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
  128. valType: GaugeValue,
  129. }, {
  130. desc: NewDesc(
  131. memstatNamespace("stack_sys_bytes"),
  132. "Number of bytes obtained from system for stack allocator.",
  133. nil, nil,
  134. ),
  135. eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
  136. valType: GaugeValue,
  137. }, {
  138. desc: NewDesc(
  139. memstatNamespace("mspan_inuse_bytes"),
  140. "Number of bytes in use by mspan structures.",
  141. nil, nil,
  142. ),
  143. eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
  144. valType: GaugeValue,
  145. }, {
  146. desc: NewDesc(
  147. memstatNamespace("mspan_sys_bytes"),
  148. "Number of bytes used for mspan structures obtained from system.",
  149. nil, nil,
  150. ),
  151. eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
  152. valType: GaugeValue,
  153. }, {
  154. desc: NewDesc(
  155. memstatNamespace("mcache_inuse_bytes"),
  156. "Number of bytes in use by mcache structures.",
  157. nil, nil,
  158. ),
  159. eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
  160. valType: GaugeValue,
  161. }, {
  162. desc: NewDesc(
  163. memstatNamespace("mcache_sys_bytes"),
  164. "Number of bytes used for mcache structures obtained from system.",
  165. nil, nil,
  166. ),
  167. eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
  168. valType: GaugeValue,
  169. }, {
  170. desc: NewDesc(
  171. memstatNamespace("buck_hash_sys_bytes"),
  172. "Number of bytes used by the profiling bucket hash table.",
  173. nil, nil,
  174. ),
  175. eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
  176. valType: GaugeValue,
  177. }, {
  178. desc: NewDesc(
  179. memstatNamespace("gc_sys_bytes"),
  180. "Number of bytes used for garbage collection system metadata.",
  181. nil, nil,
  182. ),
  183. eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
  184. valType: GaugeValue,
  185. }, {
  186. desc: NewDesc(
  187. memstatNamespace("other_sys_bytes"),
  188. "Number of bytes used for other system allocations.",
  189. nil, nil,
  190. ),
  191. eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
  192. valType: GaugeValue,
  193. }, {
  194. desc: NewDesc(
  195. memstatNamespace("next_gc_bytes"),
  196. "Number of heap bytes when next garbage collection will take place.",
  197. nil, nil,
  198. ),
  199. eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
  200. valType: GaugeValue,
  201. },
  202. }
  203. }
  204. type baseGoCollector struct {
  205. goroutinesDesc *Desc
  206. threadsDesc *Desc
  207. gcDesc *Desc
  208. gcLastTimeDesc *Desc
  209. goInfoDesc *Desc
  210. }
  211. func newBaseGoCollector() baseGoCollector {
  212. return baseGoCollector{
  213. goroutinesDesc: NewDesc(
  214. "go_goroutines",
  215. "Number of goroutines that currently exist.",
  216. nil, nil),
  217. threadsDesc: NewDesc(
  218. "go_threads",
  219. "Number of OS threads created.",
  220. nil, nil),
  221. gcDesc: NewDesc(
  222. "go_gc_duration_seconds",
  223. "A summary of the pause duration of garbage collection cycles.",
  224. nil, nil),
  225. gcLastTimeDesc: NewDesc(
  226. "go_memstats_last_gc_time_seconds",
  227. "Number of seconds since 1970 of last garbage collection.",
  228. nil, nil),
  229. goInfoDesc: NewDesc(
  230. "go_info",
  231. "Information about the Go environment.",
  232. nil, Labels{"version": runtime.Version()}),
  233. }
  234. }
  235. // Describe returns all descriptions of the collector.
  236. func (c *baseGoCollector) Describe(ch chan<- *Desc) {
  237. ch <- c.goroutinesDesc
  238. ch <- c.threadsDesc
  239. ch <- c.gcDesc
  240. ch <- c.gcLastTimeDesc
  241. ch <- c.goInfoDesc
  242. }
  243. // Collect returns the current state of all metrics of the collector.
  244. func (c *baseGoCollector) Collect(ch chan<- Metric) {
  245. ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
  246. n := getRuntimeNumThreads()
  247. ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)
  248. var stats debug.GCStats
  249. stats.PauseQuantiles = make([]time.Duration, 5)
  250. debug.ReadGCStats(&stats)
  251. quantiles := make(map[float64]float64)
  252. for idx, pq := range stats.PauseQuantiles[1:] {
  253. quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
  254. }
  255. quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
  256. ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles)
  257. ch <- MustNewConstMetric(c.gcLastTimeDesc, GaugeValue, float64(stats.LastGC.UnixNano())/1e9)
  258. ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1)
  259. }
  260. func memstatNamespace(s string) string {
  261. return "go_memstats_" + s
  262. }
  263. // memStatsMetrics provide description, evaluator, runtime/metrics name, and
  264. // value type for memstat metrics.
  265. type memStatsMetrics []struct {
  266. desc *Desc
  267. eval func(*runtime.MemStats) float64
  268. valType ValueType
  269. }