Debian Codegraph @

prometheus/collectors/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 collectors
18
19 import "github.com/prometheus/client_golang/prometheus"
20
21 // NewGoCollector returns a collector that exports metrics about the current Go
22 // process. This includes memory stats. To collect those, runtime.ReadMemStats
23 // is called. This requires to “stop the world”, which usually only happens for
24 // garbage collection (GC). Take the following implications into account when
25 // deciding whether to use the Go collector:
26 //
27 // 1. The performance impact of stopping the world is the more relevant the more
28 // frequently metrics are collected. However, with Go1.9 or later the
29 // stop-the-world time per metrics collection is very short (~25µs) so that the
30 // performance impact will only matter in rare cases. However, with older Go
31 // versions, the stop-the-world duration depends on the heap size and can be
32 // quite significant (~1.7 ms/GiB as per
33 // https://go-review.googlesource.com/c/go/+/34937).
34 //
35 // 2. During an ongoing GC, nothing else can stop the world. Therefore, if the
36 // metrics collection happens to coincide with GC, it will only complete after
37 // GC has finished. Usually, GC is fast enough to not cause problems. However,
38 // with a very large heap, GC might take multiple seconds, which is enough to
39 // cause scrape timeouts in common setups. To avoid this problem, the Go
40 // collector will use the memstats from a previous collection if
41 // runtime.ReadMemStats takes more than 1s. However, if there are no previously
42 // collected memstats, or their collection is more than 5m ago, the collection
43 // will block until runtime.ReadMemStats succeeds.
44 //
45 // NOTE: The problem is solved in Go 1.15, see
46 // https://github.com/golang/go/issues/19812 for the related Go issue.
47 func NewGoCollector() prometheus.Collector {
48 return prometheus.NewGoCollector()
49 }