Skip to content

Commit

Permalink
Do not panic on unsupported Go runtime metrics
Browse files Browse the repository at this point in the history
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 #59
Updates #60
  • Loading branch information
valyala committed Dec 17, 2023
1 parent cd448dd commit 447d235
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 27 additions & 4 deletions go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"io"
"log"
"math"
"runtime"
runtimemetrics "runtime/metrics"
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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))
}
}

Expand Down
9 changes: 9 additions & 0 deletions go_metrics_test.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/valyala/fastrand/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/valyala/histogram/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/valyala/histogram/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 447d235

Please sign in to comment.