Skip to content

Commit

Permalink
fix(outputs.influxdb_v2): Allow overriding auth and agent headers (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
DStrand1 authored Jan 9, 2025
1 parent f25992e commit 8645ac0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 12 additions & 8 deletions plugins/outputs/influxdb_v2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions plugins/outputs/influxdb_v2/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8645ac0

Please sign in to comment.