Use the product token for the user agent in more locations (#7378)

This commit is contained in:
Daniel Nelson 2020-04-21 10:02:18 -07:00 committed by GitHub
parent c9a3b697b8
commit a4eb9c2205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"
"os/exec"
"regexp"
"testing"
"time"
@ -480,3 +481,11 @@ func TestParseTimestamp(t *testing.T) {
})
}
}
func TestProductToken(t *testing.T) {
token := ProductToken()
// Telegraf version depends on the call to SetVersion, it cannot be set
// multiple times and is not thread-safe.
re := regexp.MustCompile(`^Telegraf/[^\s]+ Go/\d+.\d+.\d+$`)
require.True(t, re.MatchString(token), token)
}

View File

@ -486,7 +486,7 @@ func (s *Solr) gatherData(url string, v interface{}) error {
req.SetBasicAuth(s.Username, s.Password)
}
req.Header.Set("User-Agent", "Telegraf/"+internal.Version())
req.Header.Set("User-Agent", internal.ProductToken())
r, err := s.client.Do(req)
if err != nil {

View File

@ -193,7 +193,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("User-Agent", internal.ProductToken())
req.Header.Set("Content-Type", defaultContentType)
if h.ContentEncoding == "gzip" {
req.Header.Set("Content-Encoding", "gzip")

View File

@ -431,11 +431,9 @@ func TestDefaultUserAgent(t *testing.T) {
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"))
require.Equal(t, internal.ProductToken(), r.Header.Get("User-Agent"))
w.WriteHeader(http.StatusOK)
})

View File

@ -129,7 +129,7 @@ func NewHTTPClient(config HTTPConfig) (*httpClient, error) {
userAgent := config.UserAgent
if userAgent == "" {
userAgent = "Telegraf/" + internal.Version()
userAgent = internal.ProductToken()
}
if config.Headers == nil {

View File

@ -248,8 +248,6 @@ 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
@ -310,7 +308,7 @@ func TestHTTP_Write(t *testing.T) {
Log: testutil.Logger{},
},
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
require.Equal(t, r.Header.Get("User-Agent"), "Telegraf/1.2.3")
require.Equal(t, internal.ProductToken(), r.Header.Get("User-Agent"))
w.WriteHeader(http.StatusNoContent)
},
},

View File

@ -84,7 +84,7 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) {
userAgent := config.UserAgent
if userAgent == "" {
userAgent = "Telegraf/" + internal.Version()
userAgent = internal.ProductToken()
}
var headers = make(map[string]string, len(config.Headers)+2)