...

Source file src/github.com/go-kit/kit/metrics/graphite/graphite_test.go

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

     1  package graphite
     2  
     3  import (
     4  	"bytes"
     5  	"regexp"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/go-kit/kit/metrics/teststat"
    10  	"github.com/go-kit/log"
    11  )
    12  
    13  func TestCounter(t *testing.T) {
    14  	prefix, name := "abc.", "def"
    15  	label, value := "label", "value" // ignored for Graphite
    16  	regex := `^` + prefix + name + ` ([0-9\.]+) [0-9]+$`
    17  	g := New(prefix, log.NewNopLogger())
    18  	counter := g.NewCounter(name).With(label, value)
    19  	valuef := teststat.SumLines(g, regex)
    20  	if err := teststat.TestCounter(counter, valuef); err != nil {
    21  		t.Fatal(err)
    22  	}
    23  }
    24  
    25  func TestGauge(t *testing.T) {
    26  	prefix, name := "ghi.", "jkl"
    27  	label, value := "xyz", "abc" // ignored for Graphite
    28  	regex := `^` + prefix + name + ` ([0-9\.]+) [0-9]+$`
    29  	g := New(prefix, log.NewNopLogger())
    30  	gauge := g.NewGauge(name).With(label, value)
    31  	valuef := teststat.LastLine(g, regex)
    32  	if err := teststat.TestGauge(gauge, valuef); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  }
    36  
    37  func TestHistogram(t *testing.T) {
    38  	// The histogram test is actually like 4 gauge tests.
    39  	prefix, name := "graphite.", "histogram_test"
    40  	label, value := "abc", "def" // ignored for Graphite
    41  	re50 := regexp.MustCompile(prefix + name + `.p50 ([0-9\.]+) [0-9]+`)
    42  	re90 := regexp.MustCompile(prefix + name + `.p90 ([0-9\.]+) [0-9]+`)
    43  	re95 := regexp.MustCompile(prefix + name + `.p95 ([0-9\.]+) [0-9]+`)
    44  	re99 := regexp.MustCompile(prefix + name + `.p99 ([0-9\.]+) [0-9]+`)
    45  	g := New(prefix, log.NewNopLogger())
    46  	histogram := g.NewHistogram(name, 50).With(label, value)
    47  	quantiles := func() (float64, float64, float64, float64) {
    48  		var buf bytes.Buffer
    49  		g.WriteTo(&buf)
    50  		match50 := re50.FindStringSubmatch(buf.String())
    51  		p50, _ := strconv.ParseFloat(match50[1], 64)
    52  		match90 := re90.FindStringSubmatch(buf.String())
    53  		p90, _ := strconv.ParseFloat(match90[1], 64)
    54  		match95 := re95.FindStringSubmatch(buf.String())
    55  		p95, _ := strconv.ParseFloat(match95[1], 64)
    56  		match99 := re99.FindStringSubmatch(buf.String())
    57  		p99, _ := strconv.ParseFloat(match99[1], 64)
    58  		return p50, p90, p95, p99
    59  	}
    60  	if err := teststat.TestHistogram(histogram, quantiles, 0.01); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  }
    64  

View as plain text