api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
2015-10-12 05:35:22 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
. "github.com/prometheus/alertmanager/test/with_api_v2"
|
2015-10-12 05:35:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSilencing(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := `
|
2015-10-19 14:52:54 +00:00
|
|
|
route:
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: "default"
|
2015-10-19 15:35:59 +00:00
|
|
|
group_by: []
|
2015-10-12 05:35:22 +00:00
|
|
|
group_wait: 1s
|
|
|
|
group_interval: 1s
|
2018-02-09 09:53:46 +00:00
|
|
|
repeat_interval: 1ms
|
2015-10-12 05:35:22 +00:00
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receivers:
|
2015-10-12 05:35:22 +00:00
|
|
|
- name: "default"
|
|
|
|
webhook_configs:
|
|
|
|
- url: 'http://%s'
|
|
|
|
`
|
|
|
|
|
|
|
|
at := NewAcceptanceTest(t, &AcceptanceOpts{
|
|
|
|
Tolerance: 150 * time.Millisecond,
|
|
|
|
})
|
|
|
|
|
|
|
|
co := at.Collector("webhook")
|
|
|
|
wh := NewWebhook(co)
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
|
2015-10-12 05:35:22 +00:00
|
|
|
|
|
|
|
// No repeat interval is configured. Thus, we receive an alert
|
|
|
|
// notification every second.
|
2018-08-04 21:04:00 +00:00
|
|
|
amc.Push(At(1), Alert("alertname", "test1").Active(1))
|
|
|
|
amc.Push(At(1), Alert("alertname", "test2").Active(1))
|
2015-10-12 05:35:22 +00:00
|
|
|
|
|
|
|
co.Want(Between(2, 2.5),
|
|
|
|
Alert("alertname", "test1").Active(1),
|
|
|
|
Alert("alertname", "test2").Active(1),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add a silence that affects the first alert.
|
2018-08-04 21:04:00 +00:00
|
|
|
amc.SetSilence(At(2.3), Silence(2.5, 4.5).Match("alertname", "test1"))
|
2015-10-12 05:35:22 +00:00
|
|
|
|
|
|
|
co.Want(Between(3, 3.5), Alert("alertname", "test2").Active(1))
|
|
|
|
co.Want(Between(4, 4.5), Alert("alertname", "test2").Active(1))
|
|
|
|
|
|
|
|
// Silence should be over now and we receive both alerts again.
|
|
|
|
|
|
|
|
co.Want(Between(5, 5.5),
|
|
|
|
Alert("alertname", "test1").Active(1),
|
|
|
|
Alert("alertname", "test2").Active(1),
|
|
|
|
)
|
|
|
|
|
2015-10-12 05:40:55 +00:00
|
|
|
at.Run()
|
2018-08-04 21:04:00 +00:00
|
|
|
|
|
|
|
t.Log(co.Check())
|
2015-10-12 05:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSilenceDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := `
|
2015-10-19 14:52:54 +00:00
|
|
|
route:
|
2015-11-10 13:08:20 +00:00
|
|
|
receiver: "default"
|
2015-10-19 15:35:59 +00:00
|
|
|
group_by: []
|
2015-10-12 05:40:55 +00:00
|
|
|
group_wait: 1s
|
|
|
|
group_interval: 1s
|
2018-02-09 09:53:46 +00:00
|
|
|
repeat_interval: 1ms
|
2015-10-12 05:40:55 +00:00
|
|
|
|
2015-11-10 13:08:20 +00:00
|
|
|
receivers:
|
2015-10-12 05:40:55 +00:00
|
|
|
- name: "default"
|
|
|
|
webhook_configs:
|
|
|
|
- url: 'http://%s'
|
|
|
|
`
|
|
|
|
|
|
|
|
at := NewAcceptanceTest(t, &AcceptanceOpts{
|
|
|
|
Tolerance: 150 * time.Millisecond,
|
|
|
|
})
|
|
|
|
|
|
|
|
co := at.Collector("webhook")
|
|
|
|
wh := NewWebhook(co)
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
|
2015-10-12 05:40:55 +00:00
|
|
|
|
|
|
|
// No repeat interval is configured. Thus, we receive an alert
|
|
|
|
// notification every second.
|
2018-08-04 21:04:00 +00:00
|
|
|
amc.Push(At(1), Alert("alertname", "test1").Active(1))
|
|
|
|
amc.Push(At(1), Alert("alertname", "test2").Active(1))
|
2015-10-12 05:40:55 +00:00
|
|
|
|
|
|
|
// Silence everything for a long time and delete the silence after
|
|
|
|
// two iterations.
|
2016-08-30 09:58:27 +00:00
|
|
|
sil := Silence(1.5, 100).MatchRE("alertname", ".*")
|
2015-10-12 05:40:55 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
amc.SetSilence(At(1.3), sil)
|
|
|
|
amc.DelSilence(At(3.5), sil)
|
2015-10-12 05:40:55 +00:00
|
|
|
|
|
|
|
co.Want(Between(3.5, 4.5),
|
|
|
|
Alert("alertname", "test1").Active(1),
|
|
|
|
Alert("alertname", "test2").Active(1),
|
|
|
|
)
|
|
|
|
|
2015-10-12 05:35:22 +00:00
|
|
|
at.Run()
|
2018-08-04 21:04:00 +00:00
|
|
|
|
|
|
|
t.Log(co.Check())
|
2015-10-12 05:35:22 +00:00
|
|
|
}
|