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 ¶
- func AssertMetric(t *testing.T, values ...Metric)
- func AssertMetricExists(t *testing.T, names ...string)
- func AssertNoMetric(t *testing.T, names ...string)
- func CheckCountData(t ti, name string, wantTags map[string]string, wantValue int64)
- func CheckDistributionCount(t ti, name string, wantTags map[string]string, expectedCount int64)
- func CheckDistributionData(t ti, name string, wantTags map[string]string, expectedCount int64, ...)
- func CheckLastValueData(t ti, name string, wantTags map[string]string, wantValue float64)
- func CheckLastValueDataWithMeter(t ti, name string, wantTags map[string]string, wantValue float64, ...)
- func CheckStatsNotReported(t ti, names ...string)
- func CheckStatsReported(t ti, names ...string)
- func CheckSumData(t ti, name string, wantTags map[string]string, wantValue float64)
- func EnsureRecorded()
- func GetLastValueData(t ti, name string, tags map[string]string) float64
- func GetLastValueDataWithMeter(t ti, name string, tags map[string]string, meter view.Meter) float64
- func Unregister(names ...string)
- type Metric
- func DistributionCountOnlyMetric(name string, count int64, tags map[string]string) Metric
- func FloatMetric(name string, value float64, tags map[string]string) Metric
- func GetMetric(name string) []Metric
- func GetOneMetric(name string) Metric
- func IntMetric(name string, value int64, tags map[string]string) Metric
- func NewMetric(metric *metricdata.Metric) Metric
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertMetric ¶
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 ¶
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 ¶
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 ¶
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 ¶
CheckDistributionRange 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 ¶
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 ¶
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 ¶
GetLastValueData returns the last value for the given metric, verifying tags.
func GetLastValueDataWithMeter ¶
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 ¶
DistributionCountOnlyMetric creates a distribution metric for test, and verifying only the count.
func FloatMetric ¶
FloatMetric creates a Float64 metric
func GetOneMetric ¶
GetOneMetric is like GetMetric, but it panics if more than a single Metric is found.
func NewMetric ¶
func NewMetric(metric *metricdata.Metric) Metric
NewMetric creates a Metric from a metricdata.Metric, which is designed for compact wire representation.
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 ¶
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 ¶
VisitFloat64Value implements metricdata.ValueVisitor.
func (*Value) VisitInt64Value ¶
VisitInt64Value implements metricdata.ValueVisitor.
func (*Value) VisitSummaryValue ¶
func (v *Value) VisitSummaryValue(*metricdata.Summary)
VisitSummaryValue implements metricdata.ValueVisitor.