From 447d235cbb84d47adde5cd96943eb0d0d44a8836 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 17 Dec 2023 16:25:33 +0200 Subject: [PATCH] Do not panic on unsupported Go runtime metrics Log the unsupported Go runtime metrics on startup instead, so the user is aware of unsupported metrics. The solution for removing the log lines is to upgrade Go builder. Reduce the minimum supported Go version at go.mod from Go1.20 to Go1.16, where the runtime/metrics package has been added. See https://tip.golang.org/doc/go1.16#runtime Updates https://github.com/VictoriaMetrics/metrics/issues/59 Updates https://github.com/VictoriaMetrics/metrics/pull/60 --- go.mod | 4 +-- go_metrics.go | 31 +++++++++++++++++++--- go_metrics_test.go | 9 +++++++ vendor/github.com/valyala/fastrand/go.mod | 1 + vendor/github.com/valyala/histogram/go.mod | 5 ++++ vendor/github.com/valyala/histogram/go.sum | 2 ++ vendor/modules.txt | 5 ++-- 7 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 vendor/github.com/valyala/fastrand/go.mod create mode 100644 vendor/github.com/valyala/histogram/go.mod create mode 100644 vendor/github.com/valyala/histogram/go.sum diff --git a/go.mod b/go.mod index e9e547a..6a8d5f2 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,4 @@ require ( golang.org/x/sys v0.15.0 ) -require github.com/valyala/fastrand v1.1.0 // indirect - -go 1.20 +go 1.16 diff --git a/go_metrics.go b/go_metrics.go index 5de7cb8..1d0c7e2 100644 --- a/go_metrics.go +++ b/go_metrics.go @@ -3,6 +3,7 @@ package metrics import ( "fmt" "io" + "log" "math" "runtime" runtimemetrics "runtime/metrics" @@ -22,6 +23,25 @@ var runtimeMetrics = [][2]string{ {"/gc/gomemlimit:bytes", "go_memlimit_bytes"}, } +var supportedRuntimeMetrics = initSupportedRuntimeMetrics(runtimeMetrics) + +func initSupportedRuntimeMetrics(rms [][2]string) [][2]string { + exposedMetrics := make(map[string]struct{}) + for _, d := range runtimemetrics.All() { + exposedMetrics[d.Name] = struct{}{} + } + var supportedMetrics [][2]string + for _, rm := range rms { + metricName := rm[0] + if _, ok := exposedMetrics[metricName]; ok { + supportedMetrics = append(supportedMetrics, rm) + } else { + log.Printf("github.com/VictoriaMetrics/metrics: do not expose %s metric, since the corresponding metric %s isn't supported in the current Go runtime", rm[1], metricName) + } + } + return supportedMetrics +} + func writeGoMetrics(w io.Writer) { writeRuntimeMetrics(w) @@ -81,18 +101,19 @@ func writeGoMetrics(w io.Writer) { } func writeRuntimeMetrics(w io.Writer) { - samples := make([]runtimemetrics.Sample, len(runtimeMetrics)) - for i, rm := range runtimeMetrics { + samples := make([]runtimemetrics.Sample, len(supportedRuntimeMetrics)) + for i, rm := range supportedRuntimeMetrics { samples[i].Name = rm[0] } runtimemetrics.Read(samples) - for i, rm := range runtimeMetrics { + for i, rm := range supportedRuntimeMetrics { writeRuntimeMetric(w, rm[1], &samples[i]) } } func writeRuntimeMetric(w io.Writer, name string, sample *runtimemetrics.Sample) { - switch sample.Value.Kind() { + kind := sample.Value.Kind() + switch kind { case runtimemetrics.KindBad: panic(fmt.Errorf("BUG: unexpected runtimemetrics.KindBad for sample.Name=%q", sample.Name)) case runtimemetrics.KindUint64: @@ -101,6 +122,8 @@ func writeRuntimeMetric(w io.Writer, name string, sample *runtimemetrics.Sample) fmt.Fprintf(w, "%s %g\n", name, sample.Value.Float64()) case runtimemetrics.KindFloat64Histogram: writeRuntimeHistogramMetric(w, name, sample.Value.Float64Histogram()) + default: + panic(fmt.Errorf("unexpected metric kind=%d", kind)) } } diff --git a/go_metrics_test.go b/go_metrics_test.go index cd510e7..9493814 100644 --- a/go_metrics_test.go +++ b/go_metrics_test.go @@ -1,12 +1,21 @@ package metrics import ( + "bytes" "math" runtimemetrics "runtime/metrics" "strings" "testing" ) +func TestWriteRuntimeMetrics(t *testing.T) { + var bb bytes.Buffer + writeRuntimeMetrics(&bb) + if n := bb.Len(); n == 0 { + t.Fatalf("unexpected empty runtime metrics") + } +} + func TestWriteRuntimeHistogramMetricOk(t *testing.T) { f := func(h *runtimemetrics.Float64Histogram, resultExpected string) { t.Helper() diff --git a/vendor/github.com/valyala/fastrand/go.mod b/vendor/github.com/valyala/fastrand/go.mod new file mode 100644 index 0000000..958910b --- /dev/null +++ b/vendor/github.com/valyala/fastrand/go.mod @@ -0,0 +1 @@ +module github.com/valyala/fastrand diff --git a/vendor/github.com/valyala/histogram/go.mod b/vendor/github.com/valyala/histogram/go.mod new file mode 100644 index 0000000..cc65b00 --- /dev/null +++ b/vendor/github.com/valyala/histogram/go.mod @@ -0,0 +1,5 @@ +module github.com/valyala/histogram + +go 1.12 + +require github.com/valyala/fastrand v1.1.0 diff --git a/vendor/github.com/valyala/histogram/go.sum b/vendor/github.com/valyala/histogram/go.sum new file mode 100644 index 0000000..c5ca588 --- /dev/null +++ b/vendor/github.com/valyala/histogram/go.sum @@ -0,0 +1,2 @@ +github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= +github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= diff --git a/vendor/modules.txt b/vendor/modules.txt index 682dbd5..f8aabff 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,9 +1,8 @@ # github.com/valyala/fastrand v1.1.0 -## explicit github.com/valyala/fastrand # github.com/valyala/histogram v1.2.0 -## explicit; go 1.12 +## explicit github.com/valyala/histogram # golang.org/x/sys v0.15.0 -## explicit; go 1.18 +## explicit golang.org/x/sys/windows