-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmain.go
312 lines (278 loc) · 8.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/ShowMax/go-fqdn"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus-community/pushprox/util"
)
var (
myFqdn = kingpin.Flag("fqdn", "FQDN to register with").Default(fqdn.Get()).String()
proxyURL = kingpin.Flag("proxy-url", "Push proxy to talk to.").Required().String()
caCertFile = kingpin.Flag("tls.cacert", "<file> CA certificate to verify peer against").String()
tlsCert = kingpin.Flag("tls.cert", "<cert> Client certificate file").String()
tlsKey = kingpin.Flag("tls.key", "<key> Private key file").String()
metricsAddr = kingpin.Flag("metrics-addr", "Serve Prometheus metrics at this address").Default(":9369").String()
)
var (
scrapeErrorCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pushprox_client_scrape_errors_total",
Help: "Number of scrape errors",
},
)
pushErrorCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pushprox_client_push_errors_total",
Help: "Number of push errors",
},
)
pollErrorCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pushprox_client_poll_errors_total",
Help: "Number of poll errors",
},
)
)
func init() {
prometheus.MustRegister(pushErrorCounter, pollErrorCounter, scrapeErrorCounter)
}
// Coordinator for scrape requests and responses
type Coordinator struct {
logger log.Logger
}
func (c *Coordinator) handleErr(request *http.Request, client *http.Client, err error) {
level.Error(c.logger).Log("err", err)
scrapeErrorCounter.Inc()
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(strings.NewReader(err.Error())),
Header: http.Header{},
}
if err = c.doPush(resp, request, client); err != nil {
pushErrorCounter.Inc()
level.Warn(c.logger).Log("msg", "Failed to push failed scrape response:", "err", err)
return
}
level.Info(c.logger).Log("msg", "Pushed failed scrape response")
}
func (c *Coordinator) doScrape(request *http.Request, client *http.Client) {
logger := log.With(c.logger, "scrape_id", request.Header.Get("id"))
timeout, err := util.GetHeaderTimeout(request.Header)
if err != nil {
c.handleErr(request, client, err)
return
}
ctx, cancel := context.WithTimeout(request.Context(), timeout)
defer cancel()
request = request.WithContext(ctx)
// We cannot handle https requests at the proxy, as we would only
// see a CONNECT, so use a URL parameter to trigger it.
params := request.URL.Query()
if params.Get("_scheme") == "https" {
request.URL.Scheme = "https"
params.Del("_scheme")
request.URL.RawQuery = params.Encode()
}
if request.URL.Hostname() != *myFqdn {
c.handleErr(request, client, errors.New("scrape target doesn't match client fqdn"))
return
}
scrapeResp, err := client.Do(request)
if err != nil {
msg := fmt.Sprintf("failed to scrape %s", request.URL.String())
c.handleErr(request, client, errors.Wrap(err, msg))
return
}
level.Info(logger).Log("msg", "Retrieved scrape response")
if err = c.doPush(scrapeResp, request, client); err != nil {
pushErrorCounter.Inc()
level.Warn(logger).Log("msg", "Failed to push scrape response:", "err", err)
return
}
level.Info(logger).Log("msg", "Pushed scrape result")
}
// Report the result of the scrape back up to the proxy.
func (c *Coordinator) doPush(resp *http.Response, origRequest *http.Request, client *http.Client) error {
resp.Header.Set("id", origRequest.Header.Get("id")) // Link the request and response
// Remaining scrape deadline.
deadline, _ := origRequest.Context().Deadline()
resp.Header.Set("X-Prometheus-Scrape-Timeout", fmt.Sprintf("%f", float64(time.Until(deadline))/1e9))
base, err := url.Parse(*proxyURL)
if err != nil {
return err
}
u, err := url.Parse("push")
if err != nil {
return err
}
url := base.ResolveReference(u)
buf := &bytes.Buffer{}
resp.Write(buf)
request := &http.Request{
Method: "POST",
URL: url,
Body: ioutil.NopCloser(buf),
ContentLength: int64(buf.Len()),
}
request = request.WithContext(origRequest.Context())
if _, err = client.Do(request); err != nil {
return err
}
return nil
}
func loop(c Coordinator, client *http.Client) error {
base, err := url.Parse(*proxyURL)
if err != nil {
level.Error(c.logger).Log("msg", "Error parsing url:", "err", err)
return errors.Wrap(err, "error parsing url")
}
u, err := url.Parse("poll/" + *myFqdn)
if err != nil {
level.Error(c.logger).Log("msg", "Error parsing url:", "err", err)
return errors.Wrap(err, "error parsing url poll")
}
url := base.ResolveReference(u)
resp, err := client.Post(url.String(), "", strings.NewReader(*myFqdn))
if err != nil {
level.Error(c.logger).Log("msg", "Error polling:", "err", err)
return errors.Wrap(err, "error polling")
}
defer resp.Body.Close()
request, err := http.ReadRequest(bufio.NewReader(resp.Body))
if err != nil {
level.Error(c.logger).Log("msg", "Error reading request:", "err", err)
return errors.Wrap(err, "error reading request")
}
level.Info(c.logger).Log("msg", "Got scrape request", "scrape_id", request.Header.Get("id"), "url", request.URL)
request.RequestURI = ""
go c.doScrape(request, client)
return nil
}
// decorrelated Jitter increases the maximum jitter based on the last random value.
type decorrelatedJitter struct {
duration time.Duration // sleep time
min time.Duration // min sleep time
cap time.Duration // max sleep time
}
func newJitter() decorrelatedJitter {
rand.Seed(time.Now().UnixNano())
return decorrelatedJitter{
min: 50 * time.Millisecond,
cap: 5 * time.Second,
}
}
func (d *decorrelatedJitter) calc() time.Duration {
change := rand.Float64() * float64(d.duration*time.Duration(3)-d.min)
d.duration = d.min + time.Duration(change)
if d.duration > d.cap {
d.duration = d.cap
}
if d.duration < d.min {
d.duration = d.min
}
return d.duration
}
func (d *decorrelatedJitter) sleep() {
time.Sleep(d.calc())
}
func main() {
promlogConfig := promlog.Config{}
flag.AddFlags(kingpin.CommandLine, &promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(&promlogConfig)
coordinator := Coordinator{logger: logger}
if *proxyURL == "" {
level.Error(coordinator.logger).Log("msg", "--proxy-url flag must be specified.")
os.Exit(1)
}
// Make sure proxyURL ends with a single '/'
*proxyURL = strings.TrimRight(*proxyURL, "/") + "/"
level.Info(coordinator.logger).Log("msg", "URL and FQDN info", "proxy_url", *proxyURL, "fqdn", *myFqdn)
tlsConfig := &tls.Config{}
if *tlsCert != "" {
cert, err := tls.LoadX509KeyPair(*tlsCert, *tlsKey)
if err != nil {
level.Error(coordinator.logger).Log("msg", "Certificate or Key is invalid", "err", err)
os.Exit(1)
}
// Setup HTTPS client
tlsConfig.Certificates = []tls.Certificate{cert}
tlsConfig.BuildNameToCertificate()
}
if *caCertFile != "" {
caCert, err := ioutil.ReadFile(*caCertFile)
if err != nil {
level.Error(coordinator.logger).Log("msg", "Not able to read cacert file", "err", err)
os.Exit(1)
}
caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
level.Error(coordinator.logger).Log("msg", "Failed to use cacert file as ca certificate")
os.Exit(1)
}
tlsConfig.RootCAs = caCertPool
}
if *metricsAddr != "" {
go func() {
if err := http.ListenAndServe(*metricsAddr, promhttp.Handler()); err != nil {
level.Warn(coordinator.logger).Log("msg", "ListenAndServe", "err", err)
}
}()
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
jitter := newJitter()
client := &http.Client{Transport: transport}
for {
err := loop(coordinator, client)
if err != nil {
pollErrorCounter.Inc()
jitter.sleep()
continue
}
}
}