Debian Codegraph @

prometheus/collectors/go_collector_latest.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 (
20 "regexp"
21
22 "github.com/prometheus/client_golang/prometheus"
23 "github.com/prometheus/client_golang/prometheus/internal"
24 )
25
26 var (
27 // MetricsAll allows all the metrics to be collected from Go runtime.
28 MetricsAll = GoRuntimeMetricsRule{regexp.MustCompile("/.*")}
29 // MetricsGC allows only GC metrics to be collected from Go runtime.
30 // e.g. go_gc_cycles_automatic_gc_cycles_total
31 // NOTE: This does not include new class of "/cpu/classes/gc/..." metrics.
32 // Use custom metric rule to access those.
33 MetricsGC = GoRuntimeMetricsRule{regexp.MustCompile(`^/gc/.*`)}
34 // MetricsMemory allows only memory metrics to be collected from Go runtime.
35 // e.g. go_memory_classes_heap_free_bytes
36 MetricsMemory = GoRuntimeMetricsRule{regexp.MustCompile(`^/memory/.*`)}
37 // MetricsScheduler allows only scheduler metrics to be collected from Go runtime.
38 // e.g. go_sched_goroutines_goroutines
39 MetricsScheduler = GoRuntimeMetricsRule{regexp.MustCompile(`^/sched/.*`)}
40 // MetricsDebug allows only debug metrics to be collected from Go runtime.
41 // e.g. go_godebug_non_default_behavior_gocachetest_events_total
42 MetricsDebug = GoRuntimeMetricsRule{regexp.MustCompile(`^/godebug/.*`)}
43 )
44
45 // WithGoCollectorMemStatsMetricsDisabled disables metrics that is gathered in runtime.MemStats structure such as:
46 //
47 // go_memstats_alloc_bytes
48 // go_memstats_alloc_bytes_total
49 // go_memstats_sys_bytes
50 // go_memstats_mallocs_total
51 // go_memstats_frees_total
52 // go_memstats_heap_alloc_bytes
53 // go_memstats_heap_sys_bytes
54 // go_memstats_heap_idle_bytes
55 // go_memstats_heap_inuse_bytes
56 // go_memstats_heap_released_bytes
57 // go_memstats_heap_objects
58 // go_memstats_stack_inuse_bytes
59 // go_memstats_stack_sys_bytes
60 // go_memstats_mspan_inuse_bytes
61 // go_memstats_mspan_sys_bytes
62 // go_memstats_mcache_inuse_bytes
63 // go_memstats_mcache_sys_bytes
64 // go_memstats_buck_hash_sys_bytes
65 // go_memstats_gc_sys_bytes
66 // go_memstats_other_sys_bytes
67 // go_memstats_next_gc_bytes
68 //
69 // so the metrics known from pre client_golang v1.12.0,
70 //
71 // NOTE(bwplotka): The above represents runtime.MemStats statistics, but they are
72 // actually implemented using new runtime/metrics package. (except skipped go_memstats_gc_cpu_fraction
73 // -- see https://github.com/prometheus/client_golang/issues/842#issuecomment-861812034 for explanation).
74 //
75 // Some users might want to disable this on collector level (although you can use scrape relabelling on Prometheus),
76 // because similar metrics can be now obtained using WithGoCollectorRuntimeMetrics. Note that the semantics of new
77 // metrics might be different, plus the names can be change over time with different Go version.
78 //
79 // NOTE(bwplotka): Changing metric names can be tedious at times as the alerts, recording rules and dashboards have to be adjusted.
80 // The old metrics are also very useful, with many guides and books written about how to interpret them.
81 //
82 // As a result our recommendation would be to stick with MemStats like metrics and enable other runtime/metrics if you are interested
83 // in advanced insights Go provides. See ExampleGoCollector_WithAdvancedGoMetrics.
84 func WithGoCollectorMemStatsMetricsDisabled() func(options *internal.GoCollectorOptions) {
85 return func(o *internal.GoCollectorOptions) {
86 o.DisableMemStatsLikeMetrics = true
87 }
88 }
89
90 // GoRuntimeMetricsRule allow enabling and configuring particular group of runtime/metrics.
91 // TODO(bwplotka): Consider adding ability to adjust buckets.
92 type GoRuntimeMetricsRule struct {
93 // Matcher represents RE2 expression will match the runtime/metrics from https://golang.bg/src/runtime/metrics/description.go
94 // Use `regexp.MustCompile` or `regexp.Compile` to create this field.
95 Matcher *regexp.Regexp
96 }
97
98 // WithGoCollectorRuntimeMetrics allows enabling and configuring particular group of runtime/metrics.
99 // See the list of metrics https://golang.bg/src/runtime/metrics/description.go (pick the Go version you use there!).
100 // You can use this option in repeated manner, which will add new rules. The order of rules is important, the last rule
101 // that matches particular metrics is applied.
102 func WithGoCollectorRuntimeMetrics(rules ...GoRuntimeMetricsRule) func(options *internal.GoCollectorOptions) {
103 rs := make([]internal.GoCollectorRule, len(rules))
104 for i, r := range rules {
105 rs[i] = internal.GoCollectorRule{
106 Matcher: r.Matcher,
107 }
108 }
109
110 return func(o *internal.GoCollectorOptions) {
111 o.RuntimeMetricRules = append(o.RuntimeMetricRules, rs...)
112 }
113 }
114
115 // WithoutGoCollectorRuntimeMetrics allows disabling group of runtime/metrics that you might have added in WithGoCollectorRuntimeMetrics.
116 // It behaves similarly to WithGoCollectorRuntimeMetrics just with deny-list semantics.
117 func WithoutGoCollectorRuntimeMetrics(matchers ...*regexp.Regexp) func(options *internal.GoCollectorOptions) {
118 rs := make([]internal.GoCollectorRule, len(matchers))
119 for i, m := range matchers {
120 rs[i] = internal.GoCollectorRule{
121 Matcher: m,
122 Deny: true,
123 }
124 }
125
126 return func(o *internal.GoCollectorOptions) {
127 o.RuntimeMetricRules = append(o.RuntimeMetricRules, rs...)
128 }
129 }
130
131 // GoCollectionOption represents Go collection option flag.
132 // Deprecated.
133 type GoCollectionOption uint32
134
135 const (
136 // GoRuntimeMemStatsCollection represents the metrics represented by runtime.MemStats structure.
137 //
138 // Deprecated: Use WithGoCollectorMemStatsMetricsDisabled() function to disable those metrics in the collector.
139 GoRuntimeMemStatsCollection GoCollectionOption = 1 << iota
140 // GoRuntimeMetricsCollection is the new set of metrics represented by runtime/metrics package.
141 //
142 // Deprecated: Use WithGoCollectorRuntimeMetrics(GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")})
143 // function to enable those metrics in the collector.
144 GoRuntimeMetricsCollection
145 )
146
147 // WithGoCollections allows enabling different collections for Go collector on top of base metrics.
148 //
149 // Deprecated: Use WithGoCollectorRuntimeMetrics() and WithGoCollectorMemStatsMetricsDisabled() instead to control metrics.
150 func WithGoCollections(flags GoCollectionOption) func(options *internal.GoCollectorOptions) {
151 return func(options *internal.GoCollectorOptions) {
152 if flags&GoRuntimeMemStatsCollection == 0 {
153 WithGoCollectorMemStatsMetricsDisabled()(options)
154 }
155
156 if flags&GoRuntimeMetricsCollection != 0 {
157 WithGoCollectorRuntimeMetrics(GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")})(options)
158 }
159 }
160 }
161
162 // NewGoCollector returns a collector that exports metrics about the current Go
163 // process using debug.GCStats (base metrics) and runtime/metrics (both in MemStats style and new ones).
164 func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) prometheus.Collector {
165 //nolint:staticcheck // Ignore SA1019 until v2.
166 return prometheus.NewGoCollector(opts...)
167 }