Skip to content

Commit

Permalink
Support passing bearer token directly in prometheus input (#5294)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and danielnelson committed Jan 15, 2019
1 parent a7b443c commit d759b46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 4 additions & 2 deletions plugins/inputs/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ in Prometheus format.
## - prometheus.io/port: If port is not 9102 use this annotation
# monitor_kubernetes_pods = true

## Use bearer token for authorization
# bearer_token = /path/to/bearer/token
## Use bearer token for authorization. ('bearer_token' takes priority)
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"

## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"
Expand Down
14 changes: 9 additions & 5 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type Prometheus struct {
KubeConfig string

// Bearer Token authorization file path
BearerToken string `toml:"bearer_token"`
BearerToken string `toml:"bearer_token"`
BearerTokenString string `toml:"bearer_token_string"`

ResponseTimeout internal.Duration `toml:"response_timeout"`

Expand Down Expand Up @@ -65,8 +66,10 @@ var sampleConfig = `
## - prometheus.io/port: If port is not 9102 use this annotation
# monitor_kubernetes_pods = true
## Use bearer token for authorization
# bearer_token = /path/to/bearer/token
## Use bearer token for authorization. ('bearer_token' takes priority)
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"
## Specify timeout duration for slower prometheus clients (default is 3s)
# response_timeout = "3s"
Expand Down Expand Up @@ -230,13 +233,14 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error

req.Header.Add("Accept", acceptHeader)

var token []byte
if p.BearerToken != "" {
token, err = ioutil.ReadFile(p.BearerToken)
token, err := ioutil.ReadFile(p.BearerToken)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+string(token))
} else if p.BearerTokenString != "" {
req.Header.Set("Authorization", "Bearer "+p.BearerTokenString)
}

var resp *http.Response
Expand Down

0 comments on commit d759b46

Please sign in to comment.