...

Source file src/github.com/go-kit/kit/metrics/influx/example_test.go

Documentation: github.com/go-kit/kit/metrics/influx

     1  package influx
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	influxdb "github.com/influxdata/influxdb1-client/v2"
     8  
     9  	"github.com/go-kit/log"
    10  )
    11  
    12  func ExampleCounter() {
    13  	in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
    14  	counter := in.NewCounter("influx_counter")
    15  	counter.Add(10)
    16  	counter.With("error", "true").Add(1)
    17  	counter.With("error", "false").Add(2)
    18  	counter.Add(50)
    19  
    20  	client := &bufWriter{}
    21  	in.WriteTo(client)
    22  
    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}`,
    27  	}
    28  
    29  	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
    30  		fmt.Println(err.Error())
    31  	}
    32  
    33  	// Output:
    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
    37  }
    38  
    39  func ExampleGauge() {
    40  	in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
    41  	gauge := in.NewGauge("influx_gauge")
    42  	gauge.Set(10)
    43  	gauge.With("error", "true").Set(2)
    44  	gauge.With("error", "true").Set(1)
    45  	gauge.With("error", "false").Set(2)
    46  	gauge.Set(50)
    47  	gauge.With("test", "true").Set(1)
    48  	gauge.With("test", "true").Add(1)
    49  
    50  	client := &bufWriter{}
    51  	in.WriteTo(client)
    52  
    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}`,
    58  	}
    59  
    60  	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
    61  		fmt.Println(err.Error())
    62  	}
    63  
    64  	// Output:
    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
    69  }
    70  
    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))
    78  
    79  	client := &bufWriter{}
    80  	in.WriteTo(client)
    81  
    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}`,
    86  	}
    87  
    88  	if err := extractAndPrintMessage(expectedLines, client.buf.String()); err != nil {
    89  		fmt.Println(err.Error())
    90  	}
    91  
    92  	// Output:
    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
    96  }
    97  
    98  func extractAndPrintMessage(expected []string, msg string) error {
    99  	for _, pattern := range expected {
   100  		re := regexp.MustCompile(pattern)
   101  		match := re.FindStringSubmatch(msg)
   102  		if len(match) != 2 {
   103  			return fmt.Errorf("pattern not found! {%s} [%s]: %v\n", pattern, msg, match)
   104  		}
   105  		fmt.Println(match[1])
   106  	}
   107  	return nil
   108  }
   109  

View as plain text