Human-readable metric emit logs

Rather than dumping metrics on a single line, nearly unrelated textproto, print
them in alphabetical order, each on their own line.

e.g.,

D0108 17:42:42.198216  3382465 metric.go:253] Emitting metrics:
D0108 17:42:42.198240  3382465 metric.go:255] /fs/opens: &{Uint64Value:22}
D0108 17:42:42.198271  3382465 metric.go:255] /fs/read_wait: &{Uint64Value:0}
D0108 17:42:42.198294  3382465 metric.go:255] /fs/reads: &{Uint64Value:26}
D0108 17:42:42.198319  3382465 metric.go:255] /gofer/opened_write_execute_file: &{Uint64Value:0}
D0108 17:42:42.198327  3382465 metric.go:255] /gofer/opens_9p: &{Uint64Value:0}
D0108 17:42:42.198340  3382465 metric.go:255] /gofer/opens_host: &{Uint64Value:20}
...

PiperOrigin-RevId: 351590340
This commit is contained in:
Michael Pratt
2021-01-13 08:20:00 -08:00
committed by gVisor bot
parent 19ab0f15f3
commit e4d8f55cef
+17 -4
View File
@@ -18,6 +18,7 @@ package metric
import (
"errors"
"fmt"
"sort"
"sync/atomic"
"gvisor.dev/gvisor/pkg/eventchannel"
@@ -139,7 +140,8 @@ func MustRegisterCustomUint64Metric(name string, cumulative, sync bool, descript
}
}
// NewUint64Metric creates and registers a new cumulative metric with the given name.
// NewUint64Metric creates and registers a new cumulative metric with the given
// name.
//
// Metrics must be statically defined (i.e., at init).
func NewUint64Metric(name string, sync bool, units pb.MetricMetadata_Units, description string) (*Uint64Metric, error) {
@@ -147,7 +149,8 @@ func NewUint64Metric(name string, sync bool, units pb.MetricMetadata_Units, desc
return &m, RegisterCustomUint64Metric(name, true /* cumulative */, sync, units, description, m.Value)
}
// MustCreateNewUint64Metric calls NewUint64Metric and panics if it returns an error.
// MustCreateNewUint64Metric calls NewUint64Metric and panics if it returns an
// error.
func MustCreateNewUint64Metric(name string, sync bool, description string) *Uint64Metric {
m, err := NewUint64Metric(name, sync, pb.MetricMetadata_UNITS_NONE, description)
if err != nil {
@@ -156,7 +159,8 @@ func MustCreateNewUint64Metric(name string, sync bool, description string) *Uint
return m
}
// MustCreateNewUint64NanosecondsMetric calls NewUint64Metric and panics if it returns an error.
// MustCreateNewUint64NanosecondsMetric calls NewUint64Metric and panics if it
// returns an error.
func MustCreateNewUint64NanosecondsMetric(name string, sync bool, description string) *Uint64Metric {
m, err := NewUint64Metric(name, sync, pb.MetricMetadata_UNITS_NANOSECONDS, description)
if err != nil {
@@ -245,6 +249,15 @@ func EmitMetricUpdate() {
return
}
log.Debugf("Emitting metrics: %v", &m)
if log.IsLogging(log.Debug) {
sort.Slice(m.Metrics, func(i, j int) bool {
return m.Metrics[i].Name < m.Metrics[j].Name
})
log.Debugf("Emitting metrics:")
for _, metric := range m.Metrics {
log.Debugf("%s: %+v", metric.Name, metric.Value)
}
}
eventchannel.Emit(&m)
}