14
14
package main
15
15
16
16
import (
17
+ "fmt"
17
18
"net/http"
18
19
"net/url"
19
20
"os"
@@ -31,6 +32,16 @@ import (
31
32
"gopkg.in/alecthomas/kingpin.v2"
32
33
)
33
34
35
+ type transportWithApiKey struct {
36
+ underlyingTransport http.RoundTripper
37
+ apiKey string
38
+ }
39
+
40
+ func (t * transportWithApiKey ) RoundTrip (req * http.Request ) (* http.Response , error ) {
41
+ req .Header .Add ("Authorization" , fmt .Sprintf ("ApiKey %s" , t .apiKey ))
42
+ return t .underlyingTransport .RoundTrip (req )
43
+ }
44
+
34
45
func main () {
35
46
var (
36
47
Name = "elasticsearch_exporter"
@@ -85,6 +96,9 @@ func main() {
85
96
esInsecureSkipVerify = kingpin .Flag ("es.ssl-skip-verify" ,
86
97
"Skip SSL verification when connecting to Elasticsearch." ).
87
98
Default ("false" ).Envar ("ES_SSL_SKIP_VERIFY" ).Bool ()
99
+ esApiKey = kingpin .Flag ("es.apiKey" ,
100
+ "API Key to use for authenticating against Elasticsearch" ).
101
+ Default ("" ).Envar ("ES_API_KEY" ).String ()
88
102
logLevel = kingpin .Flag ("log.level" ,
89
103
"Sets the loglevel. Valid levels are debug, info, warn, error" ).
90
104
Default ("info" ).Envar ("LOG_LEVEL" ).String ()
@@ -114,12 +128,25 @@ func main() {
114
128
// returns nil if not provided and falls back to simple TCP.
115
129
tlsConfig := createTLSConfig (* esCA , * esClientCert , * esClientPrivateKey , * esInsecureSkipVerify )
116
130
131
+ var httpTransport http.RoundTripper
132
+
133
+ httpTransport = & http.Transport {
134
+ TLSClientConfig : tlsConfig ,
135
+ Proxy : http .ProxyFromEnvironment ,
136
+ }
137
+
138
+ if * esApiKey != "" {
139
+ apiKey := * esApiKey
140
+
141
+ httpTransport = & transportWithApiKey {
142
+ underlyingTransport : httpTransport ,
143
+ apiKey : apiKey ,
144
+ }
145
+ }
146
+
117
147
httpClient := & http.Client {
118
- Timeout : * esTimeout ,
119
- Transport : & http.Transport {
120
- TLSClientConfig : tlsConfig ,
121
- Proxy : http .ProxyFromEnvironment ,
122
- },
148
+ Timeout : * esTimeout ,
149
+ Transport : httpTransport ,
123
150
}
124
151
125
152
// version metric
0 commit comments