Skip to content

Commit 0a903a8

Browse files
authored
Merge pull request #26 from cortexproject/ruler
Implement ruler proxy
2 parents 0f2ba55 + abd59a2 commit 0a903a8

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

gateway/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config struct {
1414
Distributor Upstream `yaml:"distributor"`
1515
QueryFrontend Upstream `yaml:"frontend"`
1616
Alertmanager Upstream `yaml:"alertmanager"`
17+
Ruler Upstream `yaml:"ruler"`
1718
}
1819

1920
type Upstream struct {

gateway/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func TestInit(t *testing.T) {
5555
"/multitenant_alertmanager/delete_tenant_config",
5656
},
5757
},
58+
Ruler: Upstream{
59+
URL: "http://localhost:8084",
60+
Paths: []string{
61+
"/prometheus/api/v1/rules",
62+
"/api/prom/api/v1/alerts",
63+
},
64+
},
5865
},
5966
wantErr: nil,
6067
},

gateway/gateway.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Gateway struct {
1010
distributorProxy *Proxy
1111
queryFrontendProxy *Proxy
1212
alertmanagerProxy *Proxy
13+
ruler *Proxy
1314
srv *server.Server
1415
}
1516

@@ -45,6 +46,17 @@ var defaultAlertmanagerAPIs = []string{
4546
"/multitenant_alertmanager/delete_tenant_config",
4647
}
4748

49+
var defaultRulerAPIs = []string{
50+
"/prometheus/api/v1/rules",
51+
"/api/prom/api/v1/rules",
52+
"/prometheus/api/v1/alerts",
53+
"/api/prom/api/v1/alerts",
54+
"/api/v1/rules",
55+
"/api/v1/rules/",
56+
"/api/prom/rules/",
57+
"/ruler/delete_tenant_config",
58+
}
59+
4860
func New(config *Config, srv *server.Server) (*Gateway, error) {
4961
distributor, err := NewProxy(config.Distributor.URL, config.Distributor, DISTRIBUTOR)
5062
if err != nil {
@@ -61,10 +73,16 @@ func New(config *Config, srv *server.Server) (*Gateway, error) {
6173
return nil, err
6274
}
6375

76+
ruler, err := NewProxy(config.Ruler.URL, config.Ruler, RULER)
77+
if err != nil {
78+
return nil, err
79+
}
80+
6481
return &Gateway{
6582
distributorProxy: distributor,
6683
queryFrontendProxy: frontend,
6784
alertmanagerProxy: alertmanager,
85+
ruler: ruler,
6886
srv: srv,
6987
}, nil
7088
}
@@ -77,6 +95,7 @@ func (g *Gateway) registerRoutes(config *Config) {
7795
g.registerProxyRoutes(config.Distributor.Paths, defaultDistributorAPIs, http.HandlerFunc(g.distributorProxy.Handler))
7896
g.registerProxyRoutes(config.QueryFrontend.Paths, defaultQueryFrontendAPIs, http.HandlerFunc(g.queryFrontendProxy.Handler))
7997
g.registerProxyRoutes(config.Alertmanager.Paths, defaultAlertmanagerAPIs, http.HandlerFunc(g.alertmanagerProxy.Handler))
98+
g.registerProxyRoutes(config.Ruler.Paths, defaultRulerAPIs, http.HandlerFunc(g.ruler.Handler))
8099
g.srv.RegisterTo("/", http.HandlerFunc(g.notFoundHandler), server.UNAUTH)
81100
}
82101

gateway/gateway_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func TestNewGateway(t *testing.T) {
3333
URL: "http://localhost:9003",
3434
Paths: nil,
3535
},
36+
Ruler: Upstream{
37+
URL: "http://localhost:9004",
38+
Paths: nil,
39+
},
3640
}
3741

