Skip to content

Commit 0f2ba55

Browse files
authored
Merge pull request #25 from cortexproject/alertmanager
Implement alertmanager
2 parents bde969d + 5dc8e4f commit 0f2ba55

File tree

6 files changed

+92
-5
lines changed

6 files changed

+92
-5
lines changed

gateway/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Config struct {
1313
Tenants []Tenant `yaml:"tenants"`
1414
Distributor Upstream `yaml:"distributor"`
1515
QueryFrontend Upstream `yaml:"frontend"`
16+
Alertmanager Upstream `yaml:"alertmanager"`
1617
}
1718

1819
type Upstream struct {

gateway/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func TestInit(t *testing.T) {
4848
"/prometheus/api/v1/query_range",
4949
},
5050
},
51+
Alertmanager: Upstream{
52+
URL: "http://localhost:8083",
53+
Paths: []string{
54+
"/alertmanager/",
55+
"/multitenant_alertmanager/delete_tenant_config",
56+
},
57+
},
5158
},
5259
wantErr: nil,
5360
},

gateway/gateway.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type Gateway struct {
1010
distributorProxy *Proxy
1111
queryFrontendProxy *Proxy
12+
alertmanagerProxy *Proxy
1213
srv *server.Server
1314
}
1415

@@ -38,6 +39,12 @@ var defaultQueryFrontendAPIs = []string{
3839
"/api/prom/api/v1/status/buildinfo",
3940
}
4041

