Exemple #1
0
func TestWrite(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	o := &OpenTSDB{
		Host:   testutil.GetLocalHost(),
		Port:   4242,
		Prefix: "prefix.test.",
	}

	// Verify that we can connect to the OpenTSDB instance
	err := o.Connect()
	require.NoError(t, err)

	// Verify that we can successfully write data to OpenTSDB
	err = o.Write(testutil.MockMetrics())
	require.NoError(t, err)

	// Verify postive and negative test cases of writing data
	metrics := testutil.MockMetrics()
	metrics = append(metrics, testutil.TestMetric(float64(1.0), "justametric.float"))
	metrics = append(metrics, testutil.TestMetric(int64(123456789), "justametric.int"))
	metrics = append(metrics, testutil.TestMetric(uint64(123456789012345), "justametric.uint"))
	metrics = append(metrics, testutil.TestMetric("Lorem Ipsum", "justametric.string"))
	metrics = append(metrics, testutil.TestMetric(float64(42.0), "justametric.anotherfloat"))

	err = o.Write(metrics)
	require.NoError(t, err)

}
func TestRunningProcessor_WithNameDrop(t *testing.T) {
	inmetrics := []telegraf.Metric{
		testutil.TestMetric(1, "foo"),
		testutil.TestMetric(1, "bar"),
		testutil.TestMetric(1, "baz"),
	}

	expectedNames := []string{
		"foo",
		"baz",
		"baz",
	}
	rfp := NewTestRunningProcessor()

	rfp.Config.Filter.NameDrop = []string{"foo"}
	assert.NoError(t, rfp.Config.Filter.Compile())

	filteredMetrics := rfp.Apply(inmetrics...)

	actualNames := []string{
		filteredMetrics[0].Name(),
		filteredMetrics[1].Name(),
		filteredMetrics[2].Name(),
	}
	assert.Equal(t, expectedNames, actualNames)
}
Exemple #3
0
func TestFilter_ShouldMetricsPass(t *testing.T) {
	m := testutil.TestMetric(1, "testmetric")
	f := Filter{
		NameDrop: []string{"foobar"},
	}
	require.NoError(t, f.CompileFilter())
	require.True(t, f.ShouldMetricPass(m))

	m = testutil.TestMetric(1, "foobar")
	require.False(t, f.ShouldMetricPass(m))
}
Exemple #4
0
func BenchmarkAddMetrics(b *testing.B) {
	buf := NewBuffer(10000)
	m := testutil.TestMetric(1, "mymetric")
	for n := 0; n < b.N; n++ {
		buf.Add(m)
	}
}
// Apply renames:
//   "foo" to "fuz"
//   "bar" to "baz"
// And it also drops measurements named "dropme"
func (f *TestProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric {
	out := make([]telegraf.Metric, 0)
	for _, m := range in {
		switch m.Name() {
		case "foo":
			out = append(out, testutil.TestMetric(1, "fuz"))
		case "bar":
			out = append(out, testutil.TestMetric(1, "baz"))
		case "dropme":
			// drop the metric!
		default:
			out = append(out, m)
		}
	}
	return out
}
Exemple #6
0
func BuildTags(t *testing.T) {
	testMetric := testutil.TestMetric(0.0, "test1")
	graphiteSerializer := graphite.GraphiteSerializer{}
	tags, err := graphiteSerializer.Serialize(testMetric)
	fmt.Printf("Tags: %v", tags)
	require.NoError(t, err)
}
Exemple #7
0
// Test that each tag becomes one dimension
func TestBuildDimensions(t *testing.T) {
	const MaxDimensions = 10

	assert := assert.New(t)

	testPoint := testutil.TestMetric(1)
	dimensions := BuildDimensions(testPoint.Tags())

	tagKeys := make([]string, len(testPoint.Tags()))
	i := 0
	for k, _ := range testPoint.Tags() {
		tagKeys[i] = k
		i += 1
	}

	sort.Strings(tagKeys)

	if len(testPoint.Tags()) >= MaxDimensions {
		assert.Equal(MaxDimensions, len(dimensions), "Number of dimensions should be less than MaxDimensions")
	} else {
		assert.Equal(len(testPoint.Tags()), len(dimensions), "Number of dimensions should be equal to number of tags")
	}

	for i, key := range tagKeys {
		if i >= 10 {
			break
		}
		assert.Equal(key, *dimensions[i].Name, "Key should be equal")
		assert.Equal(testPoint.Tags()[key], *dimensions[i].Value, "Value should be equal")
	}
}
func TestRunningProcessor_DroppedMetric(t *testing.T) {
	inmetrics := []telegraf.Metric{
		testutil.TestMetric(1, "dropme"),
		testutil.TestMetric(1, "foo"),
		testutil.TestMetric(1, "bar"),
	}

	expectedNames := []string{
		"fuz",
		"baz",
	}
	rfp := NewTestRunningProcessor()
	filteredMetrics := rfp.Apply(inmetrics...)

	actualNames := []string{
		filteredMetrics[0].Name(),
		filteredMetrics[1].Name(),
	}
	assert.Equal(t, expectedNames, actualNames)
}
Exemple #9
0
func TestVerifyValue(t *testing.T) {
	var tagtests = []struct {
		ptIn        telegraf.Metric
		validMetric bool
	}{
		{
			testutil.TestMetric(float32(11234.5), "test1"),
			true,
		},
		{
			testutil.TestMetric("11234.5", "test2"),
			false,
		},
	}
	for _, tt := range tagtests {
		ok := verifyValue(tt.ptIn.Fields()["value"])
		if tt.validMetric != ok {
			t.Errorf("%s: verification failed\n", tt.ptIn.Name())
		}
	}
}
Exemple #10
0
func TestNewBufferBasicFuncs(t *testing.T) {
	b := NewBuffer(10)

	assert.True(t, b.IsEmpty())
	assert.Zero(t, b.Len())
	assert.Zero(t, b.Drops())
	assert.Zero(t, b.Total())

	m := testutil.TestMetric(1, "mymetric")
	b.Add(m)
	assert.False(t, b.IsEmpty())
	assert.Equal(t, b.Len(), 1)
	assert.Equal(t, b.Drops(), 0)
	assert.Equal(t, b.Total(), 1)

	b.Add(metricList...)
	assert.False(t, b.IsEmpty())
	assert.Equal(t, b.Len(), 6)
	assert.Equal(t, b.Drops(), 0)
	assert.Equal(t, b.Total(), 6)
}
Exemple #11
0
func BenchmarkHttpSend(b *testing.B) {
	const BatchSize = 50
	const MetricsCount = 4 * BatchSize
	metrics := make([]telegraf.Metric, MetricsCount)
	for i := 0; i < MetricsCount; i++ {
		metrics[i] = testutil.TestMetric(1.0)
	}

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintln(w, "{}")
	}))
	defer ts.Close()

	u, err := url.Parse(ts.URL)
	if err != nil {
		panic(err)
	}

	_, p, _ := net.SplitHostPort(u.Host)

	port, err := strconv.Atoi(p)
	if err != nil {
		panic(err)
	}

	o := &OpenTSDB{
		Host:          ts.URL,
		Port:          port,
		Prefix:        "",
		HttpBatchSize: BatchSize,
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		o.Write(metrics)
	}
}
Exemple #12
0
// Test that metrics with valid values have a MetricDatum created where as non valid do not.
// Skips "time.Time" type as something is converting the value to string.
func TestBuildMetricDatums(t *testing.T) {
	assert := assert.New(t)

	validMetrics := []telegraf.Metric{
		testutil.TestMetric(1),
		testutil.TestMetric(int32(1)),
		testutil.TestMetric(int64(1)),
		testutil.TestMetric(float64(1)),
		testutil.TestMetric(true),
	}

	for _, point := range validMetrics {
		datums := BuildMetricDatum(point)
		assert.Equal(1, len(datums), "Valid type should create a Datum")
	}

	nonValidPoint := testutil.TestMetric("Foo")

	assert.Equal(0, len(BuildMetricDatum(nonValidPoint)), "Invalid type should not create a Datum")
}
package models

