| 1 |
package influx |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"regexp" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
influxdb "github.com/influxdata/influxdb1-client/v2" |
| 12 |
|
| 13 |
"github.com/go-kit/kit/metrics/teststat" |
| 14 |
"github.com/go-kit/log" |
| 15 |
) |
| 16 |
|
| 17 |
func TestCounter(t *testing.T) { |
| 18 |
in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger()) |
| 19 |
re := regexp.MustCompile(`influx_counter,a=b count=([0-9\.]+) [0-9]+`) // reverse-engineered :\ |
| 20 |
counter := in.NewCounter("influx_counter") |
| 21 |
value := func() float64 { |
| 22 |
client := &bufWriter{} |
| 23 |
in.WriteTo(client) |
| 24 |
match := re.FindStringSubmatch(client.buf.String()) |
| 25 |
f, _ := strconv.ParseFloat(match[1], 64) |
| 26 |
return f |
| 27 |
} |
| 28 |
if err := teststat.TestCounter(counter, value); err != nil { |
| 29 |
t.Fatal(err) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestGauge(t *testing.T) { |
| 34 |
in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger()) |
| 35 |
re := regexp.MustCompile(`influx_gauge,foo=alpha value=([0-9\.]+) [0-9]+`) |
| 36 |
gauge := in.NewGauge("influx_gauge") |
| 37 |
value := func() []float64 { |
| 38 |
client := &bufWriter{} |
| 39 |
in.WriteTo(client) |
| 40 |
match := re.FindStringSubmatch(client.buf.String()) |
| 41 |
f, _ := strconv.ParseFloat(match[1], 64) |
| 42 |
return []float64{f} |
| 43 |
} |
| 44 |
if err := teststat.TestGauge(gauge, value); err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestHistogram(t *testing.T) { |
| 50 |
in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger()) |
| 51 |
re := regexp.MustCompile(`influx_histogram,bar=beta,foo=alpha p50=([0-9\.]+),p90=([0-9\.]+),p95=([0-9\.]+),p99=([0-9\.]+) [0-9]+`) |
| 52 |
histogram := in.NewHistogram("influx_histogram").With("bar", "beta") |
| 53 |
quantiles := func() (float64, float64, float64, float64) { |
| 54 |
w := &bufWriter{} |
| 55 |
in.WriteTo(w) |
| 56 |
match := re.FindStringSubmatch(w.buf.String()) |
| 57 |
if len(match) != 5 { |
| 58 |
t.Errorf("These are not the quantiles you're looking for: %v\n", match) |
| 59 |
} |
| 60 |
var result [4]float64 |
| 61 |
for i, q := range match[1:] { |
| 62 |
result[i], _ = strconv.ParseFloat(q, 64) |
| 63 |
} |
| 64 |
return result[0], result[1], result[2], result[3] |
| 65 |
} |
| 66 |
if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestHistogramLabels(t *testing.T) { |
| 72 |
in := New(map[string]string{}, influxdb.BatchPointsConfig{}, log.NewNopLogger()) |
| 73 |
h := in.NewHistogram("foo") |
| 74 |
h.Observe(123) |
| 75 |
h.With("abc", "xyz").Observe(456) |
| 76 |
w := &bufWriter{} |
| 77 |
if err := in.WriteTo(w); err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
if want, have := 2, len(strings.Split(strings.TrimSpace(w.buf.String()), "\n")); want != have { |
| 81 |
t.Errorf("want %d, have %d", want, have) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestIssue404(t *testing.T) { |
| 86 |
in := New(map[string]string{}, influxdb.BatchPointsConfig{}, log.NewNopLogger()) |
| 87 |
|
| 88 |
counterOne := in.NewCounter("influx_counter_one").With("a", "b") |
| 89 |
counterOne.Add(123) |
| 90 |
|
| 91 |
counterTwo := in.NewCounter("influx_counter_two").With("c", "d") |
| 92 |
counterTwo.Add(456) |
| 93 |
|
| 94 |
w := &bufWriter{} |
| 95 |
in.WriteTo(w) |
| 96 |
|
| 97 |
lines := strings.Split(strings.TrimSpace(w.buf.String()), "\n") |
| 98 |
if want, have := 2, len(lines); want != have { |
| 99 |
t.Fatalf("want %d, have %d", want, have) |
| 100 |
} |
| 101 |
for _, line := range lines { |
| 102 |
if strings.HasPrefix(line, "influx_counter_one") { |
| 103 |
if !strings.HasPrefix(line, "influx_counter_one,a=b count=123 ") { |
| 104 |
t.Errorf("invalid influx_counter_one: %s", line) |
| 105 |
} |
| 106 |
} else if strings.HasPrefix(line, "influx_counter_two") { |
| 107 |
if !strings.HasPrefix(line, "influx_counter_two,c=d count=456 ") { |
| 108 |
t.Errorf("invalid influx_counter_two: %s", line) |
| 109 |
} |
| 110 |
} else { |
| 111 |
t.Errorf("unexpected line: %s", line) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
type bufWriter struct { |
| 117 |
buf bytes.Buffer |
| 118 |
} |
| 119 |
|
| 120 |
func (w *bufWriter) Write(bp influxdb.BatchPoints) error { |
| 121 |
for _, p := range bp.Points() { |
| 122 |
fmt.Fprintf(&w.buf, p.String()+"\n") |
| 123 |
} |
| 124 |
return nil |
| 125 |
} |