diff --git a/go/vt/mysqlctl/mysqld.go b/go/vt/mysqlctl/mysqld.go index 72c1d8f6658..d7435705a8a 100644 --- a/go/vt/mysqlctl/mysqld.go +++ b/go/vt/mysqlctl/mysqld.go @@ -47,6 +47,7 @@ import ( "vitess.io/vitess/config" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/sqlerror" + "vitess.io/vitess/go/osutil" "vitess.io/vitess/go/protoutil" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" @@ -1332,18 +1333,7 @@ func hostMetrics(ctx context.Context, cnf *Mycnf) (*mysqlctlpb.HostMetricsRespon _ = func() error { metric := newMetric("loadavg") - if runtime.GOOS != "linux" { - return withError(metric, fmt.Errorf("loadavg metric is only available on Linux")) - } - content, err := os.ReadFile("/proc/loadavg") - if err != nil { - return withError(metric, err) - } - fields := strings.Fields(string(content)) - if len(fields) == 0 { - return withError(metric, fmt.Errorf("unexpected /proc/loadavg content")) - } - loadAvg, err := strconv.ParseFloat(fields[0], 64) + loadAvg, err := osutil.LoadAvg() if err != nil { return withError(metric, err) } diff --git a/go/vt/vttablet/tabletserver/throttle/base/self_metric_loadavg.go b/go/vt/vttablet/tabletserver/throttle/base/self_metric_loadavg.go index 40a2878421a..640a2153698 100644 --- a/go/vt/vttablet/tabletserver/throttle/base/self_metric_loadavg.go +++ b/go/vt/vttablet/tabletserver/throttle/base/self_metric_loadavg.go @@ -18,22 +18,12 @@ package base import ( "context" - "fmt" - "os" "runtime" - "strconv" - "strings" + "vitess.io/vitess/go/osutil" "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" ) -var ( - loadavgOnlyAvailableOnLinuxMetric = &ThrottleMetric{ - Scope: SelfScope, - Err: fmt.Errorf("loadavg metric is only available on Linux"), - } -) - var _ SelfMetric = registerSelfMetric(&LoadAvgSelfMetric{}) type LoadAvgSelfMetric struct { @@ -56,26 +46,13 @@ func (m *LoadAvgSelfMetric) RequiresConn() bool { } func (m *LoadAvgSelfMetric) Read(ctx context.Context, throttler ThrottlerMetricsPublisher, conn *connpool.Conn) *ThrottleMetric { - if runtime.GOOS != "linux" { - return loadavgOnlyAvailableOnLinuxMetric - } metric := &ThrottleMetric{ Scope: SelfScope, } - { - content, err := os.ReadFile("/proc/loadavg") - if err != nil { - return metric.WithError(err) - } - fields := strings.Fields(string(content)) - if len(fields) == 0 { - return metric.WithError(fmt.Errorf("unexpected /proc/loadavg content")) - } - loadAvg, err := strconv.ParseFloat(fields[0], 64) - if err != nil { - return metric.WithError(err) - } - metric.Value = loadAvg / float64(runtime.NumCPU()) + val, err := osutil.LoadAvg() + if err != nil { + return metric.WithError(err) } + metric.Value = val / float64(runtime.NumCPU()) return metric }