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

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