7 influxdb "github.com/influxdata/influxdb/client/v2"
9 "github.com/go-kit/kit/log"
12 func ExampleCounter() {
13 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
14 counter := in.NewCounter("influx_counter")
16 counter.With("error", "true").Add(1)
17 counter.With("error", "false").Add(2)
20 client := &bufWriter{}
23 expectedLines := []string{
24 `(influx_counter,a=b count=60) [0-9]{19}`,
25 `(influx_counter,a=b,error=true count=1) [0-9]{19}`,
26 `(influx_counter,a=b,error=false count=2) [0-9]{19}`,
29 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
30 fmt.Println(err.Error())
34 // influx_counter,a=b count=60
35 // influx_counter,a=b,error=true count=1
36 // influx_counter,a=b,error=false count=2
40 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
41 gauge := in.NewGauge("influx_gauge")
43 gauge.With("error", "true").Set(2)
44 gauge.With("error", "true").Set(1)
45 gauge.With("error", "false").Set(2)
47 gauge.With("test", "true").Set(1)
48 gauge.With("test", "true").Add(1)
50 client := &bufWriter{}
53 expectedLines := []string{
54 `(influx_gauge,a=b,test=true value=2) [0-9]{19}`,
55 `(influx_gauge,a=b value=50) [0-9]{19}`,
56 `(influx_gauge,a=b,error=true value=1) [0-9]{19}`,
57 `(influx_gauge,a=b,error=false value=2) [0-9]{19}`,
60 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
61 fmt.Println(err.Error())
65 // influx_gauge,a=b,test=true value=2
66 // influx_gauge,a=b value=50
67 // influx_gauge,a=b,error=true value=1
68 // influx_gauge,a=b,error=false value=2
71 func ExampleHistogram() {
72 in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
73 histogram := in.NewHistogram("influx_histogram")
74 histogram.Observe(float64(10))
75 histogram.With("error", "true").Observe(float64(1))
76 histogram.With("error", "false").Observe(float64(2))
77 histogram.Observe(float64(50))
79 client := &bufWriter{}
82 expectedLines := []string{
83 `(influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50) [0-9]{19}`,
84 `(influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1) [0-9]{19}`,
85 `(influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2) [0-9]{19}`,
88 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
89 fmt.Println(err.Error())
93 // influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50
94 // influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1
95 // influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2
98 func extractAndPrintMessage(expected []string, msg string) error {
99 for _, pattern := range expected {
100 re := regexp.MustCompile(pattern)
101 match := re.FindStringSubmatch(msg)
103 return fmt.Errorf("Pattern not found! {%s} [%s]: %v\n", pattern, msg, match)
105 fmt.Println(match[1])