選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

278 行
8.2 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. }
  199. }
  200. type baseGoCollector struct {
  201. goroutinesDesc *Desc
  202. threadsDesc *Desc
  203. gcDesc *Desc
  204. gcLastTimeDesc *Desc
  205. goInfoDesc *Desc
  206. }
  207. func newBaseGoCollector() baseGoCollector {
  208. return baseGoCollector{
  209. goroutinesDesc: NewDesc(
  210. "go_goroutines",
  211. "Number of goroutines that currently exist.",
  212. nil, nil),
  213. threadsDesc: NewDesc(
  214. "go_threads",
  215. "Number of OS threads created.",
  216. nil, nil),
  217. gcDesc: NewDesc(
  218. "go_gc_duration_seconds",
  219. "A summary of the pause duration of garbage collection cycles.",
  220. nil, nil),
  221. gcLastTimeDesc: NewDesc(
  222. memstatNamespace("last_gc_time_seconds"),
  223. "Number of seconds since 1970 of last garbage collection.",
  224. nil, nil),
  225. goInfoDesc: NewDesc(
  226. "go_info",
  227. "Information about the Go environment.",
  228. nil, Labels{"version": runtime.Version()}),
  229. }
  230. }
  231. // Describe returns all descriptions of the collector.
  232. func (c *baseGoCollector) Describe(ch chan<- *Desc) {
  233. ch <- c.goroutinesDesc
  234. ch <- c.threadsDesc
  235. ch <- c.gcDesc
  236. ch <- c.gcLastTimeDesc
  237. ch <- c.goInfoDesc
  238. }
  239. // Collect returns the current state of all metrics of the collector.
  240. func (c *baseGoCollector) Collect(ch chan<- Metric) {
  241. ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
  242. n, _ := runtime.ThreadCreateProfile(nil)
  243. ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
  244. var stats debug.GCStats
  245. stats.PauseQuantiles = make([]time.Duration, 5)
  246. debug.ReadGCStats(&stats)
  247. quantiles := make(map[float64]float64)
  248. for idx, pq := range stats.PauseQuantiles[1:] {
  249. quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
  250. }
  251. quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
  252. ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles)
  253. ch <- MustNewConstMetric(c.gcLastTimeDesc, GaugeValue, float64(stats.LastGC.UnixNano())/1e9)
  254. ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1)
  255. }
  256. func memstatNamespace(s string) string {
  257. return "go_memstats_" + s
  258. }
  259. // memStatsMetrics provide description, evaluator, runtime/metrics name, and
  260. // value type for memstat metrics.
  261. // TODO(bwplotka): Remove with end Go 1.16 EOL and replace with runtime/metrics.Description
  262. type memStatsMetrics []struct {
  263. desc *Desc
  264. eval func(*runtime.MemStats) float64
  265. valType ValueType
  266. }