Skip to content

Commit

Permalink
Add a rate limit for Prometheus API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jul 5, 2022
1 parent f11f82b commit e6b7fde
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 9 deletions.
18 changes: 18 additions & 0 deletions cmd/pint/tests/0025_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ level=info msg="Loading configuration file" path=.pint.hcl
"uri": "https://",
"timeout": "2m",
"concurrency": 16,
"rateLimit": 100,
"cache": 10000,
"required": true
},
Expand All @@ -24,6 +25,7 @@ level=info msg="Loading configuration file" path=.pint.hcl
"uri": "https://",
"timeout": "2m",
"concurrency": 32,
"rateLimit": 100,
"cache": 10000,
"required": false
},
Expand All @@ -32,8 +34,18 @@ level=info msg="Loading configuration file" path=.pint.hcl
"uri": "https://",
"timeout": "10s",
"concurrency": 16,
"rateLimit": 100,
"cache": 32,
"required": false
},
{
"name": "custom-rateLimit",
"uri": "https://",
"timeout": "15s",
"concurrency": 16,
"rateLimit": 1,
"cache": 10000,
"required": false
}
],
"checks": {
Expand Down Expand Up @@ -163,6 +175,12 @@ prometheus "custom-cache" {
cache = 32
}

prometheus "custom-rateLimit" {
uri = "https://"
timeout = "15s"
rateLimit = 1
}

checks {
disabled = ["promql/fragile"]
}
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.24.0

### Added

- Added rate limit for Prometheus API requests with a default value of 100
requests per second. To customize it set `rateLimit` field inside selected
`prometheus` server definition.

## v0.23.0

### Fixed
Expand Down
6 changes: 5 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ prometheus "$name" {
failover = ["https://...", ...]
timeout = "60s"
concurrency = 16
rateLimit = 100
cache = 10000
required = true|false
paths = ["...", ...]
Expand All @@ -176,8 +177,11 @@ prometheus "$name" {
configuration, otherwise pint checks might return unreliable results and potential
false positives.
- `timeout` - timeout to be used for API requests.
- `concurrency` - how many concurrent requests can pint send to this Prometheus.
- `concurrency` - how many concurrent requests can pint send to this Prometheus server.
Optional, defaults to 16.
- `rateLimit` - per second rate limit for all API requests send to this Prometheus server.
Setting it to `1000` would allow for up to 1000 requests per each wall clock second.
Optional, default to 100 requests per second.
- `cache` - size of the query cache, defaults to 10000.
- `required` - decides how pint will report errors if it's unable to get a valid response
from this Prometheus server. If `required` is `true` and all API calls to this Prometheus
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ require (
github.com/rs/zerolog v1.27.0
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.10.3
go.uber.org/ratelimit v0.2.0
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.35 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 h1:MzBOUgng9orim59UnfUTLRjMpd09C5uEVQ6RPGeCaVI=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129/go.mod h1:rFgpPQZYZ8vdbc+48xibu8ALc3yeyd64IhHS+PU6Yyg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
Expand Down Expand Up @@ -427,10 +429,13 @@ go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+n
go.opentelemetry.io/otel/trace v1.7.0 h1:O37Iogk1lEkMRXewVtZ1BBTVn5JEp8GrJvP92bJqC6o=
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/ratelimit v0.2.0 h1:UQE2Bgi7p2B85uP5dC2bbRtig0C+OeNRnNEafLjsLPA=
go.uber.org/ratelimit v0.2.0/go.mod h1:YYBV4e4naJvhpitQrWJu1vCpgB7CboMe0qhltKt6mUg=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
2 changes: 1 addition & 1 deletion internal/checks/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func simpleProm(name, uri string, timeout time.Duration, required bool) *promapi
return promapi.NewFailoverGroup(
name,
[]*promapi.Prometheus{
promapi.NewPrometheus(name, uri, timeout, 16, 1000),
promapi.NewPrometheus(name, uri, timeout, 16, 1000, 1000),
},
required,
)
Expand Down
Loading

0 comments on commit e6b7fde

Please sign in to comment.