2018-08-22 13:08:52 +03:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-12-11 02:01:46 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
2015-04-30 07:27:14 +03:00
|
|
|
"runtime/debug"
|
2019-05-04 02:00:59 +03:00
|
|
|
"sync"
|
2015-05-05 02:20:11 +03:00
|
|
|
"time"
|
2014-12-11 02:01:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type goCollector struct {
|
2017-02-15 08:06:22 +03:00
|
|
|
goroutinesDesc *Desc
|
|
|
|
threadsDesc *Desc
|
|
|
|
gcDesc *Desc
|
2017-07-24 00:36:09 +03:00
|
|
|
goInfoDesc *Desc
|
2015-07-22 06:29:02 +03:00
|
|
|
|
2019-05-04 02:00:59 +03:00
|
|
|
// ms... are memstats related.
|
|
|
|
msLast *runtime.MemStats // Previously collected memstats.
|
|
|
|
msLastTimestamp time.Time
|
|
|
|
msMtx sync.Mutex // Protects msLast and msLastTimestamp.
|
|
|
|
msMetrics memStatsMetrics
|
|
|
|
msRead func(*runtime.MemStats) // For mocking in tests.
|
|
|
|
msMaxWait time.Duration // Wait time for fresh memstats.
|
|
|
|
msMaxAge time.Duration // Maximum allowed age of old memstats.
|
2015-07-21 06:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:07:33 +03:00
|
|
|
// NewGoCollector returns a collector that exports metrics about the current Go
|
2018-05-19 22:14:04 +03:00
|
|
|
// process. This includes memory stats. To collect those, runtime.ReadMemStats
|
2019-05-04 02:00:59 +03:00
|
|
|
// is called. This requires to “stop the world”, which usually only happens for
|
|
|
|
// garbage collection (GC). Take the following implications into account when
|
|
|
|
// deciding whether to use the Go collector:
|
|
|
|
//
|
|
|
|
// 1. The performance impact of stopping the world is the more relevant the more
|
|
|
|
// frequently metrics are collected. However, with Go1.9 or later the
|
|
|
|
// stop-the-world time per metrics collection is very short (~25µs) so that the
|
|
|
|
// performance impact will only matter in rare cases. However, with older Go
|
|
|
|
// versions, the stop-the-world duration depends on the heap size and can be
|
|
|
|
// quite significant (~1.7 ms/GiB as per
|
2018-05-19 22:14:04 +03:00
|
|
|
// https://go-review.googlesource.com/c/go/+/34937).
|
2019-05-04 02:00:59 +03:00
|
|
|
//
|
|
|
|
// 2. During an ongoing GC, nothing else can stop the world. Therefore, if the
|
|
|
|
// metrics collection happens to coincide with GC, it will only complete after
|
|
|
|
// GC has finished. Usually, GC is fast enough to not cause problems. However,
|
|
|
|
// with a very large heap, GC might take multiple seconds, which is enough to
|
|
|
|
// cause scrape timeouts in common setups. To avoid this problem, the Go
|
|
|
|
// collector will use the memstats from a previous collection if
|
|
|
|
// runtime.ReadMemStats takes more than 1s. However, if there are no previously
|
|
|
|
// collected memstats, or their collection is more than 5m ago, the collection
|
2020-07-22 02:42:37 +03:00
|
|
|
// will block until runtime.ReadMemStats succeeds.
|
|
|
|
//
|
|
|
|
// NOTE: The problem is solved in Go 1.15, see
|
|
|
|
// https://github.com/golang/go/issues/19812 for the related Go issue.
|
Create a public registry interface and separate out HTTP exposition
General context and approch
===========================
This is the first part of the long awaited wider refurbishment of
`client_golang/prometheus/...`. After a lot of struggling, I decided
to not go for one breaking big-bang, but cut things into smaller steps
after all, mostly to keep the changes manageable and easy to
review. I'm aiming for having the invasive breaking changes
concentrated in as few steps as possible (ideally one). Some steps
will not be breaking at all, but typically there will be breaking
changes that only affect quite special cases so that 95+% of users
will not be affected. This first step is an example for that, see
details below.
What's happening in this commit?
================================
This step is about finally creating an exported registry
interface. This could not be done by simply export the existing
internal implementation because the interface would be _way_ too
fat. This commit introduces a qutie lean `Registry` interface
(compared to the previous interval implementation). The functions that
act on the default registry are retained (with very few exceptions) so
that most use cases won't see a change. However, several of those are
deprecated now to clean up the namespace in the future.
The default registry is kept in the public variable
`DefaultRegistry`. This follows the example of the http package in the
standard library (cf. `http.DefaultServeMux`, `http.DefaultClient`)
with the same implications. (This pattern is somewhat disputed within
the Go community but I chose to go with the devil you know instead of
creating something more complex or even disallowing any changes to the
default registry. The current approach gives everybody the freedom to
not touch DefaultRegistry or to do everything with a custom registry
to play save.)
Another important part in making the registry lean is the extraction
of the HTTP exposition, which also allows for customization of the
HTTP exposition. Note that the separation of metric collection and
exposition has the side effect that managing the MetricFamily and
Metric protobuf objects in a free-list or pool isn't really feasible
anymore. By now (with better GC in more recent Go versions), the
returns were anyway dimisishing. To be effective at all, scrapes had
to happen more often than GC cycles, and even then most elements of
the protobufs (everything excetp the MetricFamily and Metric structs
themselves) would still cause allocation churn. In a future breaking
change, the signature of the Write method in the Metric interface will
be adjusted accordingly. In this commit, avoiding breakage is more
important.
The following issues are fixed by this commit (some solved "on the
fly" now that I was touching the code anyway and it would have been
stupid to port the bugs):
https://github.com/prometheus/client_golang/issues/46
https://github.com/prometheus/client_golang/issues/100
https://github.com/prometheus/client_golang/issues/170
https://github.com/prometheus/client_golang/issues/205
Documentation including examples have been amended as required.
What future changes does this commit enable?
============================================
The following items are not yet implemented, but this commit opens the
possibility of implementing these independently.
- The separation of the HTTP exposition allows the implementation of
other exposition methods based on the Registry interface, as known
from other Prometheus client libraries, e.g. sending the metrics to
Graphite.
Cf. https://github.com/prometheus/client_golang/issues/197
- The public `Registry` interface allows the implementation of
convenience tools for testing metrics collection. Those tools can
inspect the collected MetricFamily protobufs and compare them to
expectation. Also, tests can use their own testing instance of a
registry.
Cf. https://github.com/prometheus/client_golang/issues/58
Notable non-goals of this commit
================================
Non-goals that will be tackled later
------------------------------------
The following two issues are quite closely connected to the changes in
this commit but the line has been drawn deliberately to address them
in later steps of the refurbishment:
- `InstrumentHandler` has many known problems. The plan is to create a
saner way to conveniently intrument HTTP handlers and remove the old
`InstrumentHandler` altogether. To keep breakage low for now, even
the default handler to expose metrics is still using the old
`InstrumentHandler`. This leads to weird naming inconsistencies but
I have deemed it better to not break the world right now but do it
in the change that provides better ways of instrumenting HTTP
handlers.
Cf. https://github.com/prometheus/client_golang/issues/200
- There is work underway to make the whole handling of metric
descriptors (`Desc`) more intuitive and transparent for the user
(including an ability for less strict checking,
cf. https://github.com/prometheus/client_golang/issues/47). That's
quite invasive from the perspective of the internal code, namely the
registry. I deliberately kept those changes out of this commit.
- While this commit adds new external dependency, the effort to vendor
anything within the library that is not visible in any exported
types will have to be done later.
Non-goals that _might_ be tackled later
---------------------------------------
There is a strong and understandable urge to divide the `prometheus`
package into a number of sub-packages (like `registry`, `collectors`,
`http`, `metrics`, …). However, to not run into a multitude of
circular import chains, this would need to break every single existing
usage of the library. (As just one example, if the ubiquitious
`prometheus.MustRegister` (with more than 2,000 uses on GitHub alone)
is kept in the `prometheus` package, but the other registry concerns
go into a new `registry` package, then the `prometheus` package would
import the `registry` package (to call the actual register method),
while at the same time the `registry` package needs to import the
`prometheus` package to access `Collector`, `Metric`, `Desc` and
more. If we moved `MustRegister` into the `registry` package,
thousands of code lines would have to be fixed (which would be easy if
the world was a mono repo, but it is not). If we moved everything else
the proposed registry package needs into packages of their own, we
would break thousands of other code lines.)
The main problem is really the top-level functions like
`MustRegister`, `Handler`, …, which effectively pull everything into
one package. Those functions are however very convenient for the easy
and very frequent use-cases.
This problem has to be revisited later.
For now, I'm trying to keep the amount of exported names in the
package as low as possible (e.g. I unexported expvarCollector in this
commit because the NewExpvarCollector constructor is enough to export,
and it is now consistent with other collectors, like the goCollector).
Non-goals that won't be tackled anytime soon
--------------------------------------------
Something that I have played with a lot is "streaming collection",
i.e. allow an implementation of the `Registry` interface that collects
metrics incrementally and serves them while doing so. As it has turned
out, this has many many issues and makes the `Registry` interface very
clunky. Eventually, I made the call that it is unlikely we will really
implement streaming collection; and making the interface more clunky
for something that might not even happen is really a big no-no. Note
that the `Registry` interface only creates the in-memory
representation of the metric family protobufs in one go. The
serializaton onto the wire can still be handled in a streaming fashion
(which hasn't been done so far, without causing any trouble, but might
be done in the future without breaking any interfaces).
What are the breaking changes?
==============================
- Signatures of functions pushing to Pushgateway have changed to allow
arbitrary grouping (which was planned for a long time anyway, and
now that I had to work on the Push code anyway for the registry
refurbishment, I finally did it,
cf. https://github.com/prometheus/client_golang/issues/100).
With the gained insight that pushing to the default registry is almost
never the right thing, and now that we are breaking the Push call
anyway, all the Push functions were moved to their own package,
which cleans up the namespace and is more idiomatic (pushing
Collectors is now literally done by `push.Collectors(...)`).
- The registry is doing more consistency checks by default now. Past
creators of inconsistent metrics could have masked the problem by
not setting `EnableCollectChecks`. Those inconsistencies will now be
detected. (But note that a "best effort" metrics collection is now
possible with `HandlerOpts.ErrorHandling = ContinueOnError`.)
- `EnableCollectChecks` is gone. The registry is now performing some
of those checks anyway (see previous item), and a registry with all
of those checks can now be created with `NewPedanticRegistry` (only
used for testing).
- `PanicOnCollectError` is gone. This behavior can now be configured
when creating a custom HTTP handler.
2016-07-20 18:11:14 +03:00
|
|
|
func NewGoCollector() Collector {
|
2015-07-21 18:24:22 +03:00
|
|
|
return &goCollector{
|
2017-02-15 08:06:22 +03:00
|
|
|
goroutinesDesc: NewDesc(
|
|
|
|
"go_goroutines",
|
|
|
|
"Number of goroutines that currently exist.",
|
|
|
|
nil, nil),
|
|
|
|
threadsDesc: NewDesc(
|
|
|
|
"go_threads",
|
2017-07-24 00:36:09 +03:00
|
|
|
"Number of OS threads created.",
|
2017-02-15 08:06:22 +03:00
|
|
|
nil, nil),
|
2015-07-21 18:24:22 +03:00
|
|
|
gcDesc: NewDesc(
|
|
|
|
"go_gc_duration_seconds",
|
2020-01-06 15:36:51 +03:00
|
|
|
"A summary of the pause duration of garbage collection cycles.",
|
2015-07-21 18:24:22 +03:00
|
|
|
nil, nil),
|
2017-07-24 00:36:09 +03:00
|
|
|
goInfoDesc: NewDesc(
|
|
|
|
"go_info",
|
|
|
|
"Information about the Go environment.",
|
|
|
|
nil, Labels{"version": runtime.Version()}),
|
2019-05-04 02:00:59 +03:00
|
|
|
msLast: &runtime.MemStats{},
|
|
|
|
msRead: runtime.ReadMemStats,
|
|
|
|
msMaxWait: time.Second,
|
|
|
|
msMaxAge: 5 * time.Minute,
|
|
|
|
msMetrics: memStatsMetrics{
|
2015-10-03 04:10:36 +03:00
|
|
|
{
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("alloc_bytes"),
|
|
|
|
"Number of bytes allocated and still in use.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("alloc_bytes_total"),
|
|
|
|
"Total number of bytes allocated, even if freed.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
|
|
|
|
valType: CounterValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("sys_bytes"),
|
2016-11-22 20:11:47 +03:00
|
|
|
"Number of bytes obtained from system.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
|
2015-10-05 19:27:28 +03:00
|
|
|
valType: GaugeValue,
|
2015-10-03 04:10:36 +03:00
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("lookups_total"),
|
|
|
|
"Total number of pointer lookups.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
|
|
|
|
valType: CounterValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("mallocs_total"),
|
|
|
|
"Total number of mallocs.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
|
|
|
|
valType: CounterValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("frees_total"),
|
|
|
|
"Total number of frees.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
|
|
|
|
valType: CounterValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("heap_alloc_bytes"),
|
2015-10-05 19:27:28 +03:00
|
|
|
"Number of heap bytes allocated and still in use.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("heap_sys_bytes"),
|
2015-10-05 19:27:28 +03:00
|
|
|
"Number of heap bytes obtained from system.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("heap_idle_bytes"),
|
2015-10-05 19:27:28 +03:00
|
|
|
"Number of heap bytes waiting to be used.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("heap_inuse_bytes"),
|
2015-10-05 19:27:28 +03:00
|
|
|
"Number of heap bytes that are in use.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2016-11-22 20:11:47 +03:00
|
|
|
memstatNamespace("heap_released_bytes"),
|
|
|
|
"Number of heap bytes released to OS.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
|
2016-11-22 20:11:47 +03:00
|
|
|
valType: GaugeValue,
|
2015-10-03 04:10:36 +03:00
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("heap_objects"),
|
|
|
|
"Number of allocated objects.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("stack_inuse_bytes"),
|
2015-10-03 04:10:36 +03:00
|
|
|
"Number of bytes in use by the stack allocator.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("stack_sys_bytes"),
|
2015-10-05 19:27:28 +03:00
|
|
|
"Number of bytes obtained from system for stack allocator.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("mspan_inuse_bytes"),
|
|
|
|
"Number of bytes in use by mspan structures.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("mspan_sys_bytes"),
|
|
|
|
"Number of bytes used for mspan structures obtained from system.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("mcache_inuse_bytes"),
|
|
|
|
"Number of bytes in use by mcache structures.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("mcache_sys_bytes"),
|
|
|
|
"Number of bytes used for mcache structures obtained from system.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("buck_hash_sys_bytes"),
|
|
|
|
"Number of bytes used by the profiling bucket hash table.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("gc_sys_bytes"),
|
|
|
|
"Number of bytes used for garbage collection system metadata.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("other_sys_bytes"),
|
|
|
|
"Number of bytes used for other system allocations.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("next_gc_bytes"),
|
|
|
|
"Number of heap bytes when next garbage collection will take place.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
|
|
|
|
valType: GaugeValue,
|
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
2015-10-05 19:27:28 +03:00
|
|
|
memstatNamespace("last_gc_time_seconds"),
|
|
|
|
"Number of seconds since 1970 of last garbage collection.",
|
2015-10-03 04:10:36 +03:00
|
|
|
nil, nil,
|
|
|
|
),
|
2016-02-08 03:23:24 +03:00
|
|
|
eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 },
|
2015-10-05 19:27:28 +03:00
|
|
|
valType: GaugeValue,
|
2017-02-22 19:25:59 +03:00
|
|
|
}, {
|
|
|
|
desc: NewDesc(
|
|
|
|
memstatNamespace("gc_cpu_fraction"),
|
|
|
|
"The fraction of this program's available CPU time used by the GC since the program started.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
eval: func(ms *runtime.MemStats) float64 { return ms.GCCPUFraction },
|
|
|
|
valType: GaugeValue,
|
2015-07-22 06:29:02 +03:00
|
|
|
},
|
|
|
|
},
|
2015-07-21 06:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 06:29:02 +03:00
|
|
|
func memstatNamespace(s string) string {
|
2019-05-06 01:39:02 +03:00
|
|
|
return "go_memstats_" + s
|
2015-07-22 06:29:02 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:24:22 +03:00
|
|
|
// Describe returns all descriptions of the collector.
|
|
|
|
func (c *goCollector) Describe(ch chan<- *Desc) {
|
2017-02-15 08:06:22 +03:00
|
|
|
ch <- c.goroutinesDesc
|
|
|
|
ch <- c.threadsDesc
|
2015-07-21 18:24:22 +03:00
|
|
|
ch <- c.gcDesc
|
2017-07-24 00:36:09 +03:00
|
|
|
ch <- c.goInfoDesc
|
2019-05-04 02:00:59 +03:00
|
|
|
for _, i := range c.msMetrics {
|
2015-10-03 04:10:36 +03:00
|
|
|
ch <- i.desc
|
|
|
|
}
|
2015-07-21 06:55:38 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:24:22 +03:00
|
|
|
// Collect returns the current state of all metrics of the collector.
|
|
|
|
func (c *goCollector) Collect(ch chan<- Metric) {
|
2019-05-04 02:00:59 +03:00
|
|
|
var (
|
|
|
|
ms = &runtime.MemStats{}
|
|
|
|
done = make(chan struct{})
|
|
|
|
)
|
|
|
|
// Start reading memstats first as it might take a while.
|
|
|
|
go func() {
|
|
|
|
c.msRead(ms)
|
|
|
|
c.msMtx.Lock()
|
|
|
|
c.msLast = ms
|
|
|
|
c.msLastTimestamp = time.Now()
|
|
|
|
c.msMtx.Unlock()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2017-02-15 08:06:22 +03:00
|
|
|
ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
|
2017-02-13 14:34:45 +03:00
|
|
|
n, _ := runtime.ThreadCreateProfile(nil)
|
2017-02-15 08:06:22 +03:00
|
|
|
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
|
2015-07-21 18:24:22 +03:00
|
|
|
|
|
|
|
var stats debug.GCStats
|
|
|
|
stats.PauseQuantiles = make([]time.Duration, 5)
|
|
|
|
debug.ReadGCStats(&stats)
|
|
|
|
|
|
|
|
quantiles := make(map[float64]float64)
|
|
|
|
for idx, pq := range stats.PauseQuantiles[1:] {
|
|
|
|
quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
|
2015-07-21 06:55:38 +03:00
|
|
|
}
|
2015-07-21 18:24:22 +03:00
|
|
|
quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
|
2018-04-13 23:59:56 +03:00
|
|
|
ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), stats.PauseTotal.Seconds(), quantiles)
|
2015-07-21 06:55:38 +03:00
|
|
|
|
2017-07-24 00:36:09 +03:00
|
|
|
ch <- MustNewConstMetric(c.goInfoDesc, GaugeValue, 1)
|
|
|
|
|
2019-05-04 02:00:59 +03:00
|
|
|
timer := time.NewTimer(c.msMaxWait)
|
|
|
|
select {
|
|
|
|
case <-done: // Our own ReadMemStats succeeded in time. Use it.
|
|
|
|
timer.Stop() // Important for high collection frequencies to not pile up timers.
|
|
|
|
c.msCollect(ch, ms)
|
|
|
|
return
|
|
|
|
case <-timer.C: // Time out, use last memstats if possible. Continue below.
|
|
|
|
}
|
|
|
|
c.msMtx.Lock()
|
|
|
|
if time.Since(c.msLastTimestamp) < c.msMaxAge {
|
|
|
|
// Last memstats are recent enough. Collect from them under the lock.
|
|
|
|
c.msCollect(ch, c.msLast)
|
|
|
|
c.msMtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If we are here, the last memstats are too old or don't exist. We have
|
|
|
|
// to wait until our own ReadMemStats finally completes. For that to
|
|
|
|
// happen, we have to release the lock.
|
|
|
|
c.msMtx.Unlock()
|
|
|
|
<-done
|
|
|
|
c.msCollect(ch, ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goCollector) msCollect(ch chan<- Metric, ms *runtime.MemStats) {
|
|
|
|
for _, i := range c.msMetrics {
|
2015-10-05 17:37:59 +03:00
|
|
|
ch <- MustNewConstMetric(i.desc, i.valType, i.eval(ms))
|
2015-10-03 04:10:36 +03:00
|
|
|
}
|
2015-07-22 06:29:02 +03:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:37:59 +03:00
|
|
|
// memStatsMetrics provide description, value, and value type for memstat metrics.
|
2015-07-22 06:29:02 +03:00
|
|
|
type memStatsMetrics []struct {
|
|
|
|
desc *Desc
|
|
|
|
eval func(*runtime.MemStats) float64
|
|
|
|
valType ValueType
|
|
|
|
}
|
2019-06-06 22:07:33 +03:00
|
|
|
|
|
|
|
// NewBuildInfoCollector returns a collector collecting a single metric
|
|
|
|
// "go_build_info" with the constant value 1 and three labels "path", "version",
|
|
|
|
// and "checksum". Their label values contain the main module path, version, and
|
|
|
|
// checksum, respectively. The labels will only have meaningful values if the
|
|
|
|
// binary is built with Go module support and from source code retrieved from
|
|
|
|
// the source repository (rather than the local file system). This is usually
|
|
|
|
// accomplished by building from outside of GOPATH, specifying the full address
|
|
|
|
// of the main package, e.g. "GO111MODULE=on go run
|
|
|
|
// github.com/prometheus/client_golang/examples/random". If built without Go
|
|
|
|
// module support, all label values will be "unknown". If built with Go module
|
|
|
|
// support but using the source code from the local file system, the "path" will
|
|
|
|
// be set appropriately, but "checksum" will be empty and "version" will be
|
|
|
|
// "(devel)".
|
|
|
|
//
|
|
|
|
// This collector uses only the build information for the main module. See
|
|
|
|
// https://github.com/povilasv/prommod for an example of a collector for the
|
|
|
|
// module dependencies.
|
|
|
|
func NewBuildInfoCollector() Collector {
|
|
|
|
path, version, sum := readBuildInfo()
|
|
|
|
c := &selfCollector{MustNewConstMetric(
|
|
|
|
NewDesc(
|
|
|
|
"go_build_info",
|
|
|
|
"Build information about the main Go module.",
|
|
|
|
nil, Labels{"path": path, "version": version, "checksum": sum},
|
|
|
|
),
|
|
|
|
GaugeValue, 1)}
|
|
|
|
c.init(c.self)
|
|
|
|
return c
|
|
|
|
}
|