42+
var defaultAlertmanagerAPIs = []string{
43+
"/alertmanager/",
44+
"/api/v1/alerts",
45+
"/multitenant_alertmanager/delete_tenant_config",
46+
}
47+
4148
func New(config *Config, srv *server.Server) (*Gateway, error) {
4249
distributor, err := NewProxy(config.Distributor.URL, config.Distributor, DISTRIBUTOR)
4350
if err != nil {
@@ -49,9 +56,15 @@ func New(config *Config, srv *server.Server) (*Gateway, error) {
4956
return nil, err
5057
}
5158

59+
alertmanager, err := NewProxy(config.Alertmanager.URL, config.Alertmanager, ALERTMANAGER)
60+
if err != nil {
61+
return nil, err
62+
}
63+
5264
return &Gateway{
5365
distributorProxy: distributor,
5466
queryFrontendProxy: frontend,
67+
alertmanagerProxy: alertmanager,
5568
srv: srv,
5669
}, nil
5770
}
@@ -63,6 +76,7 @@ func (g *Gateway) Start(config *Config) {
6376
func (g *Gateway) registerRoutes(config *Config) {
6477
g.registerProxyRoutes(config.Distributor.Paths, defaultDistributorAPIs, http.HandlerFunc(g.distributorProxy.Handler))
6578
g.registerProxyRoutes(config.QueryFrontend.Paths, defaultQueryFrontendAPIs, http.HandlerFunc(g.queryFrontendProxy.Handler))
79+
g.registerProxyRoutes(config.Alertmanager.Paths, defaultAlertmanagerAPIs, http.HandlerFunc(g.alertmanagerProxy.Handler))
6680
g.srv.RegisterTo("/", http.HandlerFunc(g.notFoundHandler), server.UNAUTH)
6781
}
6882

@@ -78,6 +92,6 @@ func (g *Gateway) registerProxyRoutes(paths []string, defaultPaths []string, han
7892
}
7993

8094
func (g *Gateway) notFoundHandler(w http.ResponseWriter, r *http.Request) {
81-
w.WriteHeader(404)
95+
w.WriteHeader(http.StatusNotFound)
8296
w.Write([]byte("404 - Resource not found"))
8397
}

gateway/gateway_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ func TestNewGateway(t *testing.T) {
2222

2323
config := Config{
2424
Distributor: Upstream{
25-
URL: "http://localhost:8000",
25+
URL: "http://localhost:9001",
2626
Paths: nil,
2727
},
2828
QueryFrontend: Upstream{
29-
URL: "http://localhost:9000",
29+
URL: "http://localhost:9002",
30+
Paths: nil,
31+
},
32+
Alertmanager: Upstream{
33+
URL: "http://localhost:9003",
3034
Paths: nil,
3135
},
3236
}
@@ -44,6 +48,7 @@ func TestStartGateway(t *testing.T) {
4448

4549
distributorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
4650
frontendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
51+
alertmanagerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
4752

4853
timeouts := Upstream{
4954
HTTPClientTimeout: 20 * time.Second,
@@ -90,6 +95,15 @@ func TestStartGateway(t *testing.T) {
9095
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
9196
DNSRefreshInterval: timeouts.DNSRefreshInterval,
9297
},
98+
Alertmanager: Upstream{
99+
URL: alertmanagerServer.URL,
100+
Paths: nil,
101+
HTTPClientTimeout: timeouts.HTTPClientTimeout,
102+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout * time.Second,
103+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout * time.Second,
104+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
105+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
106+
},
93107
},
94108
authHeader: "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password")),
95109
paths: []string{
@@ -113,6 +127,9 @@ func TestStartGateway(t *testing.T) {
113127
"/api/prom/api/v1/read",
114128
"/prometheus/api/v1/status/buildinfo",
115129
"/api/prom/api/v1/status/buildinfo",
130+
"/alertmanager/",
131+
"/api/v1/alerts",
132+
"/multitenant_alertmanager/delete_tenant_config",
116133
},
117134
expectedStatus: http.StatusOK,
118135
},
@@ -149,10 +166,22 @@ func TestStartGateway(t *testing.T) {
149166
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
150167
DNSRefreshInterval: timeouts.DNSRefreshInterval,
151168
},
169+
Alertmanager: Upstream{
170+
URL: alertmanagerServer.URL,
171+
Paths: []string{
172+
"/test/alertmanager",
173+
},
174+
HTTPClientTimeout: timeouts.HTTPClientTimeout,
175+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout * time.Second,
176+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout * time.Second,
177+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout * time.Second,
178+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
179+
},
152180
},
153181
paths: []string{
154182
"/test/distributor",
155183
"/test/frontend",
184+
"/test/alertmanager",
156185
},
157186
authHeader: "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password")),
158187
expectedStatus: http.StatusOK,
@@ -176,6 +205,16 @@ func TestStartGateway(t *testing.T) {
176205
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout,
177206
DNSRefreshInterval: timeouts.DNSRefreshInterval,
178207
},
208+
Alertmanager: Upstream{
209+
URL: alertmanagerServer.URL,
210+
Paths: []string{
211+
"/test/alertmanager",
212+
},
213+
HTTPClientDialerTimeout: timeouts.HTTPClientDialerTimeout,
214+
HTTPClientTLSHandshakeTimeout: timeouts.HTTPClientTLSHandshakeTimeout,
215+
HTTPClientResponseHeaderTimeout: timeouts.HTTPClientResponseHeaderTimeout,
216+
DNSRefreshInterval: timeouts.DNSRefreshInterval,
217+
},
179218
},
180219
paths: []string{
181220
"/not/found",
@@ -197,6 +236,20 @@ func TestStartGateway(t *testing.T) {
197236
},
198237
expectedErr: errors.New("invalid URL scheme:"),
199238
},
239+
{
240+
name: "invalid alertmanager proxy",
241+
config: &Config{
242+
Distributor: Upstream{
243+
URL: distributorServer.URL,
244+
Paths: []string{},
245+
},
246+
QueryFrontend: Upstream{
247+
URL: frontendServer.URL,
248+
Paths: []string{},
249+
},
250+
},
251+
expectedErr: errors.New("invalid URL scheme:"),
252+
},
200253
}
201254

202255
for _, tc := range testCases {

gateway/proxy.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
const (
14-
DISTRIBUTOR = "distributor"
15-
FRONTEND = "frontend"
14+
DISTRIBUTOR = "distributor"
15+
FRONTEND = "frontend"
16+
ALERTMANAGER = "alertmanager"
1617
)
1718

1819
var defaultTimeoutValues map[string]Upstream = map[string]Upstream{
@@ -28,6 +29,12 @@ var defaultTimeoutValues map[string]Upstream = map[string]Upstream{
2829
HTTPClientTLSHandshakeTimeout: time.Second * 5,
2930
HTTPClientResponseHeaderTimeout: time.Second * 5,
3031
},
32+
ALERTMANAGER: {
33+
HTTPClientTimeout: time.Second * 15,
34+
HTTPClientDialerTimeout: time.Second * 5,
35+
HTTPClientTLSHandshakeTimeout: time.Second * 5,
36+
HTTPClientResponseHeaderTimeout: time.Second * 5,
37+
},
3138
}
3239

3340
type Proxy struct {

gateway/testdata/valid.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ frontend:
1919
paths:
2020
- /api/prom/api/v1/query
2121
- /prometheus/api/v1/query_range
22+
alertmanager:
23+
url: http://localhost:8083
24+
paths:
25+
- /alertmanager/
26+
- /multitenant_alertmanager/delete_tenant_config

0 commit comments

Comments
 (0)