import (
	"fmt"
	"sync"
	"testing"

	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/testutil"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var first5 = []telegraf.Metric{
	testutil.TestMetric(101, "metric1"),
	testutil.TestMetric(101, "metric2"),
	testutil.TestMetric(101, "metric3"),
	testutil.TestMetric(101, "metric4"),
	testutil.TestMetric(101, "metric5"),
}

var next5 = []telegraf.Metric{
	testutil.TestMetric(101, "metric6"),
	testutil.TestMetric(101, "metric7"),
	testutil.TestMetric(101, "metric8"),
	testutil.TestMetric(101, "metric9"),
	testutil.TestMetric(101, "metric10"),
}

// Benchmark adding metrics.
Exemple #14
0
package buffer

import (
	"testing"

	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/testutil"

	"github.com/stretchr/testify/assert"
)

var metricList = []telegraf.Metric{
	testutil.TestMetric(2, "mymetric1"),
	testutil.TestMetric(1, "mymetric2"),
	testutil.TestMetric(11, "mymetric3"),
	testutil.TestMetric(15, "mymetric4"),
	testutil.TestMetric(8, "mymetric5"),
}

func BenchmarkAddMetrics(b *testing.B) {
	buf := NewBuffer(10000)
	m := testutil.TestMetric(1, "mymetric")
	for n := 0; n < b.N; n++ {
		buf.Add(m)
	}
}

