fix(outputs.influxdb_v2): Allow overriding auth and agent headers (#16383)

This commit is contained in:
Dane Strandboge 2025-01-09 03:00:36 -07:00 committed by GitHub
parent f25992e339
commit 8645ac04a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 8 deletions

View File

@ -70,17 +70,21 @@ type httpClient struct {
}
func (c *httpClient) Init() error {
token, err := c.token.Get()
if err != nil {
return fmt.Errorf("getting token failed: %w", err)
}
if c.headers == nil {
c.headers = make(map[string]string, 2)
}
c.headers["Authorization"] = "Token " + token.String()
token.Destroy()
c.headers["User-Agent"] = c.userAgent
if _, ok := c.headers["Authorization"]; !ok {
token, err := c.token.Get()
if err != nil {
return fmt.Errorf("getting token failed: %w", err)
}
c.headers["Authorization"] = "Token " + token.String()
token.Destroy()
}
if _, ok := c.headers["User-Agent"]; !ok {
c.headers["User-Agent"] = c.userAgent
}
var proxy func(*http.Request) (*url.URL, error)
if c.proxy != nil {

View File

@ -187,6 +187,22 @@ func TestExponentialBackoffCalculationWithRetryAfter(t *testing.T) {
}
}
func TestHeadersDoNotOverrideConfig(t *testing.T) {
testURL, err := url.Parse("https://localhost:8181")
require.NoError(t, err)
c := &httpClient{
headers: map[string]string{
"Authorization": "Bearer foo",
"User-Agent": "foo",
},
// URL to make Init() happy
url: testURL,
}
require.NoError(t, c.Init())
require.Equal(t, "Bearer foo", c.headers["Authorization"])
require.Equal(t, "foo", c.headers["User-Agent"])
}
// goos: linux
// goarch: amd64
// pkg: github.com/influxdata/telegraf/plugins/outputs/influxdb_v2