...

Package test

import "go.opencensus.io/metric/test"
Overview
Index
Examples

Overview ▾

Package test for testing code instrumented with the metric and stats packages.

type Exporter

Exporter keeps exported metric data in memory to aid in testing the instrumentation.

Metrics can be retrieved with `GetPoint()`. In order to deterministically retrieve the most recent values, you must first invoke `ReadAndExport()`.

type Exporter struct {
    // contains filtered or unexported fields
}

Example (Metric)

Code:

metricReader := metricexport.NewReader()
metrics := NewExporter(metricReader)
m := derivedMetric{}
r := metric.NewRegistry()
g, _ := r.AddInt64DerivedCumulative("derived", metric.WithLabelKeys(myTag.Name()))
g.UpsertEntry(m.ToInt64, metricdata.NewLabelValue("l1"))
for i := 1; i <= 3; i++ {
    // The code under test begins here.
    m.i = int64(i)
    // The code under test ends here.

    metrics.ExportMetrics(context.Background(), r.Read())
    metricValue := getCounter(metrics, "derived", newMetricKey("l1"))
    fmt.Println(metricValue)
}

Output:

1
2
3

Example (Stats)

Code:

metricReader := metricexport.NewReader()
metrics := NewExporter(metricReader)
metrics.ReadAndExport()
metricBase := getCounter(metrics, myMetric.Name(), newMetricKey("label1"))

for i := 1; i <= 3; i++ {
    // The code under test begins here.
    stats.RecordWithTags(context.Background(),
        []tag.Mutator{tag.Upsert(myTag, "label1")},
        myMetric.M(int64(i)))
    // The code under test ends here.

    metrics.ReadAndExport()
    metricValue := getCounter(metrics, myMetric.Name(), newMetricKey("label1"))
    fmt.Printf("increased by %d\n", metricValue-metricBase)
}

Output:

increased by 1
increased by 3
increased by 6

func NewExporter

func NewExporter(metricReader *metricexport.Reader) *Exporter

NewExporter returns a new exporter.

func (*Exporter) ExportMetrics

func (e *Exporter) ExportMetrics(ctx context.Context, data []*metricdata.Metric) error

ExportMetrics records the view data.

func (*Exporter) GetPoint

func (e *Exporter) GetPoint(metricName string, labels map[string]string) (metricdata.Point, bool)

GetPoint returns the latest point for the time series identified by the given labels.

func (*Exporter) ReadAndExport

func (e *Exporter) ReadAndExport()

ReadAndExport reads the current values for all metrics and makes them available to this exporter.

func (*Exporter) String

func (e *Exporter) String() string

String defines the “native” format for the exporter.