Package list golang-github-go-kit-kit / e2b8274
examples moved to a dedicated file kpacha 5 years ago
2 changed file(s) with 104 addition(s) and 94 deletion(s). Raw diff Collapse all Expand all
0 package influx
1
2 import (
3 "fmt"
4 "regexp"
5
6 influxdb "github.com/influxdata/influxdb/client/v2"
7
8 "github.com/go-kit/kit/log"
9 )
10
11 func ExampleCounter() {
12 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
13 counter := in.NewCounter("influx_counter")
14 counter.Add(10)
15 counter.With("error", "true").Add(1)
16 counter.With("error", "false").Add(2)
17 counter.Add(50)
18
19 client := &bufWriter{}
20 in.WriteTo(client)
21
22 expectedLines := []string{
23 `(influx_counter,a=b count=60) [0-9]{19}`,
24 `(influx_counter,a=b,error=true count=1) [0-9]{19}`,
25 `(influx_counter,a=b,error=false count=2) [0-9]{19}`,
26 }
27
28 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
29 fmt.Println(err.Error())
30 }
31
32 // Output:
33 // influx_counter,a=b count=60
34 // influx_counter,a=b,error=true count=1
35 // influx_counter,a=b,error=false count=2
36 }
37
38 func ExampleGauge() {
39 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
40 gauge := in.NewGauge("influx_gauge")
41 gauge.Set(10)
42 gauge.With("error", "true").Set(2)
43 gauge.With("error", "true").Set(1)
44 gauge.With("error", "false").Set(2)
45 gauge.Set(50)
46
47 client := &bufWriter{}
48 in.WriteTo(client)
49
50 expectedLines := []string{
51 `(influx_gauge,a=b value=50) [0-9]{19}`,
52 `(influx_gauge,a=b,error=true value=1) [0-9]{19}`,
53 `(influx_gauge,a=b,error=false value=2) [0-9]{19}`,
54 }
55
56 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
57 fmt.Println(err.Error())
58 }
59
60 // Output:
61 // influx_gauge,a=b value=50
62 // influx_gauge,a=b,error=true value=1
63 // influx_gauge,a=b,error=false value=2
64 }
65
66 func ExampleHistogram() {
67 in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
68 histogram := in.NewHistogram("influx_histogram")
69 histogram.Observe(float64(10))
70 histogram.With("error", "true").Observe(float64(1))
71 histogram.With("error", "false").Observe(float64(2))
72 histogram.Observe(float64(50))
73
74 client := &bufWriter{}
75 in.WriteTo(client)
76
77 expectedLines := []string{
78 `(influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50) [0-9]{19}`,
79 `(influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1) [0-9]{19}`,
80 `(influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2) [0-9]{19}`,
81 }
82
83 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
84 fmt.Println(err.Error())
85 }
86
87 // Output:
88 // influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50
89 // influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1
90 // influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2
91 }
92
93 func extractAndPrintMessage(expected []string, msg string) error {
94 for _, pattern := range expected {
95 re := regexp.MustCompile(pattern)
96 match := re.FindStringSubmatch(msg)
97 if len(match) != 2 {
98 return fmt.Errorf("Pattern not found! {%s} [%s]: %v\n", pattern, msg, match)
99 }
100 fmt.Println(match[1])
101 }
102 return nil
103 }
2929 }
3030 }
3131
32 func ExampleCounter() {
33 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
34 counter := in.NewCounter("influx_counter")
35 counter.Add(10)
36 counter.With("error", "true").Add(1)
37 counter.With("error", "false").Add(2)
38 counter.Add(50)
39
40 client := &bufWriter{}
41 in.WriteTo(client)
42
43 expectedLines := []string{
44 `(influx_counter,a=b count=60) [0-9]{19}`,
45 `(influx_counter,a=b,error=true count=1) [0-9]{19}`,
46 `(influx_counter,a=b,error=false count=2) [0-9]{19}`,
47 }
48
49 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
50 fmt.Println(err.Error())
51 }
52
53 // Output:
54 // influx_counter,a=b count=60
55 // influx_counter,a=b,error=true count=1
56 // influx_counter,a=b,error=false count=2
57 }
58
5932 func TestGauge(t *testing.T) {
6033 in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
6134 re := regexp.MustCompile(`influx_gauge,foo=alpha value=([0-9\.]+) [0-9]+`)
7043 if err := teststat.TestGauge(gauge, value); err != nil {
7144 t.Fatal(err)
7245 }
73 }
74
75 func ExampleGauge() {
76 in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
77 gauge := in.NewGauge("influx_gauge")
78 gauge.Set(10)
79 gauge.With("error", "true").Set(2)
80 gauge.With("error", "true").Set(1)
81 gauge.With("error", "false").Set(2)
82 gauge.Set(50)
83
84 client := &bufWriter{}
85 in.WriteTo(client)
86
87 expectedLines := []string{
88 `(influx_gauge,a=b value=50) [0-9]{19}`,
89 `(influx_gauge,a=b,error=true value=1) [0-9]{19}`,
90 `(influx_gauge,a=b,error=false value=2) [0-9]{19}`,
91 }
92
93 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
94 fmt.Println(err.Error())
95 }
96
97 // Output:
98 // influx_gauge,a=b value=50
99 // influx_gauge,a=b,error=true value=1
100 // influx_gauge,a=b,error=false value=2
10146 }
10247
10348 func TestHistogram(t *testing.T) {
12065 if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
12166 t.Fatal(err)
12267 }
123 }
124
125 func ExampleHistogram() {
126 in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
127 histogram := in.NewHistogram("influx_histogram")
128 histogram.Observe(float64(10))
129 histogram.With("error", "true").Observe(float64(1))
130 histogram.With("error", "false").Observe(float64(2))
131 histogram.Observe(float64(50))
132
133 client := &bufWriter{}
134 in.WriteTo(client)
135
136 expectedLines := []string{
137 `(influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50) [0-9]{19}`,
138 `(influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1) [0-9]{19}`,
139 `(influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2) [0-9]{19}`,
140 }
141
142 if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
143 fmt.Println(err.Error())
144 }
145
146 // Output:
147 // influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50
148 // influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1
149 // influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2
15068 }
15169
15270 func TestHistogramLabels(t *testing.T) {
17391 }
17492 return nil
17593 }
176
177 func extractAndPrintMessage(expected []string, msg string) error {
178 for _, pattern := range expected {
179 re := regexp.MustCompile(pattern)
180 match := re.FindStringSubmatch(msg)
181 if len(match) != 2 {
182 return fmt.Errorf("Pattern not found! {%s} [%s]: %v\n", pattern, msg, match)
183 }
184 fmt.Println(match[1])
185 }
186 return nil
187 }