From 5d05046c4526b6ef1985f7732b10c9b51aab805f Mon Sep 17 00:00:00 2001 From: Kevin Conaway Date: Wed, 10 Oct 2018 09:49:24 -0400 Subject: [PATCH] Issue #3911 Add telegraf version to User-Agent header for http requests --- plugins/outputs/http/http.go | 1 + plugins/outputs/http/http_test.go | 31 +++++++++++++++++++++++++++ plugins/outputs/influxdb/http.go | 8 +++---- plugins/outputs/influxdb/http_test.go | 14 ++++++++++++ plugins/outputs/influxdb_v2/http.go | 3 +-- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/plugins/outputs/http/http.go b/plugins/outputs/http/http.go index 8393d049926ae..abcea74b57d0f 100644 --- a/plugins/outputs/http/http.go +++ b/plugins/outputs/http/http.go @@ -187,6 +187,7 @@ func (h *HTTP) write(reqBody []byte) error { req.SetBasicAuth(h.Username, h.Password) } + req.Header.Set("User-Agent", "Telegraf/"+internal.Version()) req.Header.Set("Content-Type", defaultContentType) if h.ContentEncoding == "gzip" { req.Header.Set("Content-Encoding", "gzip") diff --git a/plugins/outputs/http/http_test.go b/plugins/outputs/http/http_test.go index 5b314cceb8aa4..0decdf0246c02 100644 --- a/plugins/outputs/http/http_test.go +++ b/plugins/outputs/http/http_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/serializers/influx" "github.com/stretchr/testify/require" @@ -422,3 +423,33 @@ func TestOAuthClientCredentialsGrant(t *testing.T) { }) } } + +func TestDefaultUserAgent(t *testing.T) { + ts := httptest.NewServer(http.NotFoundHandler()) + defer ts.Close() + + u, err := url.Parse(fmt.Sprintf("http://%s", ts.Listener.Addr().String())) + require.NoError(t, err) + + internal.SetVersion("1.2.3") + + t.Run("default-user-agent", func(t *testing.T) { + ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "Telegraf/1.2.3", r.Header.Get("User-Agent")) + w.WriteHeader(http.StatusOK) + }) + + client := &HTTP{ + URL: u.String(), + Method: defaultMethod, + } + + serializer := influx.NewSerializer() + client.SetSerializer(serializer) + err = client.Connect() + require.NoError(t, err) + + err = client.Write([]telegraf.Metric{getMetric()}) + require.NoError(t, err) + }) +} diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index f32ad79a44ac6..236d0432192ae 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -27,10 +27,8 @@ const ( ) const ( - defaultRequestTimeout = time.Second * 5 - defaultDatabase = "telegraf" - defaultUserAgent = "telegraf" - + defaultRequestTimeout = time.Second * 5 + defaultDatabase = "telegraf" errStringDatabaseNotFound = "database not found" errStringHintedHandoffNotEmpty = "hinted handoff queue not empty" errStringPartialWrite = "partial write" @@ -138,7 +136,7 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = defaultUserAgent + userAgent = "Telegraf/" + internal.Version() } var headers = make(map[string]string, len(config.Headers)+1) diff --git a/plugins/outputs/influxdb/http_test.go b/plugins/outputs/influxdb/http_test.go index 30cc1f8b69252..fa648f0f821b3 100644 --- a/plugins/outputs/influxdb/http_test.go +++ b/plugins/outputs/influxdb/http_test.go @@ -18,6 +18,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" "github.com/stretchr/testify/require" @@ -246,6 +247,8 @@ func TestHTTP_Write(t *testing.T) { u, err := url.Parse(fmt.Sprintf("http://%s", ts.Listener.Addr().String())) require.NoError(t, err) + internal.SetVersion("1.2.3") + tests := []struct { name string config *influxdb.HTTPConfig @@ -295,6 +298,17 @@ func TestHTTP_Write(t *testing.T) { w.WriteHeader(http.StatusNoContent) }, }, + { + name: "default user agent", + config: &influxdb.HTTPConfig{ + URL: u, + Database: "telegraf", + }, + queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) { + require.Equal(t, r.Header.Get("User-Agent"), "Telegraf/1.2.3") + w.WriteHeader(http.StatusNoContent) + }, + }, { name: "default database", config: &influxdb.HTTPConfig{ diff --git a/plugins/outputs/influxdb_v2/http.go b/plugins/outputs/influxdb_v2/http.go index 12826ff92b387..9858b5c2a56b4 100644 --- a/plugins/outputs/influxdb_v2/http.go +++ b/plugins/outputs/influxdb_v2/http.go @@ -40,7 +40,6 @@ const ( defaultRequestTimeout = time.Second * 5 defaultMaxWait = 10 // seconds defaultDatabase = "telegraf" - defaultUserAgent = "telegraf" ) type HTTPConfig struct { @@ -82,7 +81,7 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) { userAgent := config.UserAgent if userAgent == "" { - userAgent = defaultUserAgent + userAgent = "Telegraf/" + internal.Version() } var headers = make(map[string]string, len(config.Headers)+2)