29 | 29 |
}
|
30 | 30 |
}
|
31 | 31 |
|
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 | |
|
59 | 32 |
func TestGauge(t *testing.T) {
|
60 | 33 |
in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
|
61 | 34 |
re := regexp.MustCompile(`influx_gauge,foo=alpha value=([0-9\.]+) [0-9]+`)
|
|
70 | 43 |
if err := teststat.TestGauge(gauge, value); err != nil {
|
71 | 44 |
t.Fatal(err)
|
72 | 45 |
}
|
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
|
101 | 46 |
}
|
102 | 47 |
|
103 | 48 |
func TestHistogram(t *testing.T) {
|
|
120 | 65 |
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
|
121 | 66 |
t.Fatal(err)
|
122 | 67 |
}
|
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
|
150 | 68 |
}
|
151 | 69 |
|
152 | 70 |
func TestHistogramLabels(t *testing.T) {
|
|
173 | 91 |
}
|
174 | 92 |
return nil
|
175 | 93 |
}
|
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 | |
}
|