From c2ae7d0a14dedb767a7c038593546613127b87a3 Mon Sep 17 00:00:00 2001 From: Dimitris Rozakis Date: Wed, 13 Sep 2017 14:29:22 +0300 Subject: [PATCH 1/3] Respect path prefix in influx output uri --- plugins/outputs/influxdb/client/http.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/outputs/influxdb/client/http.go b/plugins/outputs/influxdb/client/http.go index d8c1951f5f7b1..8f0a6ac2476fa 100644 --- a/plugins/outputs/influxdb/client/http.go +++ b/plugins/outputs/influxdb/client/http.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "net/url" + "path" "time" ) @@ -305,8 +306,11 @@ func writeURL(u *url.URL, wp WriteParams) string { } u.RawQuery = params.Encode() - u.Path = "write" - return u.String() + p := u.Path + u.Path = path.Join(p, "write") + s := u.String() + u.Path = p + return s } func queryURL(u *url.URL, command string) string { @@ -314,6 +318,9 @@ func queryURL(u *url.URL, command string) string { params.Set("q", command) u.RawQuery = params.Encode() - u.Path = "query" - return u.String() + p := u.Path + u.Path = path.Join(p, "query") + s := u.String() + u.Path = p + return s } From 5eec5c76115a78fad13e98fced41931c06063d53 Mon Sep 17 00:00:00 2001 From: Dimitris Rozakis Date: Wed, 13 Sep 2017 23:28:24 +0300 Subject: [PATCH 2/3] Remove conf comments on strict influx url format Basically reverts fb851ad51732bc51b74d71b77e05d9c77d51e36d. Since path prefixes for http urls are now supported, this clarification isn't needed. --- etc/telegraf.conf | 4 +--- plugins/outputs/influxdb/README.md | 4 +--- plugins/outputs/influxdb/influxdb.go | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 4831f934b3688..c636efe560688 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -84,9 +84,7 @@ # Configuration for influxdb server to send metrics to [[outputs.influxdb]] - ## The HTTP or UDP URL for your InfluxDB instance. Each item should be - ## of the form: - ## scheme "://" host [ ":" port] + ## The full HTTP or UDP URL for your InfluxDB instance. ## ## Multiple urls can be specified as part of the same cluster, ## this means that only ONE of the urls will be written to each interval. diff --git a/plugins/outputs/influxdb/README.md b/plugins/outputs/influxdb/README.md index c6ddf35869114..35f896523c7e9 100644 --- a/plugins/outputs/influxdb/README.md +++ b/plugins/outputs/influxdb/README.md @@ -7,9 +7,7 @@ This plugin writes to [InfluxDB](https://www.influxdb.com) via HTTP or UDP. ```toml # Configuration for influxdb server to send metrics to [[outputs.influxdb]] - ## The HTTP or UDP URL for your InfluxDB instance. Each item should be - ## of the form: - ## scheme "://" host [ ":" port] + ## The full HTTP or UDP URL for your InfluxDB instance. ## ## Multiple urls can be specified as part of the same cluster, ## this means that only ONE of the urls will be written to each interval. diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 5780cb72c3c63..ac5faf455fc06 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -53,9 +53,7 @@ type InfluxDB struct { } var sampleConfig = ` - ## The HTTP or UDP URL for your InfluxDB instance. Each item should be - ## of the form: - ## scheme "://" host [ ":" port] + ## The full HTTP or UDP URL for your InfluxDB instance. ## ## Multiple urls can be specified as part of the same cluster, ## this means that only ONE of the urls will be written to each interval. From d4d3c2d6f5c0f3a087086373ae63fadbeabce9e0 Mon Sep 17 00:00:00 2001 From: Dimitris Rozakis Date: Wed, 13 Sep 2017 23:55:00 +0300 Subject: [PATCH 3/3] Add influx output tests for path prefixes --- plugins/outputs/influxdb/client/http_test.go | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/plugins/outputs/influxdb/client/http_test.go b/plugins/outputs/influxdb/client/http_test.go index d094078ea51b6..ba08918af9de2 100644 --- a/plugins/outputs/influxdb/client/http_test.go +++ b/plugins/outputs/influxdb/client/http_test.go @@ -373,3 +373,39 @@ func TestGzipCompression(t *testing.T) { assert.Equal(t, []byte(influxLine), uncompressed.Bytes()) } + +func TestHTTPClient_PathPrefix(t *testing.T) { + prefix := "/some/random/prefix" + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case prefix + "/write": + w.WriteHeader(http.StatusNoContent) + w.Header().Set("Content-Type", "application/json") + case prefix + "/query": + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `{"results":[{}]}`) + default: + w.WriteHeader(http.StatusNotFound) + msg := fmt.Sprintf("Path not found: %s", r.URL.Path) + fmt.Fprintln(w, msg) + } + })) + defer ts.Close() + + config := HTTPConfig{ + URL: ts.URL + prefix, + } + wp := WriteParams{ + Database: "test", + } + client, err := NewHTTP(config, wp) + defer client.Close() + assert.NoError(t, err) + err = client.Query("CREATE DATABASE test") + assert.NoError(t, err) + _, err = client.Write([]byte("cpu value=99\n")) + assert.NoError(t, err) + _, err = client.WriteStream(bytes.NewReader([]byte("cpu value=99\n")), 13) + assert.NoError(t, err) +}