Пример #1
0
func TestCounter(t *testing.T) {
	s := httptest.NewServer(stdprometheus.UninstrumentedHandler())
	defer s.Close()

	scrape := func() string {
		resp, _ := http.Get(s.URL)
		buf, _ := ioutil.ReadAll(resp.Body)
		return string(buf)
	}

	namespace, subsystem, name := "ns", "ss", "foo"
	re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + `{alpha="alpha-value",beta="beta-value"} ([0-9\.]+)`)

	counter := NewCounterFrom(stdprometheus.CounterOpts{
		Namespace: namespace,
		Subsystem: subsystem,
		Name:      name,
		Help:      "This is the help string.",
	}, []string{"alpha", "beta"}).With("beta", "beta-value", "alpha", "alpha-value") // order shouldn't matter

	value := func() float64 {
		matches := re.FindStringSubmatch(scrape())
		f, _ := strconv.ParseFloat(matches[1], 64)
		return f
	}

	if err := teststat.TestCounter(counter, value); err != nil {
		t.Fatal(err)
	}
}
Пример #2
0
func TestCounter(t *testing.T) {
	prefix, name := "abc.", "def"
	label, value := "label", "value" // ignored
	regex := `^` + prefix + name + `:([0-9\.]+)\|c$`
	s := New(prefix, log.NewNopLogger())
	counter := s.NewCounter(name, 1.0).With(label, value)
	valuef := teststat.SumLines(s, regex)
	if err := teststat.TestCounter(counter, valuef); err != nil {
		t.Fatal(err)
	}
}
Пример #3
0
func TestCounter(t *testing.T) {
	prefix, name := "abc.", "def"
	label, value := "label", "value" // ignored for Graphite
	regex := `^` + prefix + name + ` ([0-9\.]+) [0-9]+$`
	g := New(prefix, log.NewNopLogger())
	counter := g.NewCounter(name).With(label, value)
	valuef := teststat.SumLines(g, regex)
	if err := teststat.TestCounter(counter, valuef); err != nil {
		t.Fatal(err)
	}
}
Пример #4
0
func TestCounter(t *testing.T) {
	in := New(map[string]string{"a": "b"}, influxdb.BatchPointsConfig{}, log.NewNopLogger())
	re := regexp.MustCompile(`influx_counter,a=b count=([0-9\.]+) [0-9]+`) // reverse-engineered :\
	counter := in.NewCounter("influx_counter")
	value := func() float64 {
		client := &bufWriter{}
		in.WriteTo(client)
		match := re.FindStringSubmatch(client.buf.String())
		f, _ := strconv.ParseFloat(match[1], 64)
		return f
	}
	if err := teststat.TestCounter(counter, value); err != nil {
		t.Fatal(err)
	}
}
Пример #5
0
func TestCounter(t *testing.T) {
	// The only way to extract values from Circonus is to pose as a Circonus
	// server and receive real HTTP writes.
	const name = "abc"
	var val int64
	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var res map[string]struct {
			Value int64 `json:"_value"` // reverse-engineered :\
		}
		json.NewDecoder(r.Body).Decode(&res)
		val = res[name].Value
	}))
	defer s.Close()

	// Set up a Circonus object, submitting to our HTTP server.
	m := newCirconusMetrics(s.URL)
	counter := New(m).NewCounter(name).With("label values", "not supported")
	value := func() float64 { m.Flush(); return float64(val) }

	// Engage.
	if err := teststat.TestCounter(counter, value); err != nil {
		t.Fatal(err)
	}
}