Alerting: Basic support for time_intervals (#83216)

This commit adds basic support for time_intervals, as mute_time_intervals
is deprecated in Alertmanager and scheduled to be removed before 1.0.
It does not add support for time_intervals in API or file provisioning,
nor does it support exporting time intervals. This will be added in
later commits to keep the changes as simple as possible.
This commit is contained in:
George Robinson
2024-02-22 15:58:56 +00:00
committed by GitHub
parent 13cb09b178
commit 1ed1242358
7 changed files with 257 additions and 14 deletions
@@ -739,6 +739,8 @@ func (c *GettableApiAlertingConfig) GetReceivers() []*GettableApiReceiver {
return c.Receivers
}
func (c *GettableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *GettableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval {
return c.MuteTimeIntervals
}
@@ -803,6 +805,7 @@ type Config struct {
Global *config.GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"`
Route *Route `yaml:"route,omitempty" json:"route,omitempty"`
InhibitRules []config.InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"`
TimeIntervals []config.TimeInterval `yaml:"time_intervals,omitempty" json:"time_intervals,omitempty"`
MuteTimeIntervals []config.MuteTimeInterval `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"`
Templates []string `yaml:"templates" json:"templates"`
}
@@ -936,6 +939,15 @@ func (c *Config) UnmarshalJSON(b []byte) error {
}
tiNames := make(map[string]struct{})
for _, ti := range c.TimeIntervals {
if ti.Name == "" {
return fmt.Errorf("missing name in time interval")
}
if _, ok := tiNames[ti.Name]; ok {
return fmt.Errorf("time interval %q is not unique", ti.Name)
}
tiNames[ti.Name] = struct{}{}
}
for _, mt := range c.MuteTimeIntervals {
if mt.Name == "" {
return fmt.Errorf("missing name in mute time interval")
@@ -976,6 +988,8 @@ func (c *PostableApiAlertingConfig) GetReceivers() []*PostableApiReceiver {
return c.Receivers
}
func (c *PostableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *PostableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval {
return c.MuteTimeIntervals
}
@@ -485,7 +485,50 @@ func Test_ConfigUnmashaling(t *testing.T) {
err error
}{
{
desc: "empty mute time name should error",
desc: "missing time interval name should error",
err: errors.New("missing name in time interval"),
input: `
{
"route": {
"receiver": "grafana-default-email"
},
"time_intervals": [
{
"name": "",
"time_intervals": [
{
"times": [
{
"start_time": "00:00",
"end_time": "12:00"
}
]
}
]
}
],
"templates": null,
"receivers": [
{
"name": "grafana-default-email",
"grafana_managed_receiver_configs": [
{
"uid": "uxwfZvtnz",
"name": "email receiver",
"type": "email",
"disableResolveMessage": false,
"settings": {
"addresses": "<example@email.com>"
},
"secureFields": {}
}
]
}
]
}
`,
}, {
desc: "missing mute time interval name should error",
err: errors.New("missing name in mute time interval"),
input: `
{
@@ -529,7 +572,64 @@ func Test_ConfigUnmashaling(t *testing.T) {
`,
},
{
desc: "not unique mute time names should error",
desc: "duplicate time interval names should error",
err: errors.New("time interval \"test1\" is not unique"),
input: `
{
"route": {
"receiver": "grafana-default-email"
},
"time_intervals": [
{
"name": "test1",
"time_intervals": [
{
"times": [
{
"start_time": "00:00",
"end_time": "12:00"
}
]
}
]
},
{
"name": "test1",
"time_intervals": [
{
"times": [
{
"start_time": "00:00",
"end_time": "12:00"
}
]
}
]
}
],
"templates": null,
"receivers": [
{
"name": "grafana-default-email",
"grafana_managed_receiver_configs": [
{
"uid": "uxwfZvtnz",
"name": "email receiver",
"type": "email",
"disableResolveMessage": false,
"settings": {
"addresses": "<example@email.com>"
},
"secureFields": {}
}
]
}
]
}
`,
},
{
desc: "duplicate mute time interval names should error",
err: errors.New("mute time interval \"test1\" is not unique"),
input: `
{
@@ -585,6 +685,65 @@ func Test_ConfigUnmashaling(t *testing.T) {
}
`,
},
{
desc: "duplicate time and mute time interval names should error",
err: errors.New("mute time interval \"test1\" is not unique"),
input: `
{
"route": {
"receiver": "grafana-default-email"
},
"mute_time_intervals": [
{
"name": "test1",
"time_intervals": [
{
"times": [
{
"start_time": "00:00",
"end_time": "12:00"
}
]
}
]
}
],
"time_intervals": [
{
"name": "test1",
"time_intervals": [
{
"times": [
{
"start_time": "00:00",
"end_time": "12:00"
}
]
}
]
}
],
"templates": null,
"receivers": [
{
"name": "grafana-default-email",
"grafana_managed_receiver_configs": [
{
"uid": "uxwfZvtnz",
"name": "email receiver",
"type": "email",
"disableResolveMessage": false,
"settings": {
"addresses": "<example@email.com>"
},
"secureFields": {}
}
]
}
]
}
`,
},
{
desc: "mute time intervals on root route should error",
err: errors.New("root route must not have any mute time intervals"),