support query endpoint and change default listen port
This commit is contained in:
parent
6b5a9d34bb
commit
6e791357ef
|
@ -5,7 +5,7 @@ The plugin expects messages in the InfluxDB line-protocol ONLY, other Telegraf i
|
||||||
The intent of the plugin is to allow Telegraf to serve as a proxy/router for the /write endpoint of the InfluxDB HTTP API.
|
The intent of the plugin is to allow Telegraf to serve as a proxy/router for the /write endpoint of the InfluxDB HTTP API.
|
||||||
|
|
||||||
See: [Telegraf Input Data Formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md#influx).
|
See: [Telegraf Input Data Formats](https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md#influx).
|
||||||
Example: curl -i -XPOST 'http://localhost:8086/write' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
|
Example: curl -i -XPOST 'http://localhost:8186/write' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
|
||||||
|
|
||||||
### Configuration:
|
### Configuration:
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ This is a sample configuration for the plugin.
|
||||||
# # Influx HTTP write listener
|
# # Influx HTTP write listener
|
||||||
[[inputs.http_listener]]
|
[[inputs.http_listener]]
|
||||||
## Address and port to host HTTP listener on
|
## Address and port to host HTTP listener on
|
||||||
service_address = ":8086"
|
service_address = ":8186"
|
||||||
|
|
||||||
## timeouts in seconds
|
## timeouts in seconds
|
||||||
read_timeout = "10"
|
read_timeout = "10"
|
||||||
|
|
|
@ -31,7 +31,7 @@ type HttpListener struct {
|
||||||
|
|
||||||
const sampleConfig = `
|
const sampleConfig = `
|
||||||
## Address and port to host HTTP listener on
|
## Address and port to host HTTP listener on
|
||||||
service_address = ":8086"
|
service_address = ":8186"
|
||||||
|
|
||||||
## timeouts in seconds
|
## timeouts in seconds
|
||||||
read_timeout = "10"
|
read_timeout = "10"
|
||||||
|
@ -103,23 +103,37 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
body, err := ioutil.ReadAll(req.Body)
|
body, err := ioutil.ReadAll(req.Body)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Printf("Received request: [%s]\n", string(body))
|
var path = req.URL.Path[1:]
|
||||||
|
|
||||||
var metrics []telegraf.Metric
|
if path == "write" {
|
||||||
if len(body) == 0 {
|
log.Printf("Received write request: [%s]\n", string(body))
|
||||||
log.Printf("No metrics to parse\n")
|
|
||||||
} else {
|
var metrics []telegraf.Metric
|
||||||
metrics, err = t.parser.Parse(body)
|
if len(body) == 0 {
|
||||||
if err == nil {
|
log.Printf("No metrics to parse\n")
|
||||||
t.storeMetrics(metrics)
|
|
||||||
log.Printf("Persisted %d metrics\n", len(metrics))
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err)
|
metrics, err = t.parser.Parse(body)
|
||||||
res.WriteHeader(500)
|
if err == nil {
|
||||||
res.Write([]byte("ERROR parsing metrics"))
|
t.storeMetrics(metrics)
|
||||||
|
log.Printf("Persisted %d metrics\n", len(metrics))
|
||||||
|
} else {
|
||||||
|
log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err)
|
||||||
|
res.WriteHeader(500)
|
||||||
|
res.Write([]byte("ERROR parsing metrics"))
|
||||||
|
}
|
||||||
|
res.WriteHeader(204)
|
||||||
|
res.Write([]byte(""))
|
||||||
}
|
}
|
||||||
res.WriteHeader(204)
|
} else if path == "query" {
|
||||||
res.Write([]byte(""))
|
// Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a query
|
||||||
|
log.Printf("Received query request: [%s]\n", string(body))
|
||||||
|
res.WriteHeader(200)
|
||||||
|
res.Write([]byte("{\"results\":[]}"))
|
||||||
|
} else {
|
||||||
|
// Don't know how to respond to calls to other endpoints
|
||||||
|
log.Printf("Received unknown %s request: [%s]\n", path, string(body))
|
||||||
|
res.WriteHeader(404)
|
||||||
|
res.Write([]byte("Not Found"))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)
|
log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)
|
||||||
|
|
Loading…
Reference in New Issue