Skip to content

Commit 7c57706

Browse files
authored
Add rate limiting per tenant (observatorium#102)
* Implement rate limiting per tenant Signed-off-by: Kemal Akkoyun <[email protected]> * Add instrumentation for grpc client Signed-off-by: Kemal Akkoyun <[email protected]> * Draw finer package boundaries Signed-off-by: Kemal Akkoyun <[email protected]> * Fix protoc install script Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 8231a89 commit 7c57706

29 files changed

+2650
-89
lines changed

Diff for: .bingo/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Ignore everything
2+
# Ignore everything.
33
*
44

55
# But not these files:

Diff for: .bingo/Variables.mk

+12
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ $(UP): .bingo/up.mod
8282
@echo "(re)installing $(GOBIN)/up-v0.0.0-20200928171403-120d85735d11"
8383
@cd .bingo && $(GO) build -modfile=up.mod -o=$(GOBIN)/up-v0.0.0-20200928171403-120d85735d11 "github.com/observatorium/up/cmd/up"
8484

85+
GUBERNATOR := $(GOBIN)/gubernator-v0.8.4-0.20200617200142-07e238f8cd86
86+
$(GUBERNATOR): .bingo/gubernator.mod
87+
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
88+
@echo "(re)installing $(GOBIN)/gubernator-v0.8.4-0.20200617200142-07e238f8cd86"
89+
@cd .bingo && $(GO) build -modfile=gubernator.mod -o=$(GOBIN)/gubernator-v0.8.4-0.20200617200142-07e238f8cd86 "github.com/mailgun/gubernator/cmd/gubernator"
90+
91+
PROTOC_GEN_GO := $(GOBIN)/protoc-gen-go-v1.4.2
92+
$(PROTOC_GEN_GO): .bingo/protoc-gen-go.mod
93+
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
94+
@echo "(re)installing $(GOBIN)/protoc-gen-go-v1.4.2"
95+
@cd .bingo && $(GO) build -modfile=protoc-gen-go.mod -o=$(GOBIN)/protoc-gen-go-v1.4.2 "github.com/golang/protobuf/protoc-gen-go"
96+

Diff for: .bingo/gubernator.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
2+
3+
go 1.15
4+
5+
require github.com/mailgun/gubernator v0.8.4-0.20200617200142-07e238f8cd86 // cmd/gubernator

Diff for: .bingo/protoc-gen-go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
2+
3+
go 1.15
4+
5+
require github.com/golang/protobuf v1.4.2 // protoc-gen-go

Diff for: .bingo/variables.env

+4
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ STYX="${gobin}/styx-v0.0.0-20200109161911-78a77eb717b4"
3030

3131
UP="${gobin}/up-v0.0.0-20200928171403-120d85735d11"
3232

33+
GUBERNATOR="${gobin}/gubernator-v0.8.4-0.20200617200142-07e238f8cd86"
34+
35+
PROTOC_GEN_GO="${gobin}/protoc-gen-go-v1.4.2"
36+

Diff for: .circleci/config.yml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
- run: |
3636
make README.md
3737
make generate validate --always-make
38+
apt-get update && apt-get -y install unzip
39+
make proto
3840
git diff --exit-code
3941
4042
container-push:

Diff for: Makefile

+31-9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ PROMREMOTEBENCH_VERSION ?= 0.8.0
4343
SHELLCHECK ?= $(BIN_DIR)/shellcheck
4444
MOCKPROVIDER ?= $(BIN_DIR)/mockprovider
4545
GENERATE_TLS_CERT ?= $(BIN_DIR)/generate-tls-cert
46+
47+
PROTOC ?= $(TMP_DIR)/protoc
48+
PROTOC_VERSION ?= 3.13.0
49+
4650
SERVER_CERT ?= $(CERT_DIR)/server.pem
4751

4852
default: observatorium
@@ -88,7 +92,7 @@ validate: $(KUBEVAL)
8892

8993
.PHONY: shellcheck
9094
shellcheck: $(SHELLCHECK)
91-
$(SHELLCHECK) $(shell find . -type f -name "*.sh" -not -path "*vendor*" -not -path "${TMP_DIR}/*")
95+
$(SHELLCHECK) $(shell find . -type f -name "*.sh" -not -path "*vendor*" -not -path "*tmp*")
9296

9397
.PHONY: lint
9498
lint: $(GOLANGCI_LINT) deps shellcheck jsonnet-fmt
@@ -113,8 +117,23 @@ test-load: build load-test-dependencies
113117
clean:
114118
-rm tmp/help.txt
115119
-rm -rf tmp/bin
120+
-rm -rf tmp/src
116121
-rm observatorium
117122

123+
ratelimit/gubernator/proto/google:
124+
mkdir -p $(TMP_DIR)/src/grpc-gateway
125+
mkdir -p $(shell dirname $@)
126+
curl -L "https://github.com/grpc-ecosystem/grpc-gateway/archive/master.tar.gz" | tar --strip-components=1 -xzf - -C $(TMP_DIR)/src/grpc-gateway
127+
mv $(TMP_DIR)/src/grpc-gateway/third_party/googleapis/google $@
128+
129+
ratelimit/gubernator/gubernator.proto:
130+
curl -L -o ratelimit/gubernator/gubernator.proto "https://raw.githubusercontent.com/mailgun/gubernator/master/proto/gubernator.proto"
131+
132+
.PHONY: proto
133+
proto: ratelimit/gubernator/proto/google ratelimit/gubernator/gubernator.proto $(PROTOC) $(PROTOC_GEN_GO) $(BIN_DIR)
134+
@cp -f $(PROTOC_GEN_GO) $(BIN_DIR)/protoc-gen-go
135+
PATH=$$PATH:$(BIN_DIR):$(FIRST_GOPATH)/bin scripts/generate_proto.sh
136+
118137
.PHONY: container
119138
container: Dockerfile
120139
@docker build --build-arg BUILD_DATE="$(BUILD_TIMESTAMP)" \
@@ -139,7 +158,7 @@ container-release: container
139158
docker push $(DOCKER_REPO):latest
140159

141160
.PHONY: integration-test-dependencies
142-
integration-test-dependencies: $(THANOS) $(UP) $(DEX) $(LOKI) $(WEBSOCAT) $(OPA)
161+
integration-test-dependencies: $(THANOS) $(UP) $(DEX) $(LOKI) $(WEBSOCAT) $(OPA) $(GUBERNATOR)
143162

144163
.PHONY: load-test-dependencies
145164
load-test-dependencies: $(PROMREMOTEBENCH) $(PROMETHEUS) $(STYX) $(MOCKPROVIDER)
@@ -184,24 +203,27 @@ $(WEBSOCAT): | $(BIN_DIR)
184203
mv $(WEBSOCAT_PKG) websocat && \
185204
chmod u+x websocat
186205

187-
$(PROMREMOTEBENCH): | $(BIN_DIR)
188-
mkdir -p $(TMP_DIR)/promremotebench
189-
curl -L https://github.com/m3dbx/promremotebench/archive/v$(PROMREMOTEBENCH_VERSION).tar.gz | tar --strip-components=1 -xzf - -C $(TMP_DIR)/promremotebench
190-
cd $(TMP_DIR)/promremotebench/src && \
206+
$(PROMREMOTEBENCH): | deps $(BIN_DIR)
207+
mkdir -p $(TMP_DIR)/src/promremotebench
208+
curl -L https://github.com/m3dbx/promremotebench/archive/v$(PROMREMOTEBENCH_VERSION).tar.gz | tar --strip-components=1 -xzf - -C $(TMP_DIR)/src/promremotebench
209+
cd $(TMP_DIR)/src/promremotebench && \
191210
go build ./cmd/promremotebench
192-
mv $(TMP_DIR)/promremotebench/src/promremotebench $@
211+
mv $(TMP_DIR)/src/promremotebench/promremotebench $@
193212

194213
$(SHELLCHECK): $(BIN_DIR)
195214
curl -sNL "https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.$(OS).$(ARCH).tar.xz" | tar --strip-components=1 -xJf - -C $(BIN_DIR)
196215

197216
$(MOCKPROVIDER): | deps $(BIN_DIR)
198-
go build -tags tools -o $@ github.com/observatorium/observatorium/test/mock
217+
go build -tags tools -o $@ github.com/observatorium/observatorium/test/mock
199218

200219
$(GENERATE_TLS_CERT): | deps $(BIN_DIR)
201220
# A thin wrapper around github.com/cloudflare/cfssl
202221
go build -tags tools -o $@ github.com/observatorium/observatorium/test/tls
203222

204-
# Jsonnet and Example manifests
223+
$(PROTOC): $(TMP_DIR) $(BIN_DIR)
224+
@PROTOC_VERSION="$(PROTOC_VERSION)" TMP_DIR="$(TMP_DIR)" BIN_DIR="$(BIN_DIR)" scripts/install_protoc.sh
225+
226+
# Jsonnet and Example manifests.
205227

206228
CONTAINER_CMD:=docker run --rm \
207229
-u="$(shell id -u):$(shell id -g)" \

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Usage of ./observatorium:
7575
The name of the HTTP header containing the tenant ID to forward to the metrics upstreams. (default "THANOS-TENANT")
7676
-metrics.write.endpoint string
7777
The endpoint against which to make write requests for metrics.
78+
-middleware.rate-limiter.grpc-address string
79+
The gRPC Server Address against which to run rate limit checks when the rate limits are specified for a given tenant. If it is not specified local non-shared rate limiting will be used.
7880
-rbac.config string
7981
Path to the RBAC configuration file. (default "rbac.yaml")
8082
-tenants.config string

Diff for: api/logs/v1/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type handlerConfiguration struct {
2929
writeMiddlewares []func(http.Handler) http.Handler
3030
}
3131

32-
// HandlerOption modifies the handler's configuration
32+
// HandlerOption modifies the handler's configuration.
3333
type HandlerOption func(h *handlerConfiguration)
3434

3535
// Logger add a custom logger for the handler to use.

Diff for: api/metrics/v1/http.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type handlerConfiguration struct {
2727
writeMiddlewares []func(http.Handler) http.Handler
2828
}
2929

30-
// HandlerOption modifies the handler's configuration
30+
// HandlerOption modifies the handler's configuration.
3131
type HandlerOption func(h *handlerConfiguration)
3232

3333
// Logger add a custom logger for the handler to use.
@@ -75,7 +75,7 @@ func (n nopInstrumentHandler) NewHandler(labels prometheus.Labels, handler http.
7575
return handler.ServeHTTP
7676
}
7777

78-
// NewHandler creates the new metrics v1 handler
78+
// NewHandler creates the new metrics v1 handler.
7979
func NewHandler(read, write *url.URL, opts ...HandlerOption) http.Handler {
8080
c := &handlerConfiguration{
8181
logger: log.NewNopLogger(),

Diff for: go.mod

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ require (
88
github.com/coreos/go-oidc v2.2.1+incompatible
99
github.com/ghodss/yaml v1.0.0
1010
github.com/go-chi/chi v4.0.2+incompatible
11+
github.com/go-chi/httprate v0.4.0
1112
github.com/go-kit/kit v0.10.0
12-
github.com/golang/protobuf v1.4.0 // indirect
13+
github.com/golang/protobuf v1.4.2
14+
github.com/google/go-cmp v0.5.1 // indirect
15+
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4
16+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1317
github.com/lib/pq v1.3.0 // indirect
1418
github.com/mattn/go-sqlite3 v1.11.0 // indirect
1519
github.com/metalmatze/signal v0.0.0-20201002154727-d0c16e42a3cf
@@ -21,11 +25,16 @@ require (
2125
github.com/prometheus/procfs v0.0.11 // indirect
2226
github.com/prometheus/prometheus v1.8.2-0.20200305080338-7164b58945bb
2327
github.com/spaolacci/murmur3 v1.1.0 // indirect
28+
github.com/stretchr/testify v1.6.1 // indirect
2429
go.uber.org/automaxprocs v1.2.0
25-
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
30+
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 // indirect
2631
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
27-
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
32+
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect
2833
google.golang.org/appengine v1.6.1 // indirect
34+
google.golang.org/genproto v0.0.0-20200413115906-b5235f65be36
35+
google.golang.org/grpc v1.27.1
36+
google.golang.org/protobuf v1.23.0
2937
gopkg.in/square/go-jose.v2 v2.4.1 // indirect
38+
gopkg.in/yaml.v2 v2.3.0 // indirect
3039
k8s.io/component-base v0.18.0
3140
)

0 commit comments

Comments
 (0)