go.opencensus.io: go.opencensus.io/metric/test Index | Examples | Files

package test

import "go.opencensus.io/metric/test"

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

Index

Examples

Package Files

doc.go exporter.go

type Exporter Uses

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

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()`.

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

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 Uses

func NewExporter(metricReader *metricexport.Reader) *Exporter

NewExporter returns a new exporter.

func (*Exporter) ExportMetrics Uses

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

ExportMetrics records the view data.

func (*Exporter) GetPoint Uses

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 Uses

func (e *Exporter) ReadAndExport()

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

func (*Exporter) String Uses

func (e *Exporter) String() string

String defines the “native” format for the exporter.

Package test imports 8 packages (graph). Updated 2019-12-04. Refresh now. Tools for package owners.