From befc9061676fa6a583bab4d826245ae5f7a3f8ef Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 2 Nov 2015 16:19:37 -0700 Subject: [PATCH] Improve the HTTP JSON plugin README with more examples. --- plugins/httpjson/README.md | 101 ++++++++++++++++++++--- plugins/httpjson/httpjson.go | 6 +- plugins/kafka_consumer/kafka_consumer.go | 14 +++- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/plugins/httpjson/README.md b/plugins/httpjson/README.md index fd8bcb4cb..d016633a7 100644 --- a/plugins/httpjson/README.md +++ b/plugins/httpjson/README.md @@ -2,7 +2,8 @@ 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: +For example, if you have a service called _mycollector_, which has HTTP endpoint for gathering stats at http://my.service.com/_stats, you would configure the HTTP JSON +plugin like this: ``` [[httpjson.services]] @@ -16,11 +17,11 @@ For example, if you have a service called _mycollector_, which has HTTP endpoint method = "GET" ``` -The name is used as a prefix for the measurements. +`name` is used as a prefix for the measurements. -The `method` specifies HTTP method to use for requests. +`method` specifies HTTP method to use for requests. -You can specify which keys from server response should be considered as tags: +You can also specify which keys from server response should be considered tags: ``` [[httpjson.services]] @@ -32,8 +33,6 @@ You can specify which keys from server response should be considered as tags: ] ``` -**NOTE**: tag values should be strings. - You can also specify additional request parameters for the service: ``` @@ -47,11 +46,30 @@ You can also specify additional request parameters for the service: ``` -# Sample +# Example: + +Let's say that we have a service named "mycollector" configured like this: + +``` +[httpjson] + [[httpjson.services]] + name = "mycollector" + + servers = [ + "http://my.service.com/_stats" + ] + + # HTTP method to use (case-sensitive) + method = "GET" + + tag_keys = ["service"] +``` + +which responds with the following JSON: -Let's say that we have a service named "mycollector", which responds with: ```json { + "service": "service01", "a": 0.5, "b": { "c": "some text", @@ -63,7 +81,68 @@ Let's say that we have a service named "mycollector", which responds with: The collected metrics will be: ``` -httpjson_mycollector_a value=0.5 -httpjson_mycollector_b_d value=0.1 -httpjson_mycollector_b_e value=5 +httpjson_mycollector_a,service='service01',server='http://my.service.com/_stats' value=0.5 +httpjson_mycollector_b_d,service='service01',server='http://my.service.com/_stats' value=0.1 +httpjson_mycollector_b_e,service='service01',server='http://my.service.com/_stats' value=5 +``` + +# Example 2, Multiple Services: + +There is also the option to collect JSON from multiple services, here is an +example doing that. + +``` +[httpjson] + [[httpjson.services]] + name = "mycollector1" + + servers = [ + "http://my.service1.com/_stats" + ] + + # HTTP method to use (case-sensitive) + method = "GET" + + [[httpjson.services]] + name = "mycollector2" + + servers = [ + "http://service.net/json/stats" + ] + + # HTTP method to use (case-sensitive) + method = "POST" +``` + +The services respond with the following JSON: + +mycollector1: +```json +{ + "a": 0.5, + "b": { + "c": "some text", + "d": 0.1, + "e": 5 + } +} +``` + +mycollector2: +```json +{ + "load": 100, + "users": 1335 +} +``` + +The collected metrics will be: + +``` +httpjson_mycollector1_a,server='http://my.service.com/_stats' value=0.5 +httpjson_mycollector1_b_d,server='http://my.service.com/_stats' value=0.1 +httpjson_mycollector1_b_e,server='http://my.service.com/_stats' value=5 + +httpjson_mycollector2_load,server='http://service.net/json/stats' value=100 +httpjson_mycollector2_users,server='http://service.net/json/stats' value=1335 ``` diff --git a/plugins/httpjson/httpjson.go b/plugins/httpjson/httpjson.go index a3f02e65f..b5913e2e1 100644 --- a/plugins/httpjson/httpjson.go +++ b/plugins/httpjson/httpjson.go @@ -127,7 +127,11 @@ func (h *HttpJson) Gather(acc plugins.Accumulator) error { // // Returns: // error: Any error that may have occurred -func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, serverURL string) error { +func (h *HttpJson) gatherServer( + acc plugins.Accumulator, + service Service, + serverURL string, +) error { resp, err := h.sendRequest(service, serverURL) if err != nil { return err diff --git a/plugins/kafka_consumer/kafka_consumer.go b/plugins/kafka_consumer/kafka_consumer.go index 7c1258944..1c0fbe266 100644 --- a/plugins/kafka_consumer/kafka_consumer.go +++ b/plugins/kafka_consumer/kafka_consumer.go @@ -74,7 +74,11 @@ func (k *Kafka) Gather(acc plugins.Accumulator) error { k.Consumer.Close() }() - go readFromKafka(k.Consumer.Messages(), metricQueue, k.BatchSize, k.Consumer.CommitUpto, halt) + go readFromKafka(k.Consumer.Messages(), + metricQueue, + k.BatchSize, + k.Consumer.CommitUpto, + halt) } return emitMetrics(k, acc, metricQueue) @@ -105,7 +109,13 @@ const millisecond = 1000000 * time.Nanosecond type ack func(*sarama.ConsumerMessage) error -func readFromKafka(kafkaMsgs <-chan *sarama.ConsumerMessage, metricProducer chan<- []byte, maxBatchSize int, ackMsg ack, halt <-chan bool) { +func readFromKafka( + kafkaMsgs <-chan *sarama.ConsumerMessage, + metricProducer chan<- []byte, + maxBatchSize int, + ackMsg ack, + halt <-chan bool, +) { batch := make([]byte, 0) currentBatchSize := 0 timeout := time.After(500 * millisecond)