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