Skip to content

Commit

Permalink
feat: add default alertmanagers to silence form
Browse files Browse the repository at this point in the history
That will allow setting some default alertmanagers to select when
creating a new silence, overriding the current behavior of getting all
the ones that are not readonly.

Signed-off-by: David Caro <dcaro@wikimedia.org>
  • Loading branch information
david-caro committed Mar 6, 2023
1 parent 9e402bf commit d726d6b
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/059_log_full_config_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ level=info msg=" labels:"
level=info msg=" - job"
level=info msg=" - severity"
level=info msg=" - region"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 10s"
level=info msg=" hideFiltersWhenIdle: false"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/060_log_full_config_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ level=info msg=" labels:"
level=info msg=" - job"
level=info msg=" - severity"
level=info msg=" - region"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 10s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/065_proxy-with-readonly.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ level=info msg=" rules: []"
level=info msg="silenceForm:"
level=info msg=" strip:"
level=info msg=" labels: []"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 30s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/066_proxy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ level=info msg=" rules: []"
level=info msg="silenceForm:"
level=info msg=" strip:"
level=info msg=" labels: []"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 30s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/067_readonly.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ level=info msg=" rules: []"
level=info msg="silenceForm:"
level=info msg=" strip:"
level=info msg=" labels: []"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 30s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/070_upper_case_keys.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ level=info msg=" rules: []"
level=info msg="silenceForm:"
level=info msg=" strip:"
level=info msg=" labels: []"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 30s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/tests/testscript/097_proxy_url_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ level=info msg=" rules: []"
level=info msg="silenceForm:"
level=info msg=" strip:"
level=info msg=" labels: []"
level=info msg=" defaultAlertmanagers: []"
level=info msg="ui:"
level=info msg=" refresh: 30s"
level=info msg=" hideFiltersWhenIdle: true"
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func alerts(w http.ResponseWriter, r *http.Request) {
Strip: models.SilenceFormStripSettings{
Labels: config.Config.SilenceForm.Strip.Labels,
},
DefaultAlertmanagers: config.Config.SilenceForm.DefaultAlertmanagers,
},
AlertAcknowledgement: models.AlertAcknowledgementSettings{
Enabled: config.Config.AlertAcknowledgement.Enabled,
Expand Down
1 change: 1 addition & 0 deletions cmd/karma/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ func TestEmptySettings(t *testing.T) {
Strip: models.SilenceFormStripSettings{
Labels: []string{},
},
DefaultAlertmanagers: []string{},
},
AlertAcknowledgement: models.AlertAcknowledgementSettings{
Enabled: false,
Expand Down
2 changes: 2 additions & 0 deletions demo/karma.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ silenceForm:
- job
- severity
- region
defaultAlertmanagers:
- single
ui:
refresh: 10s
hideFiltersWhenIdle: true
Expand Down
13 changes: 13 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,19 @@ silenceForm:
- job
```

- `defaultAlertmanagers` - list of Alertmanager names that will be used as
default when creating a new silence.

Example where alertmanagers `prod1` and `prod2` will be the default ones when
creating a new silence

```YAML
silenceForm:
defaultAlertmanagers:
- prod1
- prod2
```

## UI defaults

`ui` section allows configuring default values for UI settings controled via the
Expand Down
2 changes: 2 additions & 0 deletions docs/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ silenceForm:
strip:
labels:
- job
defaultAlertmanagers:
- local
ui:
refresh: 30s
hideFiltersWhenIdle: true
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func SetupFlags(f *pflag.FlagSet) {

f.Duration("silences.expired", time.Minute*10, "Maximum age of expired silences to show on active alerts")
f.StringSlice("silenceform.strip.labels", []string{}, "List of labels to ignore when auto-filling silence form from alerts")
f.StringSlice("silenceform.defaultAlertmanagers", []string{}, "List of Alertmanager names to use as default when creating a new silence")

f.String("listen.address", "", "IP/Hostname to listen on")
f.Int("listen.port", 8080, "HTTP port to listen on")
Expand Down Expand Up @@ -236,6 +237,8 @@ func readEnvVariables(k *koanf.Koanf) {
return "labels.valueOnly_re"
case "SILENCEFORM_STRIP_LABELS":
return "silenceForm.strip.labels"
case "SILENCEFORM_DEFAULTALERTMANAGERS":
return "silenceForm.defaultAlertmanagers"
case "UI_HIDEFILTERSWHENIDLE":
return "ui.hideFiltersWhenIdle"
case "UI_COLORTITLEBAR":
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ silences:
silenceForm:
strip:
labels: []
defaultAlertmanagers: []
ui:
refresh: 30s
hideFiltersWhenIdle: true
Expand Down Expand Up @@ -393,6 +394,7 @@ func TestDefaultConfig(t *testing.T) {
expectedConfig.Receivers.Keep = []string{}
expectedConfig.Receivers.Strip = []string{}
expectedConfig.SilenceForm.Strip.Labels = []string{}
expectedConfig.SilenceForm.DefaultAlertmanagers = []string{}

if diff := cmp.Diff(expectedConfig.Annotations, Config.Annotations); diff != "" {
t.Errorf("Wrong annotations config returned (-want +got):\n%s", diff)
Expand All @@ -406,6 +408,9 @@ func TestDefaultConfig(t *testing.T) {
if diff := cmp.Diff(expectedConfig.SilenceForm.Strip, Config.SilenceForm.Strip); diff != "" {
t.Errorf("Wrong silence form config returned (-want +got):\n%s", diff)
}
if diff := cmp.Diff(expectedConfig.SilenceForm.DefaultAlertmanagers, Config.SilenceForm.DefaultAlertmanagers); diff != "" {
t.Errorf("Wrong defaultAlertmanagers form config returned (-want +got):\n%s", diff)
}
}

func TestValidateConfigMissingFile(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type configSchema struct {
Strip struct {
Labels []string
}
DefaultAlertmanagers []string `yaml:"defaultAlertmanagers" koanf:"defaultAlertmanagers"`
} `yaml:"silenceForm" koanf:"silenceForm"`
// nolint: maligned
UI struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ type SilenceFormStripSettings struct {
}

type SilenceFormSettings struct {
Strip SilenceFormStripSettings `json:"strip"`
Strip SilenceFormStripSettings `json:"strip"`
DefaultAlertmanagers []string `json:"defaultAlertmanagers"`
}

type AlertAcknowledgementSettings struct {
Expand Down
13 changes: 12 additions & 1 deletion ui/src/Components/SilenceModal/AlertManagerInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ const AlertManagerInput: FC<{
}> = observer(({ alertStore, silenceFormStore }) => {
useEffect(() => {
if (silenceFormStore.data.alertmanagers.length === 0) {
// get only the clusters that match the defaults, or all of them if there are no defaults
silenceFormStore.data.setAlertmanagers(
AlertmanagerClustersToOption(alertStore.data.clustersWithoutReadOnly)
AlertmanagerClustersToOption(
alertStore.data.clustersWithoutReadOnly
).filter((am) => {
return (
alertStore.settings.values.silenceForm.defaultAlertmanagers.some(
(amName) => am.value.includes(amName)
) ||
alertStore.settings.values.silenceForm.defaultAlertmanagers
.length === 0
);
})
);
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
Expand Down
1 change: 1 addition & 0 deletions ui/src/Models/APITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface APISettingsT {
strip: {
labels: string[];
};
defaultAlertmanagers: string[];
};
alertAcknowledgement: {
enabled: boolean;
Expand Down
1 change: 1 addition & 0 deletions ui/src/Stores/AlertStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ class AlertStore {
strip: {
labels: [] as string[],
},
defaultAlertmanagers: [] as string[],
},
alertAcknowledgement: {
enabled: false as boolean,
Expand Down
1 change: 1 addition & 0 deletions ui/src/__fixtures__/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const EmptyAPIResponse = (): APIAlertsResponseT => ({
strip: {
labels: [],
},
defaultAlertmanagers: [],
},
alertAcknowledgement: {
enabled: false,
Expand Down

0 comments on commit d726d6b

Please sign in to comment.