File: example_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (108 lines) | stat: -rw-r--r-- 3,094 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
package influx

import (
	"fmt"
	"regexp"

	influxdb "github.com/influxdata/influxdb1-client/v2"

	"github.com/go-kit/log"
)

func ExampleCounter() {
	in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
	counter := in.NewCounter("influx_counter")
	counter.Add(10)
	counter.With("error", "true").Add(1)
	counter.With("error", "false").Add(2)
	counter.Add(50)

	client := &bufWriter{}
	in.WriteTo(client)

	expectedLines := []string{
		`(influx_counter,a=b count=60) [0-9]{19}`,
		`(influx_counter,a=b,error=true count=1) [0-9]{19}`,
		`(influx_counter,a=b,error=false count=2) [0-9]{19}`,
	}

	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
		fmt.Println(err.Error())
	}

	// Output:
	// influx_counter,a=b count=60
	// influx_counter,a=b,error=true count=1
	// influx_counter,a=b,error=false count=2
}

func ExampleGauge() {
	in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
	gauge := in.NewGauge("influx_gauge")
	gauge.Set(10)
	gauge.With("error", "true").Set(2)
	gauge.With("error", "true").Set(1)
	gauge.With("error", "false").Set(2)
	gauge.Set(50)
	gauge.With("test", "true").Set(1)
	gauge.With("test", "true").Add(1)

	client := &bufWriter{}
	in.WriteTo(client)

	expectedLines := []string{
		`(influx_gauge,a=b,test=true value=2) [0-9]{19}`,
		`(influx_gauge,a=b value=50) [0-9]{19}`,
		`(influx_gauge,a=b,error=true value=1) [0-9]{19}`,
		`(influx_gauge,a=b,error=false value=2) [0-9]{19}`,
	}

	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
		fmt.Println(err.Error())
	}

	// Output:
	// influx_gauge,a=b,test=true value=2
	// influx_gauge,a=b value=50
	// influx_gauge,a=b,error=true value=1
	// influx_gauge,a=b,error=false value=2
}

func ExampleHistogram() {
	in := New(map[string]string{"foo": "alpha"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
	histogram := in.NewHistogram("influx_histogram")
	histogram.Observe(float64(10))
	histogram.With("error", "true").Observe(float64(1))
	histogram.With("error", "false").Observe(float64(2))
	histogram.Observe(float64(50))

	client := &bufWriter{}
	in.WriteTo(client)

	expectedLines := []string{
		`(influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50) [0-9]{19}`,
		`(influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1) [0-9]{19}`,
		`(influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2) [0-9]{19}`,
	}

	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
		fmt.Println(err.Error())
	}

	// Output:
	// influx_histogram,foo=alpha p50=10,p90=50,p95=50,p99=50
	// influx_histogram,error=true,foo=alpha p50=1,p90=1,p95=1,p99=1
	// influx_histogram,error=false,foo=alpha p50=2,p90=2,p95=2,p99=2
}

func extractAndPrintMessage(expected []string, msg string) error {
	for _, pattern := range expected {
		re := regexp.MustCompile(pattern)
		match := re.FindStringSubmatch(msg)
		if len(match) != 2 {
			return fmt.Errorf("pattern not found! {%s} [%s]: %v\n", pattern, msg, match)
		}
		fmt.Println(match[1])
	}
	return nil
}