1
1
package prometheus
2
2
3
3
import (
4
+ "crypto/tls"
4
5
"errors"
5
6
"fmt"
6
7
"github.com/influxdata/telegraf"
7
8
"github.com/influxdata/telegraf/plugins/inputs"
8
9
"io/ioutil"
10
+ "net"
9
11
"net/http"
10
12
"sync"
11
13
"time"
12
14
)
13
15
14
16
type Prometheus struct {
15
17
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"`
16
23
}
17
24
18
25
var sampleConfig = `
19
26
## An array of urls to scrape metrics from.
20
27
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
21
33
`
22
34
23
- func (r * Prometheus ) SampleConfig () string {
35
+ func (p * Prometheus ) SampleConfig () string {
24
36
return sampleConfig
25
37
}
26
38
27
- func (r * Prometheus ) Description () string {
39
+ func (p * Prometheus ) Description () string {
28
40
return "Read metrics from one or many prometheus clients"
29
41
}
30
42
31
43
var ErrProtocolError = errors .New ("prometheus protocol error" )
32
44
33
45
// Reads stats from all configured servers accumulates stats.
34
46
// 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 {
36
48
var wg sync.WaitGroup
37
49
38
50
var outerr error
39
51
40
- for _ , serv := range g .Urls {
52
+ for _ , serv := range p .Urls {
41
53
wg .Add (1 )
42
54
go func (serv string ) {
43
55
defer wg .Done ()
44
- outerr = g .gatherURL (serv , acc )
56
+ outerr = p .gatherURL (serv , acc )
45
57
}(serv )
46
58
}
47
59
@@ -59,9 +71,34 @@ var client = &http.Client{
59
71
Timeout : time .Duration (4 * time .Second ),
60
72
}
61
73
62
- func (g * Prometheus ) gatherURL (url string , acc telegraf.Accumulator ) error {
74
+ func (p * Prometheus ) gatherURL (url string , acc telegraf.Accumulator ) error {
63
75
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 : p .InsecureSkipVerify ,
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 )
65
102
if err != nil {
66
103
return fmt .Errorf ("error making HTTP request to %s: %s" , url , err )
67
104
}
0 commit comments