func TestNewBufferBasicFuncs(t *testing.T) {
	b := NewBuffer(10)

	assert.True(t, b.IsEmpty())
Exemple #15
0
func TestBuildGauge(t *testing.T) {
	var gaugeTests = []struct {
		ptIn     telegraf.Metric
		outGauge *Gauge
		err      error
	}{
		{
			testutil.TestMetric(0.0, "test1"),
			&Gauge{
				Name:        "test1",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       0.0,
			},
			nil,
		},
		{
			testutil.TestMetric(1.0, "test2"),
			&Gauge{
				Name:        "test2",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       1.0,
			},
			nil,
		},
		{
			testutil.TestMetric(10, "test3"),
			&Gauge{
				Name:        "test3",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       10.0,
			},
			nil,
		},
		{
			testutil.TestMetric(int32(112345), "test4"),
			&Gauge{
				Name:        "test4",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       112345.0,
			},
			nil,
		},
		{
			testutil.TestMetric(int64(112345), "test5"),
			&Gauge{
				Name:        "test5",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       112345.0,
			},
			nil,
		},
		{
			testutil.TestMetric(float32(11234.5), "test6"),
			&Gauge{
				Name:        "test6",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       11234.5,
			},
			nil,
		},
		{
			testutil.TestMetric("11234.5", "test7"),
			&Gauge{
				Name:        "test7",
				MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
				Value:       11234.5,
			},
			fmt.Errorf("unable to extract value from Fields, undeterminable type"),
		},
	}

	l := NewLibrato(fakeUrl)
	for _, gt := range gaugeTests {
		gauges, err := l.buildGauges(gt.ptIn)
		if err != nil && gt.err == nil {
			t.Errorf("%s: unexpected error, %+v\n", gt.ptIn.Name(), err)
		}
		if gt.err != nil && err == nil {
			t.Errorf("%s: expected an error (%s) but none returned",
				gt.ptIn.Name(), gt.err.Error())
		}
		if len(gauges) == 0 {
			continue
		}
		if gt.err == nil && !reflect.DeepEqual(gauges[0], gt.outGauge) {
			t.Errorf("%s: \nexpected %+v\ngot %+v\n",
				gt.ptIn.Name(), gt.outGauge, gauges[0])
		}
	}
}
Exemple #16
0
func TestBuildPoint(t *testing.T) {
	var tagtests = []struct {
		ptIn  telegraf.Metric
		outPt Point
		err   error
	}{
		{
			testutil.TestMetric(0.0, "test1"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				0.0,
			},
			nil,
		},
		{
			testutil.TestMetric(1.0, "test2"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				1.0,
			},
			nil,
		},
		{
			testutil.TestMetric(10, "test3"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				10.0,
			},
			nil,
		},
		{
			testutil.TestMetric(int32(112345), "test4"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				112345.0,
			},
			nil,
		},
		{
			testutil.TestMetric(int64(112345), "test5"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				112345.0,
			},
			nil,
		},
		{
			testutil.TestMetric(float32(11234.5), "test6"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				11234.5,
			},
			nil,
		},
		{
			testutil.TestMetric("11234.5", "test7"),
			Point{
				float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
				11234.5,
			},
			fmt.Errorf("unable to extract value from Fields, undeterminable type"),
		},
	}
	for _, tt := range tagtests {
		pt, err := buildMetrics(tt.ptIn)
		if err != nil && tt.err == nil {
			t.Errorf("%s: unexpected error, %+v\n", tt.ptIn.Name(), err)
		}
		if tt.err != nil && err == nil {
			t.Errorf("%s: expected an error (%s) but none returned", tt.ptIn.Name(), tt.err.Error())
		}
		if !reflect.DeepEqual(pt["value"], tt.outPt) && tt.err == nil {
			t.Errorf("%s: \nexpected %+v\ngot %+v\n",
				tt.ptIn.Name(), tt.outPt, pt["value"])
		}
	}
}