summaryrefslogtreecommitdiffhomepage
path: root/pkg/metric/metric.go
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2020-04-21 16:48:30 +0000
committergVisor bot <gvisor-bot@google.com>2020-04-21 16:48:30 +0000
commit4813fb89ce2049644fe8bf13b214d89c65950642 (patch)
tree76b3e329656726f85664805c26355100b2ffd3d6 /pkg/metric/metric.go
parent80adf98cbdd5306410c005fc5ea1fe994cefb793 (diff)
parent8b72623e6ababc5448de0cb347476eaf4a611e2c (diff)
Merge release-20200323.0-201-g8b72623 (automated)
Diffstat (limited to 'pkg/metric/metric.go')
-rw-r--r--pkg/metric/metric.go41
1 files changed, 21 insertions, 20 deletions
diff --git a/pkg/metric/metric.go b/pkg/metric/metric.go
index 895253625..64aa365ce 100644
--- a/pkg/metric/metric.go
+++ b/pkg/metric/metric.go
@@ -39,16 +39,11 @@ var (
// Uint64Metric encapsulates a uint64 that represents some kind of metric to be
// monitored.
//
-// All metrics must be cumulative, meaning that their values will only increase
-// over time.
-//
// Metrics are not saved across save/restore and thus reset to zero on restore.
//
-// TODO(b/67298402): Support non-cumulative metrics.
// TODO(b/67298427): Support metric fields.
type Uint64Metric struct {
- // value is the actual value of the metric. It must be accessed
- // atomically.
+ // value is the actual value of the metric. It must be accessed atomically.
value uint64
}
@@ -110,13 +105,10 @@ type customUint64Metric struct {
// Register must only be called at init and will return and error if called
// after Initialized.
//
-// All metrics must be cumulative, meaning that the return values of value must
-// only increase over time.
-//
// Preconditions:
// * name must be globally unique.
// * Initialize/Disable have not been called.
-func RegisterCustomUint64Metric(name string, sync bool, description string, value func() uint64) error {
+func RegisterCustomUint64Metric(name string, cumulative, sync bool, units pb.MetricMetadata_Units, description string, value func() uint64) error {
if initialized {
return ErrInitializationDone
}
@@ -129,9 +121,10 @@ func RegisterCustomUint64Metric(name string, sync bool, description string, valu
metadata: &pb.MetricMetadata{
Name: name,
Description: description,
- Cumulative: true,
+ Cumulative: cumulative,
Sync: sync,
- Type: pb.MetricMetadata_UINT64,
+ Type: pb.MetricMetadata_TYPE_UINT64,
+ Units: units,
},
value: value,
}
@@ -140,24 +133,32 @@ func RegisterCustomUint64Metric(name string, sync bool, description string, valu
// MustRegisterCustomUint64Metric calls RegisterCustomUint64Metric and panics
// if it returns an error.
-func MustRegisterCustomUint64Metric(name string, sync bool, description string, value func() uint64) {
- if err := RegisterCustomUint64Metric(name, sync, description, value); err != nil {
+func MustRegisterCustomUint64Metric(name string, cumulative, sync bool, description string, value func() uint64) {
+ if err := RegisterCustomUint64Metric(name, cumulative, sync, pb.MetricMetadata_UNITS_NONE, description, value); err != nil {
panic(fmt.Sprintf("Unable to register metric %q: %v", name, err))
}
}
-// NewUint64Metric creates and registers a new 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, description string) (*Uint64Metric, error) {
+func NewUint64Metric(name string, sync bool, units pb.MetricMetadata_Units, description string) (*Uint64Metric, error) {
var m Uint64Metric
- return &m, RegisterCustomUint64Metric(name, sync, description, m.Value)
+ 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, description)
+ m, err := NewUint64Metric(name, sync, pb.MetricMetadata_UNITS_NONE, description)
+ if err != nil {
+ panic(fmt.Sprintf("Unable to create metric %q: %v", name, err))
+ }
+ return m
+}
+
+// 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 {
panic(fmt.Sprintf("Unable to create metric %q: %v", name, err))
}