Skip to content

Commit f7e85eb

Browse files
glintondanielnelson
authored andcommitted
Add basic auth to prometheus input plugin (#6062)
1 parent 5bad2c3 commit f7e85eb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

plugins/inputs/prometheus/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ in Prometheus format.
3333
## OR
3434
# bearer_token_string = "abc_123"
3535

36+
## HTTP Basic Authentication username and password. ('bearer_token' and
37+
## 'bearer_token_string' take priority)
38+
# username = ""
39+
# password = ""
40+
3641
## Specify timeout duration for slower prometheus clients (default is 3s)
3742
# response_timeout = "3s"
3843

plugins/inputs/prometheus/prometheus.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Prometheus struct {
3434
BearerToken string `toml:"bearer_token"`
3535
BearerTokenString string `toml:"bearer_token_string"`
3636

37+
// Basic authentication credentials
38+
Username string `toml:"username"`
39+
Password string `toml:"password"`
40+
3741
ResponseTimeout internal.Duration `toml:"response_timeout"`
3842

3943
tls.ClientConfig
@@ -75,7 +79,12 @@ var sampleConfig = `
7579
## OR
7680
# bearer_token_string = "abc_123"
7781
78-
## Specify timeout duration for slower prometheus clients (default is 3s)
82+
## HTTP Basic Authentication username and password. ('bearer_token' and
83+
## 'bearer_token_string' take priority)
84+
# username = ""
85+
# password = ""
86+
87+
## Specify timeout duration for slower prometheus clients (default is 3s)
7988
# response_timeout = "3s"
8089
8190
## Optional TLS Config
@@ -251,6 +260,8 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
251260
req.Header.Set("Authorization", "Bearer "+string(token))
252261
} else if p.BearerTokenString != "" {
253262
req.Header.Set("Authorization", "Bearer "+p.BearerTokenString)
263+
} else if p.Username != "" || p.Password != "" {
264+
req.SetBasicAuth(p.Username, p.Password)
254265
}
255266

256267
var resp *http.Response

0 commit comments

Comments
 (0)