From 0d9f0e9dea3eaa8cb5028244fb2d54172597f05c Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Wed, 23 Nov 2016 12:21:17 +0000 Subject: [PATCH] 'discard' output plugin --- plugins/outputs/all/all.go | 1 + plugins/outputs/discard/README.md | 12 ++ plugins/outputs/discard/discard.go | 35 +++++ plugins/outputs/discard/discard_test.go | 196 ++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 plugins/outputs/discard/README.md create mode 100644 plugins/outputs/discard/discard.go create mode 100644 plugins/outputs/discard/discard_test.go diff --git a/plugins/outputs/all/all.go b/plugins/outputs/all/all.go index 28354e7e45ad9..96091b2ade894 100644 --- a/plugins/outputs/all/all.go +++ b/plugins/outputs/all/all.go @@ -5,6 +5,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/outputs/amqp" _ "github.com/influxdata/telegraf/plugins/outputs/cloudwatch" _ "github.com/influxdata/telegraf/plugins/outputs/datadog" + _ "github.com/influxdata/telegraf/plugins/outputs/discard" _ "github.com/influxdata/telegraf/plugins/outputs/file" _ "github.com/influxdata/telegraf/plugins/outputs/graphite" _ "github.com/influxdata/telegraf/plugins/outputs/graylog" diff --git a/plugins/outputs/discard/README.md b/plugins/outputs/discard/README.md new file mode 100644 index 0000000000000..e1c70b742450a --- /dev/null +++ b/plugins/outputs/discard/README.md @@ -0,0 +1,12 @@ +# discard Output Plugin + +This output plugin simply drops all metrics that are sent to it. It is only +meant to be used for testing purposes. + +### Configuration: + +```toml +# Send metrics to nowhere at all +[[outputs.discard]] + # no configuration +``` diff --git a/plugins/outputs/discard/discard.go b/plugins/outputs/discard/discard.go new file mode 100644 index 0000000000000..3a066ae215422 --- /dev/null +++ b/plugins/outputs/discard/discard.go @@ -0,0 +1,35 @@ +package discard + +import ( + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/outputs" +) + +type Discard struct { +} + +func (d *Discard) Connect() error { + return nil +} + +func (d *Discard) Close() error { + return nil +} + +func (d *Discard) SampleConfig() string { + return "" +} + +func (d *Discard) Description() string { + return "Send metrics to nowhere at all" +} + +func (d *Discard) Write(metrics []telegraf.Metric) error { + return nil +} + +func init() { + outputs.Add("discard", func() telegraf.Output { + return &Discard{} + }) +} diff --git a/plugins/outputs/discard/discard_test.go b/plugins/outputs/discard/discard_test.go new file mode 100644 index 0000000000000..bc65a71fa6336 --- /dev/null +++ b/plugins/outputs/discard/discard_test.go @@ -0,0 +1,196 @@ +package discard + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/plugins/serializers" + "github.com/influxdata/telegraf/testutil" +) + +const ( + expNewFile = "test1,tag1=value1 value=1 1257894000000000000\n" + expExistFile = "cpu,cpu=cpu0 value=100 1455312810012459582\n" + + "test1,tag1=value1 value=1 1257894000000000000\n" +) + +func TestFileExistingFile(t *testing.T) { + fh := createFile() + s, _ := serializers.NewInfluxSerializer() + f := File{ + Files: []string{fh.Name()}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + validateFile(fh.Name(), expExistFile, t) + + err = f.Close() + assert.NoError(t, err) +} + +func TestFileNewFile(t *testing.T) { + s, _ := serializers.NewInfluxSerializer() + fh := tmpFile() + f := File{ + Files: []string{fh}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + validateFile(fh, expNewFile, t) + + err = f.Close() + assert.NoError(t, err) +} + +func TestFileExistingFiles(t *testing.T) { + fh1 := createFile() + fh2 := createFile() + fh3 := createFile() + + s, _ := serializers.NewInfluxSerializer() + f := File{ + Files: []string{fh1.Name(), fh2.Name(), fh3.Name()}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + validateFile(fh1.Name(), expExistFile, t) + validateFile(fh2.Name(), expExistFile, t) + validateFile(fh3.Name(), expExistFile, t) + + err = f.Close() + assert.NoError(t, err) +} + +func TestFileNewFiles(t *testing.T) { + s, _ := serializers.NewInfluxSerializer() + fh1 := tmpFile() + fh2 := tmpFile() + fh3 := tmpFile() + f := File{ + Files: []string{fh1, fh2, fh3}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + validateFile(fh1, expNewFile, t) + validateFile(fh2, expNewFile, t) + validateFile(fh3, expNewFile, t) + + err = f.Close() + assert.NoError(t, err) +} + +func TestFileBoth(t *testing.T) { + fh1 := createFile() + fh2 := tmpFile() + + s, _ := serializers.NewInfluxSerializer() + f := File{ + Files: []string{fh1.Name(), fh2}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + validateFile(fh1.Name(), expExistFile, t) + validateFile(fh2, expNewFile, t) + + err = f.Close() + assert.NoError(t, err) +} + +func TestFileStdout(t *testing.T) { + // keep backup of the real stdout + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + s, _ := serializers.NewInfluxSerializer() + f := File{ + Files: []string{"stdout"}, + serializer: s, + } + + err := f.Connect() + assert.NoError(t, err) + + err = f.Write(testutil.MockMetrics()) + assert.NoError(t, err) + + err = f.Close() + assert.NoError(t, err) + + outC := make(chan string) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + io.Copy(&buf, r) + outC <- buf.String() + }() + + // back to normal state + w.Close() + // restoring the real stdout + os.Stdout = old + out := <-outC + + assert.Equal(t, expNewFile, out) +} + +func createFile() *os.File { + f, err := ioutil.TempFile("", "") + if err != nil { + panic(err) + } + f.WriteString("cpu,cpu=cpu0 value=100 1455312810012459582\n") + return f +} + +func tmpFile() string { + d, err := ioutil.TempDir("", "") + if err != nil { + panic(err) + } + return d + internal.RandomString(10) +} + +func validateFile(fname, expS string, t *testing.T) { + buf, err := ioutil.ReadFile(fname) + if err != nil { + panic(err) + } + assert.Equal(t, expS, string(buf)) +}