From 60dc19fe69a1ad69ee05f9acee266bedb2d246b5 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Tue, 15 Dec 2015 22:12:47 -0800 Subject: [PATCH] Rename influxdbjson to influxdb --- plugins/all/all.go | 2 +- plugins/{influxdbjson => influxdb}/README.md | 12 +-- .../influxdb_json.go => influxdb/influxdb.go} | 77 ++++++++----------- .../influxdb_test.go} | 6 +- 4 files changed, 43 insertions(+), 54 deletions(-) rename plugins/{influxdbjson => influxdb}/README.md (73%) rename plugins/{influxdbjson/influxdb_json.go => influxdb/influxdb.go} (63%) rename plugins/{influxdbjson/influxdb_json_test.go => influxdb/influxdb_test.go} (93%) diff --git a/plugins/all/all.go b/plugins/all/all.go index c280f6bf4..8b4e754be 100644 --- a/plugins/all/all.go +++ b/plugins/all/all.go @@ -9,7 +9,7 @@ import ( _ "github.com/influxdb/telegraf/plugins/exec" _ "github.com/influxdb/telegraf/plugins/haproxy" _ "github.com/influxdb/telegraf/plugins/httpjson" - _ "github.com/influxdb/telegraf/plugins/influxdbjson" + _ "github.com/influxdb/telegraf/plugins/influxdb" _ "github.com/influxdb/telegraf/plugins/jolokia" _ "github.com/influxdb/telegraf/plugins/kafka_consumer" _ "github.com/influxdb/telegraf/plugins/leofs" diff --git a/plugins/influxdbjson/README.md b/plugins/influxdb/README.md similarity index 73% rename from plugins/influxdbjson/README.md rename to plugins/influxdb/README.md index f26c937f2..c55aa1fc8 100644 --- a/plugins/influxdbjson/README.md +++ b/plugins/influxdb/README.md @@ -1,11 +1,11 @@ -# influxdbjson plugin +# influxdb plugin -The influxdbjson plugin collects InfluxDB-formatted data from JSON endpoints. +The influxdb plugin collects InfluxDB-formatted data from JSON endpoints. With a configuration of: ```toml -[[plugins.influxdbjson]] +[[plugins.influxdb]] name = "produce" urls = [ "http://127.0.0.1:8086/debug/vars", @@ -58,10 +58,10 @@ And if 192.168.2.1 responds like so: Then the collected metrics will be: ``` -influxdbjson_produce_fruit,url='http://127.0.0.1:8086/debug/vars',kind='apple' inventory=371.0,sold=112.0 -influxdbjson_produce_fruit,url='http://127.0.0.1:8086/debug/vars',kind='banana' inventory=1000.0,sold=403.0 +influxdb_produce_fruit,url='http://127.0.0.1:8086/debug/vars',kind='apple' inventory=371.0,sold=112.0 +influxdb_produce_fruit,url='http://127.0.0.1:8086/debug/vars',kind='banana' inventory=1000.0,sold=403.0 -influxdbjson_produce_transactions,url='http://192.168.2.1:8086/debug/vars' total=100.0,balance=184.75 +influxdb_produce_transactions,url='http://192.168.2.1:8086/debug/vars' total=100.0,balance=184.75 ``` There are two important details to note about the collected metrics: diff --git a/plugins/influxdbjson/influxdb_json.go b/plugins/influxdb/influxdb.go similarity index 63% rename from plugins/influxdbjson/influxdb_json.go rename to plugins/influxdb/influxdb.go index 61d966e47..a261018ae 100644 --- a/plugins/influxdbjson/influxdb_json.go +++ b/plugins/influxdb/influxdb.go @@ -1,8 +1,9 @@ -package influxdbjson +package influxdb import ( "encoding/json" "errors" + "fmt" "net/http" "strings" "sync" @@ -10,59 +11,41 @@ import ( "github.com/influxdb/telegraf/plugins" ) -type InfluxDBJSON struct { +type InfluxDB struct { Name string URLs []string `toml:"urls"` } -func (*InfluxDBJSON) Description() string { +func (*InfluxDB) Description() string { return "Read InfluxDB-formatted JSON metrics from one or more HTTP endpoints" } -func (*InfluxDBJSON) SampleConfig() string { +func (*InfluxDB) SampleConfig() string { return ` - # Reads InfluxDB-formatted JSON from given URLs. For example, - # monitoring a URL which responded with a JSON object formatted like this: - # - # { - # "(ignored_key)": { - # "name": "connections", - # "tags": { - # "host": "foo" - # }, - # "values": { - # "avg_ms": 1.234, - # } - # } - # } - # - # with configuration of { name = "server", urls = ["http://127.0.0.1:8086/x"] } - # - # Would result in this recorded metric: - # - # influxdbjson_server_connections,url='http://127.0.0.1:8086/x',host='foo' avg_ms=1.234 - [[plugins.influxdbjson]] - # Name to use for measurement - name = "influxdb" + # Reads InfluxDB-formatted JSON from given URLs. + # Works with InfluxDB debug endpoints out of the box, but other services can use this format too. + # See the influxdb plugin's README for more details. + [[plugins.influxdb]] + # Name to use for measurement + name = "influxdb" - # Multiple URLs from which to read InfluxDB-formatted JSON - urls = [ - "http://localhost:8086/debug/vars" - ] + # Multiple URLs from which to read InfluxDB-formatted JSON + urls = [ + "http://localhost:8086/debug/vars" + ] ` } -func (i *InfluxDBJSON) Gather(acc plugins.Accumulator) error { - var wg sync.WaitGroup - +func (i *InfluxDB) Gather(acc plugins.Accumulator) error { errorChannel := make(chan error, len(i.URLs)) + var wg sync.WaitGroup for _, u := range i.URLs { wg.Add(1) go func(url string) { defer wg.Done() if err := i.gatherURL(acc, url); err != nil { - errorChannel <- err + errorChannel <- fmt.Errorf("[name=%s][url=%s]: %s", i.Name, url, err) } }(u) } @@ -70,15 +53,17 @@ func (i *InfluxDBJSON) Gather(acc plugins.Accumulator) error { wg.Wait() close(errorChannel) - // Get all errors and return them as one giant error - errorStrings := []string{} + // If there weren't any errors, we can return nil now. + if len(errorChannel) == 0 { + return nil + } + + // There were errors, so join them all together as one big error. + errorStrings := make([]string, 0, len(errorChannel)) for err := range errorChannel { errorStrings = append(errorStrings, err.Error()) } - if len(errorStrings) == 0 { - return nil - } return errors.New(strings.Join(errorStrings, "\n")) } @@ -95,7 +80,7 @@ type point struct { // // Returns: // error: Any error that may have occurred -func (i *InfluxDBJSON) gatherURL( +func (i *InfluxDB) gatherURL( acc plugins.Accumulator, url string, ) error { @@ -105,7 +90,11 @@ func (i *InfluxDBJSON) gatherURL( } defer resp.Body.Close() - // Can't predict what all is going to be in the response, so decode the top keys one at a time. + // It would be nice to be able to decode into a map[string]point, but + // we'll get a decoder error like: + // `json: cannot unmarshal array into Go value of type influxdb.point` + // if any of the values aren't objects. + // To avoid that error, we decode by hand. dec := json.NewDecoder(resp.Body) // Parse beginning of object @@ -155,7 +144,7 @@ func (i *InfluxDBJSON) gatherURL( } func init() { - plugins.Add("influxdbjson", func() plugins.Plugin { - return &InfluxDBJSON{} + plugins.Add("influxdb", func() plugins.Plugin { + return &InfluxDB{} }) } diff --git a/plugins/influxdbjson/influxdb_json_test.go b/plugins/influxdb/influxdb_test.go similarity index 93% rename from plugins/influxdbjson/influxdb_json_test.go rename to plugins/influxdb/influxdb_test.go index 1f3663e20..f5035bc7e 100644 --- a/plugins/influxdbjson/influxdb_json_test.go +++ b/plugins/influxdb/influxdb_test.go @@ -1,11 +1,11 @@ -package influxdbjson_test +package influxdb_test import ( "net/http" "net/http/httptest" "testing" - "github.com/influxdb/telegraf/plugins/influxdbjson" + "github.com/influxdb/telegraf/plugins/influxdb" "github.com/influxdb/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -64,7 +64,7 @@ func TestBasic(t *testing.T) { })) defer fakeServer.Close() - plugin := &influxdbjson.InfluxDBJSON{ + plugin := &influxdb.InfluxDB{ Name: "test", URLs: []string{fakeServer.URL + "/endpoint"}, }