File: exporter_test.go

package info (click to toggle)
golang-go.opencensus 0.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 86; sh: 6
file content (96 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package test

import (
	"context"
	"fmt"

	"go.opencensus.io/metric"
	"go.opencensus.io/metric/metricdata"
	"go.opencensus.io/metric/metricexport"
	"go.opencensus.io/stats"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/tag"
)

var (
	myTag    = tag.MustNewKey("my_label")
	myMetric = stats.Int64("my_metric", "description", stats.UnitDimensionless)
)

func init() {
	if err := view.Register(
		&view.View{
			Measure:     myMetric,
			TagKeys:     []tag.Key{myTag},
			Aggregation: view.Sum(),
		},
	); err != nil {
		panic(err)
	}
}

func ExampleExporter_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
}

type derivedMetric struct {
	i int64
}

func (m *derivedMetric) ToInt64() int64 {
	return m.i
}

func ExampleExporter_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
}

func newMetricKey(v string) map[string]string {
	return map[string]string{myTag.Name(): v}
}

func getCounter(metrics *Exporter, metricName string, metricKey map[string]string) int64 {
	p, ok := metrics.GetPoint(metricName, metricKey)
	if !ok {
		// This is expected before the metric is recorded the first time.
		return 0
	}
	return p.Value.(int64)
}