package test
import "go.opencensus.io/metric/test"
Package test for testing code instrumented with the metric and stats packages.
Index ¶
- type Exporter
- func NewExporter(metricReader *metricexport.Reader) *Exporter
- func (e *Exporter) ExportMetrics(ctx context.Context, data []*metricdata.Metric) error
- func (e *Exporter) GetPoint(metricName string, labels map[string]string) (metricdata.Point, bool)
- func (e *Exporter) ReadAndExport()
- func (e *Exporter) String() string
Examples ¶
Types ¶
type Exporter ¶
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:
Output: Code:
Output:Example (Metric)¶
{
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
}
1
2
3
Example (Stats)¶
{
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
}
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 ¶
ExportMetrics records the view data.
func (*Exporter) GetPoint ¶
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 ¶
String defines the “native” format for the exporter.
Source Files ¶
doc.go exporter.go
- Version
- v0.24.0 (latest)
- Published
- Nov 3, 2022
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 5 hours ago –
Tools for package owners.