Debian Codegraph @

prometheus/go_collector.go

raw
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
14 package prometheus
15
16 import (
17 "runtime"
18 "runtime/debug"
19 "time"
20 )
21
22 // goRuntimeMemStats provides the metrics initially provided by runtime.ReadMemStats.
23 // From Go 1.17 those similar (and better) statistics are provided by runtime/metrics, so
24 // while eval closure works on runtime.MemStats, the struct from Go 1.17+ is
25 // populated using runtime/metrics. Those are the defaults we can't alter.
26 func goRuntimeMemStats() memStatsMetrics {
27 return memStatsMetrics{
28 {
29 desc: NewDesc(
30 memstatNamespace("alloc_bytes"),
31 "Number of bytes allocated in heap and currently in use. Equals to /memory/classes/heap/objects:bytes.",
32 nil, nil,
33 ),
34 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
35 valType: GaugeValue,
36 }, {
37 desc: NewDesc(
38 memstatNamespace("alloc_bytes_total"),
39 "Total number of bytes allocated in heap until now, even if released already. Equals to /gc/heap/allocs:bytes.",
40 nil, nil,
41 ),
42 eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
43 valType: CounterValue,
44 }, {
45 desc: NewDesc(
46 memstatNamespace("sys_bytes"),
47 "Number of bytes obtained from system. Equals to /memory/classes/total:byte.",
48 nil, nil,
49 ),
50 eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
51 valType: GaugeValue,
52 }, {
53 desc: NewDesc(
54 memstatNamespace("mallocs_total"),
55 // TODO(bwplotka): We could add go_memstats_heap_objects, probably useful for discovery. Let's gather more feedback, kind of a waste of bytes for everybody for compatibility reasons to keep both, and we can't really rename/remove useful metric.
56 "Total number of heap objects allocated, both live and gc-ed. Semantically a counter version for go_memstats_heap_objects gauge. Equals to /gc/heap/allocs:objects + /gc/heap/tiny/allocs:objects.",
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 heap objects frees. Equals to /gc/heap/frees:objects + /gc/heap/tiny/allocs:objects.",
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 currently in use, same as go_memstats_alloc_bytes. Equals to /memory/classes/heap/objects:bytes.",
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. Equals to /memory/classes/heap/objects:bytes + /memory/classes/heap/unused:bytes + /memory/classes/heap/released:bytes + /memory/classes/heap/free:bytes.",
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. Equals to /memory/classes/heap/released:bytes + /memory/classes/heap/free:bytes.",
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. Equals to /memory/classes/heap/objects:bytes + /memory/classes/heap/unused:bytes",
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. Equals to /memory/classes/heap/released:bytes.",
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 currently allocated objects. Equals to /gc/heap/objects: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 obtained from system for stack allocator in non-CGO environments. Equals to /memory/classes/heap/stacks:bytes.",
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. Equals to /memory/classes/heap/stacks:bytes + /memory/classes/os-stacks:bytes.",
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. Equals to /memory/classes/metadata/mspan/inuse:bytes.",
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. Equals to /memory/classes/metadata/mspan/inuse:bytes + /memory/classes/metadata/mspan/free:bytes.",
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. Equals to /memory/classes/metadata/mcache/inuse:bytes.",
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. Equals to /memory/classes/metadata/mcache/inuse:bytes + /memory/classes/metadata/mcache/free:bytes.",
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. Equals to /memory/classes/profiling/buckets:bytes.",
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. Equals to /memory/classes/metadata/other:bytes.",
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. Equals to /memory/classes/other:bytes.",
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. Equals to /gc/heap/goal:bytes.",
193 nil, nil,
194 ),
195 eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
196 valType: GaugeValue,
197 },
198 }
199 }
200
201 type baseGoCollector struct {
202 goroutinesDesc *Desc
203 threadsDesc *Desc
204 gcDesc *Desc
205 gcLastTimeDesc *Desc
206 goInfoDesc *Desc
207 }
208
209 func newBaseGoCollector() baseGoCollector {
210 return baseGoCollector{
211 goroutinesDesc: NewDesc(
212 "go_goroutines",
213 "Number of goroutines that currently exist.",
214 nil, nil),
215 threadsDesc: NewDesc(
216 "go_threads",
217 "Number of OS threads created.",
218 nil, nil),
219 gcDesc: NewDesc(
220 "go_gc_duration_seconds",
221 "A summary of the wall-time pause (stop-the-world) duration in garbage collection cycles.",
222 nil, nil),
223 gcLastTimeDesc: NewDesc(
224 "go_memstats_last_gc_time_seconds",
225 "Number of seconds since 1970 of last garbage collection.",
226 nil, nil),
227 goInfoDesc: NewDesc(
228 "go_info",
229 "Information about the Go environment.",
230 nil, Labels{"version": runtime.Version()}),
231 }
232 }
233
234 // Describe returns all descriptions of the collector.
235 func (c *baseGoCollector) Describe(ch chan<- *Desc) {
236 ch <- c.goroutinesDesc
237 ch <- c.threadsDesc
238 ch <- c.gcDesc
239 ch <- c.gcLastTimeDesc
240 ch <- c.goInfoDesc
241 }
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
247 n := getRuntimeNumThreads()
248 ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)
249
250 var stats debug.GCStats
251 stats.PauseQuantiles = make([]time.Duration, 5)
252 debug.ReadGCStats(&stats)
253
254 quantiles := make(map[float64]float64)
255 for idx, pq := range stats.PauseQuantiles[1:] {
256 quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
257 }
258 quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
259 ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles)
260 ch <- MustNewConstMetric(c.gcLastTimeDesc, GaugeValue, float64(stats.LastGC.UnixNano())/1e9)
261 ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1)
262 }
263
264 func memstatNamespace(s string) string {
265 return "go_memstats_" + s
266 }
267
268 // memStatsMetrics provide description, evaluator, runtime/metrics name, and
269 // value type for memstat metrics.
270 type memStatsMetrics []struct {
271 desc *Desc
272 eval func(*runtime.MemStats) float64
273 valType ValueType
274 }