Skip to content

Commit c367079

Browse files
authored
Merge pull request #35 from olvesh/retry-with-exp-backoff
Added exponential backoff and retries on stackdriver api
2 parents 49c2fbd + bdf0a23 commit c367079

File tree

8 files changed

+521
-12
lines changed

8 files changed

+521
-12
lines changed

Gopkg.lock

Lines changed: 81 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stackdriver_exporter.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/PuerkitoBio/rehttp"
910
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/prometheus/common/log"
1112
"github.com/prometheus/common/version"
@@ -45,6 +46,26 @@ var (
4546
collectorFillMissingLabels = kingpin.Flag(
4647
"collector.fill-missing-labels", "Fill missing metrics labels with empty string to avoid label dimensions inconsistent failure ($STACKDRIVER_EXPORTER_COLLECTOR_FILL_MISSING_LABELS).",
4748
).Envar("STACKDRIVER_EXPORTER_COLLECTOR_FILL_MISSING_LABELS").Default("true").Bool()
49+
50+
stackdriverMaxRetries = kingpin.Flag(
51+
"stackdriver.max-retries", "Max number of retries that should be attempted on 503 errors from stackdriver. ($STACKDRIVER_EXPORTER_MAX_RETRIES)",
52+
).Envar("STACKDRIVER_EXPORTER_MAX_RETRIES").Default("0").Int()
53+
54+
stackdriverHttpTimeout = kingpin.Flag(
55+
"stackdriver.http-timeout", "How long should stackdriver_exporter wait for a result from the Stackdriver API ($STACKDRIVER_EXPORTER_HTTP_TIMEOUT)",
56+
).Envar("STACKDRIVER_EXPORTER_HTTP_TIMEOUT").Default("10s").Duration()
57+
58+
stackdriverMaxBackoffDuration = kingpin.Flag(
59+
"stackdriver.max-backoff", "Max time between each request in an exp backoff scenario ($STACKDRIVER_EXPORTER_MAX_BACKOFF_DURATION)",
60+
).Envar("STACKDRIVER_EXPORTER_MAX_BACKOFF_DURATION").Default("5s").Duration()
61+
62+
stackdriverBackoffJitterBase = kingpin.Flag(
63+
"stackdriver.backoff-jitter", "The amount of jitter to introduce in a exp backoff scenario ($STACKDRIVER_EXPORTER_BACKODFF_JITTER_BASE)",
64+
).Envar("STACKDRIVER_EXPORTER_BACKODFF_JITTER_BASE").Default("1s").Duration()
65+
66+
stackdriverRetryStatuses = kingpin.Flag(
67+
"stackdriver.retry-statuses", "The HTTP statuses that should trigger a retry ($STACKDRIVER_EXPORTER_RETRY_STATUSES)",
68+
).Envar("STACKDRIVER_EXPORTER_RETRY_STATUSES").Default("503").Ints()
4869
)
4970

5071
func init() {
@@ -55,6 +76,16 @@ func createMonitoringService() (*monitoring.Service, error) {
5576
ctx := context.Background()
5677

5778
googleClient, err := google.DefaultClient(ctx, monitoring.MonitoringReadScope)
79+
80+
googleClient.Timeout = *stackdriverHttpTimeout
81+
googleClient.Transport = rehttp.NewTransport(
82+
googleClient.Transport, // need to wrap DefaultClient transport
83+
rehttp.RetryAll(
84+
rehttp.RetryMaxRetries(*stackdriverMaxRetries),
85+
rehttp.RetryStatuses(*stackdriverRetryStatuses...)), // Cloud support suggests retrying on 503 errors
86+
rehttp.ExpJitterDelay(*stackdriverBackoffJitterBase, *stackdriverMaxBackoffDuration), // Set timeout to <10s as that is prom default timeout
87+
)
88+
5889
if err != nil {
5990
return nil, fmt.Errorf("Error creating Google client: %v", err)
6091
}

vendor/github.com/PuerkitoBio/rehttp/.travis.yml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/PuerkitoBio/rehttp/LICENSE

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/PuerkitoBio/rehttp/README.md

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/PuerkitoBio/rehttp/context_post17.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)