Skip to content

Commit f1550d4

Browse files
author
Arvind Iyengar
committed
Introduce changes from upstream
1 parent 84f3730 commit f1550d4

File tree

6 files changed

+90
-95
lines changed

6 files changed

+90
-95
lines changed

.gitignore

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@
33
/bin
44
/dist
55
*.swp
6-
.idea
6+
.idea
7+
8+
/.build/
9+
/.release/
10+
/.tarballs/
11+
/node_exporter*
12+
/prometheus*
13+
/data/
14+
/pushprox-client
15+
/pushprox-proxy
16+
17+
/vendor

SECURITY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Reporting a security issue
2+
3+
The Prometheus security policy, including how to report vulnerabilities, can be
4+
found here:
5+
6+
https://prometheus.io/docs/operating/security/

cmd/client/main.go

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"crypto/x509"
2222
"fmt"
2323
"io/ioutil"
24-
"math/rand"
2524
"net"
2625
"net/http"
2726
"net/url"
@@ -31,7 +30,8 @@ import (
3130

3231
kingpin "gopkg.in/alecthomas/kingpin.v2"
3332

34-
"github.com/ShowMax/go-fqdn"
33+
"github.com/Showmax/go-fqdn"
34+
"github.com/cenkalti/backoff/v4"
3535
"github.com/go-kit/kit/log"
3636
"github.com/go-kit/kit/log/level"
3737
"github.com/pkg/errors"
@@ -53,6 +53,9 @@ var (
5353
insecureSkipVerify = kingpin.Flag("insecure-skip-verify", "Disable SSL security checks for client").Default("false").Bool()
5454
useLocalhost = kingpin.Flag("use-localhost", "Use 127.0.0.1 to scrape metrics instead of FQDN").Default("false").Bool()
5555
allowPort = kingpin.Flag("allow-port", "Restricts the proxy to only being allowed to scrape the given port").Default("*").String()
56+
57+
retryInitialWait = kingpin.Flag("proxy.retry.initial-wait", "Amount of time to wait after proxy failure").Default("1s").Duration()
58+
retryMaxWait = kingpin.Flag("proxy.retry.max-wait", "Maximum amount of time to wait between proxy poll retries").Default("5s").Duration()
5659
)
5760

5861
var (
@@ -80,6 +83,15 @@ func init() {
8083
prometheus.MustRegister(pushErrorCounter, pollErrorCounter, scrapeErrorCounter)
8184
}
8285

86+
func newBackOffFromFlags() backoff.BackOff {
87+
b := backoff.NewExponentialBackOff()
88+
b.InitialInterval = *retryInitialWait
89+
b.Multiplier = 1.5
90+
b.MaxInterval = *retryMaxWait
91+
b.MaxElapsedTime = time.Duration(0)
92+
return b
93+
}
94+
8395
// Coordinator for scrape requests and responses
8496
type Coordinator struct {
8597
logger log.Logger
@@ -193,7 +205,7 @@ func (c *Coordinator) doPush(resp *http.Response, origRequest *http.Request, cli
193205
return nil
194206
}
195207

196-
func loop(c Coordinator, client *http.Client) error {
208+
func (c *Coordinator) doPoll(client *http.Client) error {
197209
base, err := url.Parse(*proxyURL)
198210
if err != nil {
199211
level.Error(c.logger).Log("msg", "Error parsing url:", "err", err)
@@ -226,35 +238,18 @@ func loop(c Coordinator, client *http.Client) error {
226238
return nil
227239
}
228240

229-
// decorrelated Jitter increases the maximum jitter based on the last random value.
230-
type decorrelatedJitter struct {
231-
duration time.Duration // sleep time
232-
min time.Duration // min sleep time
233-
cap time.Duration // max sleep time
234-
}
235-
236-
func newJitter() decorrelatedJitter {
237-
rand.Seed(time.Now().UnixNano())
238-
return decorrelatedJitter{
239-
min: 50 * time.Millisecond,
240-
cap: 5 * time.Second,
241+
func (c *Coordinator) loop(bo backoff.BackOff, client *http.Client) {
242+
op := func() error {
243+
return c.doPoll(client)
241244
}
242-
}
243245

244-
func (d *decorrelatedJitter) calc() time.Duration {
245-
change := rand.Float64() * float64(d.duration*time.Duration(3)-d.min)
246-
d.duration = d.min + time.Duration(change)
247-
if d.duration > d.cap {
248-
d.duration = d.cap
249-
}
250-
if d.duration < d.min {
251-
d.duration = d.min
246+
for {
247+
if err := backoff.RetryNotify(op, bo, func(err error, _ time.Duration) {
248+
pollErrorCounter.Inc()
249+
}); err != nil {
250+
level.Error(c.logger).Log("err", err)
251+
}
252252
}
253-
return d.duration
254-
}
255-
256-
func (d *decorrelatedJitter) sleep() {
257-
time.Sleep(d.calc())
258253
}
259254

260255
func main() {
@@ -333,14 +328,7 @@ func main() {
333328
TLSClientConfig: tlsConfig,
334329
}
335330

336-
jitter := newJitter()
337331
client := &http.Client{Transport: transport}
338-
for {
339-
err := loop(coordinator, client)
340-
if err != nil {
341-
pollErrorCounter.Inc()
342-
jitter.sleep()
343-
continue
344-
}
345-
}
332+
333+
coordinator.loop(newBackOffFromFlags(), client)
346334
}

cmd/client/main_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ import (
2222
"github.com/pkg/errors"
2323
)
2424

25-
func TestJitter(t *testing.T) {
26-
jitter := newJitter()
27-
for i := 0; i < 100000; i++ {
28-
duration := jitter.calc()
29-
if !(jitter.min <= duration || duration <= jitter.cap) {
30-
t.Fatal("invalid jitter value: ", duration)
31-
}
32-
}
33-
}
34-
3525
type TestLogger struct{}
3626

3727
func (tl *TestLogger) Log(vars ...interface{}) error {
@@ -76,7 +66,7 @@ func TestHandleErr(t *testing.T) {
7666
func TestLoop(t *testing.T) {
7767
ts, c := prepareTest()
7868
defer ts.Close()
79-
if err := loop(c, ts.Client()); err != nil {
69+
if err := c.doPoll(ts.Client()); err != nil {
8070
t.Fatal(err)
8171
}
8272
}

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ module github.com/rancher/pushprox
33
go 1.13
44

55
require (
6-
github.com/ShowMax/go-fqdn v0.0.0-20180501083314-6f60894d629f
7-
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
6+
github.com/Showmax/go-fqdn v1.0.0
7+
github.com/cenkalti/backoff/v4 v4.1.0
88
github.com/go-kit/kit v0.10.0
9-
github.com/google/uuid v1.1.1
9+
github.com/google/uuid v1.2.0
1010
github.com/pkg/errors v0.9.1
11-
github.com/prometheus/client_golang v1.6.0
12-
github.com/prometheus/common v0.10.0
11+
github.com/prometheus/client_golang v1.10.0
12+
github.com/prometheus/common v0.23.0
1313
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1414
)

0 commit comments

Comments
 (0)