metricstest

package
v0.0.0-...-339c22b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 3

Documentation

Overview

Package metricstest simplifies some of the common boilerplate around testing metrics exports. It should work with or without the code in metrics, but this code particularly knows how to deal with metrics which are exported for multiple Resources in the same process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertMetric

func AssertMetric(t *testing.T, values ...Metric)

AssertMetric verifies that the metrics have the specified values. Note that this method will spuriously fail if there are multiple metrics with the same name on different Meters. Calls EnsureRecorded internally before fetching the batch of metrics.

func AssertMetricExists

func AssertMetricExists(t *testing.T, names ...string)

AssertMetricExists verifies that at least one metric values has been reported for each of metric names. Calls EnsureRecorded internally before fetching the batch of metrics.

func AssertNoMetric

func AssertNoMetric(t *testing.T, names ...string)

AssertNoMetric verifies that no metrics have been reported for any of the metric names. Calls EnsureRecorded internally before fetching the batch of metrics.

func CheckCountData

func CheckCountData(t ti, name string, wantTags map[string]string, wantValue int64)

CheckCountData checks the view with a name matching string name to verify that the CountData stats reported are tagged with the tags in wantTags and that wantValue matches reported count.

func CheckDistributionCount

func CheckDistributionCount(t ti, name string, wantTags map[string]string, expectedCount int64)

CheckDistributionCount checks the view with a name matching string name to verify that the DistributionData stats reported are tagged with the tags in wantTags and that expectedCount number of records were reported.

func CheckDistributionData

func CheckDistributionData(t ti, name string, wantTags map[string]string, expectedCount int64, expectedMin float64, expectedMax float64)

CheckDistributionData checks the view with a name matching string name to verify that the DistributionData stats reported are tagged with the tags in wantTags and that expectedCount number of records were reported. It also checks that expectedMin and expectedMax match the minimum and maximum reported values, respectively.

func CheckLastValueData

func CheckLastValueData(t ti, name string, wantTags map[string]string, wantValue float64)

CheckLastValueData checks the view with a name matching string name to verify that the LastValueData stats reported are tagged with the tags in wantTags and that wantValue matches reported last value.

func CheckLastValueDataWithMeter

func CheckLastValueDataWithMeter(t ti, name string, wantTags map[string]string, wantValue float64, meter view.Meter)

CheckLastValueDataWithMeter checks the view with a name matching the string name in the specified Meter (resource-specific view) to verify that the LastValueData stats are tagged with the tags in wantTags and that wantValue matches the last reported value.

func CheckStatsNotReported

func CheckStatsNotReported(t ti, names ...string)

CheckStatsNotReported checks that there are no records for any views that a name matching a string in names. Names that do not match registered views are considered not reported.

func CheckStatsReported

func CheckStatsReported(t ti, names ...string)

CheckStatsReported checks that there is a view registered with the given name for each string in names, and that each view has at least one record.

func CheckSumData

func CheckSumData(t ti, name string, wantTags map[string]string, wantValue float64)

CheckSumData checks the view with a name matching string name to verify that the SumData stats reported are tagged with the tags in wantTags and that wantValue matches the reported sum.

func EnsureRecorded

func EnsureRecorded()

EnsureRecorded makes sure that all stats metrics are actually flushed and recorded.

func GetLastValueData

func GetLastValueData(t ti, name string, tags map[string]string) float64

GetLastValueData returns the last value for the given metric, verifying tags.

func GetLastValueDataWithMeter

func GetLastValueDataWithMeter(t ti, name string, tags map[string]string, meter view.Meter) float64

GetLastValueDataWithMeter returns the last value of the given metric using meter, verifying tags.

func Unregister

func Unregister(names ...string)

Unregister unregisters the metrics that were registered. This is useful for testing since golang execute test iterations within the same process and opencensus views maintain global state. At the beginning of each test, tests should unregister for all metrics and then re-register for the same metrics. This effectively clears out any existing data and avoids a panic due to re-registering a metric.

In normal process shutdown, metrics do not need to be unregistered.

Types

type Metric

type Metric struct {
	// Name is the exported name of the metric, probably from the View's name.
	Name string
	// Unit is the units of measure of the metric. This is only checked for
	// equality if Unit is non-empty or VerifyMetadata is true on both Metrics.
	Unit metricdata.Unit
	// Type is the type of measurement represented by the metric. This is only
	// checked for equality if VerifyMetadata is true on both Metrics.
	Type metricdata.Type

	// Resource is the reported Resource (if any) for this metric. This is only
	// checked for equality if Resource is non-nil or VerifyResource is true on
	// both Metrics.
	Resource *resource.Resource

	// Values contains the values recorded for different Key=Value Tag
	// combinations. Value is checked for equality if present.
	Values []Value

	// VerifyMetadata makes Equal compare Unit and Type if it is true on both
	// Metrics.
	VerifyMetadata bool
	// VerifyResource makes Equal compare Resource if it is true on Metrics with
	// nil Resource. Metrics with non-nil Resource are always compared.
	VerifyResource bool
}

Metric provides a simplified (for testing) implementation of a metric report for a given metric name in a given Resource.

func DistributionCountOnlyMetric

func DistributionCountOnlyMetric(name string, count int64, tags map[string]string) Metric

DistributionCountOnlyMetric creates a distribution metric for test, and verifying only the count.

func FloatMetric

func FloatMetric(name string, value float64, tags map[string]string) Metric

FloatMetric creates a Float64 metric

func GetMetric

func GetMetric(name string) []Metric

GetMetric returns all values for the named metric.

func GetOneMetric

func GetOneMetric(name string) Metric

GetOneMetric is like GetMetric, but it panics if more than a single Metric is found.

func IntMetric

func IntMetric(name string, value int64, tags map[string]string) Metric

IntMetric creates an Int64 metric.

func NewMetric

func NewMetric(metric *metricdata.Metric) Metric

NewMetric creates a Metric from a metricdata.Metric, which is designed for compact wire representation.

func (Metric) Equal

func (m Metric) Equal(other Metric) bool

Equal provides a contract for use with github.com/google/go-cmp/cmp. Due to the reflection in cmp, it only works if the type of the two arguments to cmp are the same.

func (Metric) WithResource

func (m Metric) WithResource(r *resource.Resource) Metric

WithResource sets the resource of the metric.

type Value

type Value struct {
	Tags map[string]string
	// union interface, only one of these will be set
	Int64        *int64
	Float64      *float64
	Distribution *metricdata.Distribution
	// VerifyDistributionCountOnly makes Equal compare the Distribution with the
	// field Count only, and ignore all other fields of Distribution.
	// This is ignored when the value is not a Distribution.
	VerifyDistributionCountOnly bool
}

Value provides a simplified implementation of a metric Value suitable for easy testing.

func (Value) Equal

func (v Value) Equal(other Value) bool

Equal provides a contract for github.com/google/go-cmp/cmp. It compares two values, including deep comparison of Distributions. (Exemplars are intentional not included in the comparison, but other fields are considered).

func (*Value) VisitDistributionValue

func (v *Value) VisitDistributionValue(d *metricdata.Distribution)

VisitDistributionValue implements metricdata.ValueVisitor.

func (*Value) VisitFloat64Value

func (v *Value) VisitFloat64Value(f float64)

VisitFloat64Value implements metricdata.ValueVisitor.

func (*Value) VisitInt64Value

func (v *Value) VisitInt64Value(i int64)

VisitInt64Value implements metricdata.ValueVisitor.

func (*Value) VisitSummaryValue

func (v *Value) VisitSummaryValue(*metricdata.Summary)

VisitSummaryValue implements metricdata.ValueVisitor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL