...
Package test
Package test for testing code instrumented with the metric and stats packages.
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 {
}
▾ 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++ {
m.i = int64(i)
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++ {
stats.RecordWithTags(context.Background(),
[]tag.Mutator{tag.Upsert(myTag, "label1")},
myMetric.M(int64(i)))
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(metricReader *metricexport.Reader) *Exporter
NewExporter returns a new exporter.
func (e *Exporter) ExportMetrics(ctx context.Context, data []*metricdata.Metric) error
ExportMetrics records the view data.
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 (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.