3842
gw, err := New(&config, srv)
@@ -49,6 +53,7 @@ func TestStartGateway(t *testing.T) {
4953
distributorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5054
frontendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5155
alertmanagerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
56+
rulerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5257

5358
timeouts := Upstream{
5459
HTTPClientTimeout: 20 * time.Second,
@@ -104,6 +109,15 @@ func TestStartGateway(t *testing.T) {
104109
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
105110
DNSRefreshInterval: timeouts.DNSRefreshInterval,
106111
},
112+
Ruler: Upstream{
113+
URL: rulerServer.URL,
114+
Paths: nil,
115+
HTTPClientTimeout: timeouts.HTTPClientTimeout,
116+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout * time.Second,
117+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout * time.Second,
118+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
119+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
120+
},
107121
},
108122
authHeader: "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password")),
109123
paths: []string{
@@ -130,6 +144,11 @@ func TestStartGateway(t *testing.T) {
130144
"/alertmanager/",
131145
"/api/v1/alerts",
132146
"/multitenant_alertmanager/delete_tenant_config",
147+
"/prometheus/api/v1/rules",
148+
"/api/prom/api/v1/rules",
149+
"/prometheus/api/v1/alerts",
150+
"/api/prom/api/v1/alerts",
151+
"/api/v1/rules/",
133152
},
134153
expectedStatus: http.StatusOK,
135154
},
@@ -177,11 +196,23 @@ func TestStartGateway(t *testing.T) {
177196
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
178197
DNSRefreshInterval: timeouts.DNSRefreshInterval,
179198
},
199+
Ruler: Upstream{
200+
URL: rulerServer.URL,
201+
Paths: []string{
202+
"/test/ruler",
203+
},
204+
HTTPClientTimeout: timeouts.HTTPClientTimeout,
205+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout * time.Second,
206+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout * time.Second,
207+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
208+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
209+
},
180210
},
181211
paths: []string{
182212
"/test/distributor",
183213
"/test/frontend",
184214
"/test/alertmanager",
215+
"/test/ruler",
185216
},
186217
authHeader: "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password")),
187218
expectedStatus: http.StatusOK,
@@ -215,6 +246,17 @@ func TestStartGateway(t *testing.T) {
215246
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout,
216247
DNSRefreshInterval: timeouts.DNSRefreshInterval,
217248
},
249+
Ruler: Upstream{
250+
URL: rulerServer.URL,
251+
Paths: []string{
252+
"/test/ruler",
253+
},
254+
HTTPClientTimeout: timeouts.HTTPClientTimeout,
255+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout * time.Second,
256+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout * time.Second,
257+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
258+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
259+
},
218260
},
219261
paths: []string{
220262
"/not/found",
@@ -250,6 +292,24 @@ func TestStartGateway(t *testing.T) {
250292
},
251293
expectedErr: errors.New("invalid URL scheme:"),
252294
},
295+
{
296+
name: "invalid ruler proxy",
297+
config: &Config{
298+
Distributor: Upstream{
299+
URL: distributorServer.URL,
300+
Paths: []string{},
301+
},
302+
QueryFrontend: Upstream{
303+
URL: frontendServer.URL,
304+
Paths: []string{},
305+
},
306+
Alertmanager: Upstream{
307+
URL: alertmanagerServer.URL,
308+
Paths: []string{},
309+
},
310+
},
311+
expectedErr: errors.New("invalid URL scheme:"),
312+
},
253313
}
254314

255315
for _, tc := range testCases {

gateway/proxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
DISTRIBUTOR = "distributor"
1515
FRONTEND = "frontend"
1616
ALERTMANAGER = "alertmanager"
17+
RULER = "ruler"
1718
)
1819

1920
var defaultTimeoutValues map[string]Upstream = map[string]Upstream{
@@ -35,6 +36,12 @@ var defaultTimeoutValues map[string]Upstream = map[string]Upstream{
3536
HTTPClientTLSHandshakeTimeout: time.Second * 5,
3637
HTTPClientResponseHeaderTimeout: time.Second * 5,
3738
},
39+
RULER: {
40+
HTTPClientTimeout: time.Second * 15,
41+
HTTPClientDialerTimeout: time.Second * 5,
42+
HTTPClientTLSHandshakeTimeout: time.Second * 5,
43+
HTTPClientResponseHeaderTimeout: time.Second * 5,
44+
},
3845
}
3946

4047
type Proxy struct {

gateway/testdata/valid.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ alertmanager:
2424
paths:
2525
- /alertmanager/
2626
- /multitenant_alertmanager/delete_tenant_config
27+
ruler:
28+
url: http://localhost:8084
29+
paths:
30+
- /prometheus/api/v1/rules
31+
- /api/prom/api/v1/alerts

0 commit comments

Comments
 (0)