feat(inputs.prometheus): Add support for custom header (#12050)

This commit is contained in:
Charly Nanfack 2022-11-21 15:55:21 -05:00 committed by GitHub
parent 6f407c5949
commit d4eda21742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 32 deletions

View File

@ -98,6 +98,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# username = ""
# password = ""
## Optional custom HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}
## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"

View File

@ -58,6 +58,8 @@ type Prometheus struct {
Username string `toml:"username"`
Password string `toml:"password"`
HTTPHeaders map[string]string `toml:"http_headers"`
ResponseTimeout config.Duration `toml:"response_timeout"`
MetricVersion int `toml:"metric_version"`
@ -298,6 +300,12 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
req.SetBasicAuth(p.Username, p.Password)
}
if p.HTTPHeaders != nil {
for key, value := range p.HTTPHeaders {
req.Header.Add(key, value)
}
}
var resp *http.Response
if u.URL.Scheme != "unix" {
//nolint:bodyclose // False positive (because of if-else) - body will be closed in `defer`

View File

@ -81,6 +81,9 @@
# username = ""
# password = ""
## Optional custom HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}
## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"