address further code review comments
This commit is contained in:
parent
7843c3a3dd
commit
4b424a2a73
|
@ -17,7 +17,7 @@ This is a sample configuration for the plugin.
|
||||||
## Address and port to host HTTP listener on
|
## Address and port to host HTTP listener on
|
||||||
service_address = ":8186"
|
service_address = ":8186"
|
||||||
|
|
||||||
## timeouts in seconds
|
## timeouts
|
||||||
read_timeout = "10"
|
read_timeout = "10s"
|
||||||
write_timeout = "10"
|
write_timeout = "10s"
|
||||||
```
|
```
|
||||||
|
|
|
@ -5,11 +5,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs/http_listener/stoppableListener"
|
"github.com/influxdata/telegraf/plugins/inputs/http_listener/stoppableListener"
|
||||||
"github.com/influxdata/telegraf/plugins/parsers"
|
"github.com/influxdata/telegraf/plugins/parsers"
|
||||||
|
@ -17,8 +17,8 @@ import (
|
||||||
|
|
||||||
type HttpListener struct {
|
type HttpListener struct {
|
||||||
ServiceAddress string
|
ServiceAddress string
|
||||||
ReadTimeout string
|
ReadTimeout internal.Duration
|
||||||
WriteTimeout string
|
WriteTimeout internal.Duration
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ const sampleConfig = `
|
||||||
## Address and port to host HTTP listener on
|
## Address and port to host HTTP listener on
|
||||||
service_address = ":8186"
|
service_address = ":8186"
|
||||||
|
|
||||||
## timeouts in seconds
|
## timeouts
|
||||||
read_timeout = "10"
|
read_timeout = "10s"
|
||||||
write_timeout = "10"
|
write_timeout = "10s"
|
||||||
`
|
`
|
||||||
|
|
||||||
func (t *HttpListener) SampleConfig() string {
|
func (t *HttpListener) SampleConfig() string {
|
||||||
|
@ -90,19 +90,17 @@ func (t *HttpListener) Stop() {
|
||||||
// httpListen listens for HTTP requests.
|
// httpListen listens for HTTP requests.
|
||||||
func (t *HttpListener) httpListen() error {
|
func (t *HttpListener) httpListen() error {
|
||||||
|
|
||||||
readTimeout, err := strconv.ParseInt(t.ReadTimeout, 10, 32)
|
if t.ReadTimeout.Duration < time.Second {
|
||||||
if err != nil {
|
t.ReadTimeout.Duration = time.Second * 10
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
writeTimeout, err := strconv.ParseInt(t.WriteTimeout, 10, 32)
|
if t.WriteTimeout.Duration < time.Second {
|
||||||
if err != nil {
|
t.WriteTimeout.Duration = time.Second * 10
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var server = http.Server{
|
var server = http.Server{
|
||||||
Handler: t,
|
Handler: t,
|
||||||
ReadTimeout: time.Duration(readTimeout) * time.Second,
|
ReadTimeout: t.ReadTimeout.Duration,
|
||||||
WriteTimeout: time.Duration(writeTimeout) * time.Second,
|
WriteTimeout: t.WriteTimeout.Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.Serve(t.listener)
|
return server.Serve(t.listener)
|
||||||
|
@ -113,8 +111,8 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)
|
log.Printf("Problem reading request: [%s], Error: %s\n", string(body), err)
|
||||||
res.WriteHeader(http.StatusInternalServerError)
|
http.Error(res, "ERROR reading request", http.StatusInternalServerError)
|
||||||
res.Write([]byte("ERROR reading request"))
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = req.URL.Path[1:]
|
var path = req.URL.Path[1:]
|
||||||
|
@ -125,11 +123,9 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.storeMetrics(metrics)
|
t.storeMetrics(metrics)
|
||||||
res.WriteHeader(http.StatusNoContent)
|
res.WriteHeader(http.StatusNoContent)
|
||||||
res.Write([]byte(""))
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err)
|
log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err)
|
||||||
res.WriteHeader(http.StatusInternalServerError)
|
http.Error(res, "ERROR parsing metrics", http.StatusInternalServerError)
|
||||||
res.Write([]byte("ERROR parsing metrics"))
|
|
||||||
}
|
}
|
||||||
} else if path == "query" {
|
} else if path == "query" {
|
||||||
// Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a query
|
// Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a query
|
||||||
|
@ -139,8 +135,7 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Write([]byte("{\"results\":[]}"))
|
res.Write([]byte("{\"results\":[]}"))
|
||||||
} else {
|
} else {
|
||||||
// Don't know how to respond to calls to other endpoints
|
// Don't know how to respond to calls to other endpoints
|
||||||
res.WriteHeader(http.StatusNotFound)
|
http.NotFound(res, req)
|
||||||
res.Write([]byte("Not Found"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/plugins/parsers"
|
"github.com/influxdata/telegraf/plugins/parsers"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
|
|
||||||
|
@ -29,8 +30,8 @@ cpu_load_short,host=server06 value=12.0 1422568543702900257
|
||||||
func newTestHttpListener() *HttpListener {
|
func newTestHttpListener() *HttpListener {
|
||||||
listener := &HttpListener{
|
listener := &HttpListener{
|
||||||
ServiceAddress: ":8186",
|
ServiceAddress: ":8186",
|
||||||
ReadTimeout: "10",
|
ReadTimeout: internal.Duration{Duration: time.Second * 10},
|
||||||
WriteTimeout: "10",
|
WriteTimeout: internal.Duration{Duration: time.Second * 10},
|
||||||
}
|
}
|
||||||
return listener
|
return listener
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue