0.3.0 httpjson

This commit is contained in:
Cameron Sparr 2015-12-14 16:57:17 -06:00
parent 40d84accee
commit 319c363c8e
1 changed files with 14 additions and 18 deletions

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/influxdb/telegraf/internal"
"github.com/influxdb/telegraf/plugins" "github.com/influxdb/telegraf/plugins"
) )
@ -154,7 +155,19 @@ func (h *HttpJson) gatherServer(
delete(jsonOut, tag) delete(jsonOut, tag)
} }
processResponse(acc, service.Name, tags, jsonOut) f := internal.JSONFlattener{}
err = f.FlattenJSON("", jsonOut)
if err != nil {
return err
}
var msrmnt_name string
if service.Name == "" {
msrmnt_name = "httpjson"
} else {
msrmnt_name = "httpjson_" + service.Name
}
acc.AddFields(msrmnt_name, f.Fields, nil)
return nil return nil
} }
@ -209,23 +222,6 @@ func (h *HttpJson) sendRequest(service Service, serverURL string) (string, error
return string(body), err return string(body), err
} }
// Flattens the map generated from the JSON object and stores its float values using a
// plugins.Accumulator. It ignores any non-float values.
// Parameters:
// acc: the Accumulator to use
// prefix: What the name of the measurement name should be prefixed by.
// tags: telegraf tags to
func processResponse(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) {
switch t := v.(type) {
case map[string]interface{}:
for k, v := range t {
processResponse(acc, prefix+"_"+k, tags, v)
}
case float64:
acc.Add(prefix, v, tags)
}
}
func init() { func init() {
plugins.Add("httpjson", func() plugins.Plugin { plugins.Add("httpjson", func() plugins.Plugin {
return &HttpJson{client: RealHTTPClient{client: &http.Client{}}} return &HttpJson{client: RealHTTPClient{client: &http.Client{}}}