Skip to content

Commit 0ce6d71

Browse files
bwplotkavesari
andauthored
go collector: add default metrics acceptance tests; adding more context to HELP (#1568)
* go collector: add default metrics acceptance tests; adding more context to HELP The context and details for help were possible thanks to @vesari research, thanks for that! Signed-off-by: bwplotka <[email protected]> * Update prometheus/go_collector.go Co-authored-by: Arianna Vespri <[email protected]> Signed-off-by: Bartlomiej Plotka <[email protected]> --------- Signed-off-by: bwplotka <[email protected]> Signed-off-by: Bartlomiej Plotka <[email protected]> Co-authored-by: Arianna Vespri <[email protected]>
1 parent 697372d commit 0ce6d71

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

prometheus/collectors/go_collector_latest_test.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ var baseMetrics = []string{
3838
"go_threads",
3939
}
4040

41+
var memstatMetrics = []string{
42+
"go_memstats_alloc_bytes",
43+
"go_memstats_alloc_bytes_total",
44+
"go_memstats_buck_hash_sys_bytes",
45+
"go_memstats_frees_total",
46+
"go_memstats_gc_sys_bytes",
47+
"go_memstats_heap_alloc_bytes",
48+
"go_memstats_heap_idle_bytes",
49+
"go_memstats_heap_inuse_bytes",
50+
"go_memstats_heap_objects",
51+
"go_memstats_heap_released_bytes",
52+
"go_memstats_heap_sys_bytes",
53+
"go_memstats_lookups_total",
54+
"go_memstats_mallocs_total",
55+
"go_memstats_mcache_inuse_bytes",
56+
"go_memstats_mcache_sys_bytes",
57+
"go_memstats_mspan_inuse_bytes",
58+
"go_memstats_mspan_sys_bytes",
59+
"go_memstats_next_gc_bytes",
60+
"go_memstats_other_sys_bytes",
61+
"go_memstats_stack_inuse_bytes",
62+
"go_memstats_stack_sys_bytes",
63+
"go_memstats_sys_bytes",
64+
}
65+
4166
func TestGoCollectorMarshalling(t *testing.T) {
4267
reg := prometheus.NewRegistry()
4368
reg.MustRegister(NewGoCollector(
@@ -55,6 +80,24 @@ func TestGoCollectorMarshalling(t *testing.T) {
5580
}
5681
}
5782

83+
func TestWithGoCollectorDefault(t *testing.T) {
84+
reg := prometheus.NewRegistry()
85+
reg.MustRegister(NewGoCollector())
86+
result, err := reg.Gather()
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
got := []string{}
92+
for _, r := range result {
93+
got = append(got, r.GetName())
94+
}
95+
96+
if diff := cmp.Diff(got, withBaseMetrics(memstatMetrics)); diff != "" {
97+
t.Errorf("[IMPORTANT, those are default metrics, can't change in 1.x] missmatch (-want +got):\n%s", diff)
98+
}
99+
}
100+
58101
func TestWithGoCollectorMemStatsMetricsDisabled(t *testing.T) {
59102
reg := prometheus.NewRegistry()
60103
reg.MustRegister(NewGoCollector(
@@ -192,7 +235,7 @@ func TestGoCollectorDenyList(t *testing.T) {
192235
func ExampleGoCollector() {
193236
reg := prometheus.NewRegistry()
194237

195-
// Register the GoCollector with the default options. Only the base metrics will be enabled.
238+
// Register the GoCollector with the default options. Only the base metrics and memstats are enabled.
196239
reg.MustRegister(NewGoCollector())
197240

198241
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))

prometheus/go_collector.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ import (
2222
// goRuntimeMemStats provides the metrics initially provided by runtime.ReadMemStats.
2323
// From Go 1.17 those similar (and better) statistics are provided by runtime/metrics, so
2424
// while eval closure works on runtime.MemStats, the struct from Go 1.17+ is
25-
// populated using runtime/metrics.
25+
// populated using runtime/metrics. Those are the defaults we can't alter.
2626
func goRuntimeMemStats() memStatsMetrics {
2727
return memStatsMetrics{
2828
{
2929
desc: NewDesc(
3030
memstatNamespace("alloc_bytes"),
31-
"Number of bytes allocated and still in use.",
31+
"Number of bytes allocated and currently in use.",
3232
nil, nil,
3333
),
3434
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
3535
valType: GaugeValue,
3636
}, {
3737
desc: NewDesc(
3838
memstatNamespace("alloc_bytes_total"),
39-
"Total number of bytes allocated, even if freed.",
39+
"Total number of bytes allocated until now, even if released already.",
4040
nil, nil,
4141
),
4242
eval: func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
@@ -60,23 +60,24 @@ func goRuntimeMemStats() memStatsMetrics {
6060
}, {
6161
desc: NewDesc(
6262
memstatNamespace("mallocs_total"),
63-
"Total number of mallocs.",
63+
// TODO(bwplotka): We could add go_memstats_heap_objects, probably useful for discovery. Let's gather more feedback, kind of waste of bytes for everybody for compatibility reason.
64+
"Total number of heap objects allocated, both live and gc-ed. Semantically a counter version for go_memstats_heap_objects gauge.",
6465
nil, nil,
6566
),
6667
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
6768
valType: CounterValue,
6869
}, {
6970
desc: NewDesc(
7071
memstatNamespace("frees_total"),
71-
"Total number of frees.",
72+
"Total number of heap objects frees.",
7273
nil, nil,
7374
),
7475
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
7576
valType: CounterValue,
7677
}, {
7778
desc: NewDesc(
7879
memstatNamespace("heap_alloc_bytes"),
79-
"Number of heap bytes allocated and still in use.",
80+
"Number of heap bytes allocated and currently in use.",
8081
nil, nil,
8182
),
8283
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
@@ -116,7 +117,7 @@ func goRuntimeMemStats() memStatsMetrics {
116117
}, {
117118
desc: NewDesc(
118119
memstatNamespace("heap_objects"),
119-
"Number of allocated objects.",
120+
"Number of currently allocated objects.",
120121
nil, nil,
121122
),
122123
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
@@ -225,7 +226,7 @@ func newBaseGoCollector() baseGoCollector {
225226
nil, nil),
226227
gcDesc: NewDesc(
227228
"go_gc_duration_seconds",
228-
"A summary of the pause duration of garbage collection cycles.",
229+
"A summary of the wall-time pause (stop-the-world) duration in garbage collection cycles.",
229230
nil, nil),
230231
gcLastTimeDesc: NewDesc(
231232
"go_memstats_last_gc_time_seconds",

0 commit comments

Comments
 (0)