From cb951ebd282b9479e53c67de50d9a0a3409c75ee Mon Sep 17 00:00:00 2001 From: palkan Date: Sat, 17 Oct 2015 14:38:50 +0300 Subject: [PATCH] Add httpjson readme closes #275 --- plugins/httpjson/README.md | 69 +++++++++++++++++++++++++++++++ plugins/httpjson/httpjson.go | 10 ++--- plugins/httpjson/httpjson_test.go | 1 - 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 plugins/httpjson/README.md diff --git a/plugins/httpjson/README.md b/plugins/httpjson/README.md new file mode 100644 index 000000000..fd8bcb4cb --- /dev/null +++ b/plugins/httpjson/README.md @@ -0,0 +1,69 @@ +# HTTP JSON Plugin + +The httpjson plugin can collect data from remote URLs which respond with JSON. Then it flattens JSON and finds all numeric values, treating them as floats. + +For example, if you have a service called _mycollector_, which has HTTP endpoint for gathering stats http://my.service.com/_stats: + +``` +[[httpjson.services]] + name = "mycollector" + + servers = [ + "http://my.service.com/_stats" + ] + + # HTTP method to use (case-sensitive) + method = "GET" +``` + +The name is used as a prefix for the measurements. + +The `method` specifies HTTP method to use for requests. + +You can specify which keys from server response should be considered as tags: + +``` +[[httpjson.services]] + ... + + tag_keys = [ + "role", + "version" + ] +``` + +**NOTE**: tag values should be strings. + +You can also specify additional request parameters for the service: + +``` +[[httpjson.services]] + ... + + [httpjson.services.parameters] + event_type = "cpu_spike" + threshold = "0.75" + +``` + + +# Sample + +Let's say that we have a service named "mycollector", which responds with: +```json +{ + "a": 0.5, + "b": { + "c": "some text", + "d": 0.1, + "e": 5 + } +} +``` + +The collected metrics will be: +``` +httpjson_mycollector_a value=0.5 +httpjson_mycollector_b_d value=0.1 +httpjson_mycollector_b_e value=5 +``` diff --git a/plugins/httpjson/httpjson.go b/plugins/httpjson/httpjson.go index 5c9bee02a..a3f02e65f 100644 --- a/plugins/httpjson/httpjson.go +++ b/plugins/httpjson/httpjson.go @@ -22,7 +22,7 @@ type Service struct { Name string Servers []string Method string - TagKeys []string + TagKeys []string Parameters map[string]string } @@ -62,8 +62,8 @@ var sampleConfig = ` # HTTP method to use (case-sensitive) method = "GET" - # List of tag names to extract from server response - # tagKeys = [ + # List of tag names to extract from top-level of JSON server response + # tag_keys = [ # "my_tag_1", # "my_tag_2" # ] @@ -144,8 +144,8 @@ func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, server for _, tag := range service.TagKeys { switch v := jsonOut[tag].(type) { - case string: - tags[tag] = v + case string: + tags[tag] = v } delete(jsonOut, tag) } diff --git a/plugins/httpjson/httpjson_test.go b/plugins/httpjson/httpjson_test.go index 335568724..8f9bfe3ac 100644 --- a/plugins/httpjson/httpjson_test.go +++ b/plugins/httpjson/httpjson_test.go @@ -35,7 +35,6 @@ const validJSONTags = ` "build": "123" }` - const invalidJSON = "I don't think this is JSON" const empty = ""