fix(outputs.influxdb_v2): Allow overriding auth and agent headers (#16383)
This commit is contained in:
parent
f25992e339
commit
8645ac04a8
|
|
@ -70,17 +70,21 @@ type httpClient struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) Init() error {
|
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 {
|
if c.headers == nil {
|
||||||
c.headers = make(map[string]string, 2)
|
c.headers = make(map[string]string, 2)
|
||||||
}
|
}
|
||||||
c.headers["Authorization"] = "Token " + token.String()
|
|
||||||
token.Destroy()
|
if _, ok := c.headers["Authorization"]; !ok {
|
||||||
c.headers["User-Agent"] = c.userAgent
|
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)
|
var proxy func(*http.Request) (*url.URL, error)
|
||||||
if c.proxy != nil {
|
if c.proxy != nil {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// goos: linux
|
||||||
// goarch: amd64
|
// goarch: amd64
|
||||||
// pkg: github.com/influxdata/telegraf/plugins/outputs/influxdb_v2
|
// pkg: github.com/influxdata/telegraf/plugins/outputs/influxdb_v2
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue