Skip to content

Commit

Permalink
Add basic auth to prometheus input plugin (#6062)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and danielnelson committed Jul 2, 2019
1 parent 5bad2c3 commit f7e85eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions plugins/inputs/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ in Prometheus format.
## OR
# bearer_token_string = "abc_123"

## HTTP Basic Authentication username and password. ('bearer_token' and
## 'bearer_token_string' take priority)
# username = ""
# password = ""

## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"

Expand Down
13 changes: 12 additions & 1 deletion plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type Prometheus struct {
BearerToken string `toml:"bearer_token"`
BearerTokenString string `toml:"bearer_token_string"`

// Basic authentication credentials
Username string `toml:"username"`
Password string `toml:"password"`

ResponseTimeout internal.Duration `toml:"response_timeout"`

tls.ClientConfig
Expand Down Expand Up @@ -75,7 +79,12 @@ var sampleConfig = `
## OR
# bearer_token_string = "abc_123"
## Specify timeout duration for slower prometheus clients (default is 3s)
## HTTP Basic Authentication username and password. ('bearer_token' and
## 'bearer_token_string' take priority)
# username = ""
# password = ""
## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"
## Optional TLS Config
Expand Down Expand Up @@ -251,6 +260,8 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
req.Header.Set("Authorization", "Bearer "+string(token))
} else if p.BearerTokenString != "" {
req.Header.Set("Authorization", "Bearer "+p.BearerTokenString)
} else if p.Username != "" || p.Password != "" {
req.SetBasicAuth(p.Username, p.Password)
}

var resp *http.Response
Expand Down

0 comments on commit f7e85eb

Please sign in to comment.