Exemple #1
0
func TestSparkline(t *testing.T) {
	t.Parallel()

	i, err := New("_", "")
	assert.Nil(t, err)

	buf := util.NewRingBuffer(120)
	for i := 1; i <= 100; i++ {
		buf.Add(float64(i))
	}
	src := &mockDisplayable{buf}

	output := buildSparkline(i.Host, "memory:rss", func(family, name string) displayable {
		return src
	})

	expected := "localhost memory:rss min 1.00m max 100.00m avg 50.50m\n▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▃▃▃▃▃▃▃▃▄▄▄▄▄▄▄▄▄▄▄▄▅▅▅▅▅▅▅▅▅▅▅▅▆▆▆▆▆▆▆▆▆▆▆▆▆▇▇▇▇▇▇▇▇▇▇▇▇█████████████\n"
	assert.Equal(t, output, expected)

	// alternate, CLI friendy metric naming format
	expected = "localhost memory:rss min 1.00m max 100.00m avg 50.50m\n▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▃▃▃▃▃▃▃▃▄▄▄▄▄▄▄▄▄▄▄▄▅▅▅▅▅▅▅▅▅▅▅▅▆▆▆▆▆▆▆▆▆▆▆▆▆▇▇▇▇▇▇▇▇▇▇▇▇█████████████\n"
	output = buildSparkline(i.Host, "memory:rss", func(family, name string) displayable {
		return src
	})
	assert.Equal(t, output, expected)

	// invalid metric
	output = buildSparkline(i.Host, "memory:xxx", func(family, name string) displayable {
		return nil
	})
	assert.Equal(t, output, "Unknown metric: memory:xxx\n")
}
Exemple #2
0
func (store *storage) DeclareCounter(familyName string, name string, xform TransformFunc, display DisplayFunc) {
	fam := store.tree[familyName]
	if fam == nil {
		store.tree[familyName] = &family{familyName, false, map[string]Metric{}}
		fam = store.tree[familyName]
	}

	data := fam.metrics[name]
	if data == nil {
		fam.metrics[name] = &counter{util.NewRingBuffer(Slots), xform, display}
		data = fam.metrics[name]
	}
}
Exemple #3
0
func (store *storage) DeclareGauge(familyName string, name string, display DisplayFunc) {
	fam := store.tree[familyName]
	if fam == nil {
		store.tree[familyName] = &family{familyName, false, map[string]Metric{}}
		fam = store.tree[familyName]
	}

	data := fam.metrics[name]
	if data == nil {
		fam.metrics[name] = &gauge{util.NewRingBuffer(Slots), display}
		data = fam.metrics[name]
	}
}