Skip to content

Commit 78f648f

Browse files
author
Jonathan Chauncey
committed
fix(prometheus): Add support for bearer token to prometheus input plugin
closes #864
1 parent a15fed3 commit 78f648f

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

plugins/inputs/prometheus/prometheus.go

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
11
package prometheus
22

33
import (
4+
"crypto/tls"
45
"errors"
56
"fmt"
67
"github.com/influxdata/telegraf"
78
"github.com/influxdata/telegraf/plugins/inputs"
89
"io/ioutil"
10+
"net"
911
"net/http"
1012
"sync"
1113
"time"
1214
)
1315

1416
type Prometheus struct {
1517
Urls []string
18+
19+
// Use SSL but skip chain & host verification
20+
InsecureSkipVerify bool
21+
// Bearer Token authorization file path
22+
BearerToken string `toml:"bearer_token"`
1623
}
1724

1825
var sampleConfig = `
1926
## An array of urls to scrape metrics from.
2027
urls = ["http://localhost:9100/metrics"]
28+
29+
### Use SSL but skip chain & host verification
30+
# insecure_skip_verify = false
31+
### Use bearer token for authorization
32+
# bearer_token = /path/to/bearer/token
2133
`
2234

23-
func (r *Prometheus) SampleConfig() string {
35+
func (p *Prometheus) SampleConfig() string {
2436
return sampleConfig
2537
}
2638

27-
func (r *Prometheus) Description() string {
39+
func (p *Prometheus) Description() string {
2840
return "Read metrics from one or many prometheus clients"
2941
}
3042

3143
var ErrProtocolError = errors.New("prometheus protocol error")
3244

3345
// Reads stats from all configured servers accumulates stats.
3446
// Returns one of the errors encountered while gather stats (if any).
35-
func (g *Prometheus) Gather(acc telegraf.Accumulator) error {
47+
func (p *Prometheus) Gather(acc telegraf.Accumulator) error {
3648
var wg sync.WaitGroup
3749

3850
var outerr error
3951

40-
for _, serv := range g.Urls {
52+
for _, serv := range p.Urls {
4153
wg.Add(1)
4254
go func(serv string) {
4355
defer wg.Done()
44-
outerr = g.gatherURL(serv, acc)
56+
outerr = p.gatherURL(serv, acc)
4557
}(serv)
4658
}
4759

@@ -59,9 +71,34 @@ var client = &http.Client{
5971
Timeout: time.Duration(4 * time.Second),
6072
}
6173

62-
func (g *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
74+
func (p *Prometheus) gatherURL(url string, acc telegraf.Accumulator) error {
6375
collectDate := time.Now()
64-
resp, err := client.Get(url)
76+
var req, err = http.NewRequest("GET", url, nil)
77+
req.Header = make(http.Header)
78+
var token []byte
79+
var resp *http.Response
80+
81+
var rt http.RoundTripper = &http.Transport{
82+
Dial: (&net.Dialer{
83+
Timeout: 10 * time.Second,
84+
KeepAlive: 30 * time.Second,
85+
}).Dial,
86+
TLSHandshakeTimeout: 10 * time.Second,
87+
TLSClientConfig: &tls.Config{
88+
InsecureSkipVerify: true,
89+
},
90+
ResponseHeaderTimeout: time.Duration(3 * time.Second),
91+
}
92+
93+
if p.BearerToken != "" {
94+
token, err = ioutil.ReadFile(p.BearerToken)
95+
if err != nil {
96+
return err
97+
}
98+
req.Header.Set("Authorization", "Bearer "+string(token))
99+
}
100+
101+
resp, err = rt.RoundTrip(req)
65102
if err != nil {
66103
return fmt.Errorf("error making HTTP request to %s: %s", url, err)
67104
}

0 commit comments

Comments
 (0)