Debian Codegraph @

prometheus/go_collector_go116.go

raw
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
14 //go:build !go1.17
15 // +build !go1.17
16
17 package prometheus
18
19 import (
20 "runtime"
21 "sync"
22 "time"
23 )
24
25 type goCollector struct {
26 base baseGoCollector
27
28 // ms... are memstats related.
29 msLast *runtime.MemStats // Previously collected memstats.
30 msLastTimestamp time.Time
31 msMtx sync.Mutex // Protects msLast and msLastTimestamp.
32 msMetrics memStatsMetrics
33 msRead func(*runtime.MemStats) // For mocking in tests.
34 msMaxWait time.Duration // Wait time for fresh memstats.
35 msMaxAge time.Duration // Maximum allowed age of old memstats.
36 }
37
38 // NewGoCollector is the obsolete version of collectors.NewGoCollector.
39 // See there for documentation.
40 //
41 // Deprecated: Use collectors.NewGoCollector instead.
42 func NewGoCollector() Collector {
43 msMetrics := goRuntimeMemStats()
44 msMetrics = append(msMetrics, struct {
45 desc *Desc
46 eval func(*runtime.MemStats) float64
47 valType ValueType
48 }{
49 // This metric is omitted in Go1.17+, see https://github.com/prometheus/client_golang/issues/842#issuecomment-861812034
50 desc: NewDesc(
51 memstatNamespace("gc_cpu_fraction"),
52 "The fraction of this program's available CPU time used by the GC since the program started.",
53 nil, nil,
54 ),
55 eval: func(ms *runtime.MemStats) float64 { return ms.GCCPUFraction },
56 valType: GaugeValue,
57 })
58 return &goCollector{
59 base: newBaseGoCollector(),
60 msLast: &runtime.MemStats{},
61 msRead: runtime.ReadMemStats,
62 msMaxWait: time.Second,
63 msMaxAge: 5 * time.Minute,
64 msMetrics: msMetrics,
65 }
66 }
67
68 // Describe returns all descriptions of the collector.
69 func (c *goCollector) Describe(ch chan<- *Desc) {
70 c.base.Describe(ch)
71 for _, i := range c.msMetrics {
72 ch <- i.desc
73 }
74 }
75
76 // Collect returns the current state of all metrics of the collector.
77 func (c *goCollector) Collect(ch chan<- Metric) {
78 var (
79 ms = &runtime.MemStats{}
80 done = make(chan struct{})
81 )
82 // Start reading memstats first as it might take a while.
83 go func() {
84 c.msRead(ms)
85 c.msMtx.Lock()
86 c.msLast = ms
87 c.msLastTimestamp = time.Now()
88 c.msMtx.Unlock()
89 close(done)
90 }()
91
92 // Collect base non-memory metrics.
93 c.base.Collect(ch)
94
95 timer := time.NewTimer(c.msMaxWait)
96 select {
97 case <-done: // Our own ReadMemStats succeeded in time. Use it.
98 timer.Stop() // Important for high collection frequencies to not pile up timers.
99 c.msCollect(ch, ms)
100 return
101 case <-timer.C: // Time out, use last memstats if possible. Continue below.
102 }
103 c.msMtx.Lock()
104 if time.Since(c.msLastTimestamp) < c.msMaxAge {
105 // Last memstats are recent enough. Collect from them under the lock.
106 c.msCollect(ch, c.msLast)
107 c.msMtx.Unlock()
108 return
109 }
110 // If we are here, the last memstats are too old or don't exist. We have
111 // to wait until our own ReadMemStats finally completes. For that to
112 // happen, we have to release the lock.
113 c.msMtx.Unlock()
114 <-done
115 c.msCollect(ch, ms)
116 }
117
118 func (c *goCollector) msCollect(ch chan<- Metric, ms *runtime.MemStats) {
119 for _, i := range c.msMetrics {
120 ch <- MustNewConstMetric(i.desc, i.valType, i.eval(ms))
121 }
122 }