Add telegraf version to User-Agent header (#4838)
Header is added in influxdb, influxdb_v2, and http outputs.
This commit is contained in:
committed by
Daniel Nelson
parent
502d9ab499
commit
44fd74d688
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user