Files
Pulse/internal/alerts/config/update.go
rcourtman f7ac26b573 Keep alert settings a config save leaves out
The Thresholds page builds its PUT /api/alerts/config body from the
settings it edits. It has no control for flapping detection, alert TTL
cleanup or custom rules, so it never sends flappingEnabled,
maxAlertAgeDays, maxAcknowledgedAgeDays, autoAcknowledgeAfterHours or
customRules. The handler decoded the body into a zero AlertConfig, so
every thresholds save turned flapping detection and TTL cleanup off,
reset the flapping tuning, dropped custom rules, and persisted that.
The page's flapping, aggregation and ioNormalization objects are not
AlertConfig fields and never enabled anything.

The handler now hands the body to Manager.ApplyConfigUpdate, which
reads the stored config, merges and applies it under the manager lock,
so two concurrent partial saves cannot revert each other. The body is
decoded on its own, so the keys it carries mean exactly what
encoding/json makes of them; every top-level field it left out is
copied from a JSON round trip of the stored config, sharing no maps
with it. ApplyConfigUpdate returns a cloned snapshot, which the handler
persists, because SaveAlertConfig normalizes the config it is handed in
place and used to write into the live config's maps. Saves run one at
a time so alerts.json ends with the last applied config. A body that
is not a JSON object, null included, is a 400. Stored configs are not
rewritten, so a value an earlier save zeroed stays as it is.

On a private stack, a node CPU change saved from the Thresholds page
zeroed all four fields in memory and in alerts.json on main; with this
change the same save kept them and stored the new threshold.
2026-10-07 16:31:52 +01:00

91 lines
2.8 KiB
Go

package config
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
var errAlertConfigUpdateNotObject = errors.New("alert configuration update must be a JSON object")
// ApplyAlertConfigUpdate decodes a client's alert configuration update onto
// the stored configuration. A top-level key the client sent replaces that
// setting whole; a key it left out keeps the stored value.
//
// Clients only send what they know about. The settings page has no control
// for flapping detection, alert TTL cleanup or custom rules, and an older
// browser bundle or API script predates any field added since. Decoding the
// body into a zero AlertConfig used to turn every one of those settings off
// on each save.
//
// The body is decoded on its own, so the keys it carries mean exactly what
// encoding/json makes of them. Each setting it left out is copied from a JSON
// round trip of stored, so the result shares no maps or slices with it.
func ApplyAlertConfigUpdate(stored AlertConfig, update []byte) (AlertConfig, error) {
var sent map[string]json.RawMessage
if err := json.Unmarshal(update, &sent); err != nil {
return AlertConfig{}, err
}
if sent == nil {
return AlertConfig{}, errAlertConfigUpdateNotObject
}
var result AlertConfig
if err := json.Unmarshal(update, &result); err != nil {
return AlertConfig{}, err
}
kept, err := CloneAlertConfig(stored)
if err != nil {
return AlertConfig{}, err
}
resultValue := reflect.ValueOf(&result).Elem()
keptValue := reflect.ValueOf(kept)
for i := 0; i < resultValue.NumField(); i++ {
if !alertConfigKeySent(sent, alertConfigJSONKey(resultValue.Type().Field(i))) {
resultValue.Field(i).Set(keptValue.Field(i))
}
}
return result, nil
}
// CloneAlertConfig returns a copy of config that shares no maps, slices or
// pointers with it, by the same JSON round trip config takes through
// alerts.json.
func CloneAlertConfig(config AlertConfig) (AlertConfig, error) {
data, err := json.Marshal(config)
if err != nil {
return AlertConfig{}, fmt.Errorf("encode alert config: %w", err)
}
var clone AlertConfig
if err := json.Unmarshal(data, &clone); err != nil {
return AlertConfig{}, fmt.Errorf("decode alert config: %w", err)
}
return clone, nil
}
// alertConfigJSONKey returns the key encoding/json reads a field from.
func alertConfigJSONKey(field reflect.StructField) string {
name, _, _ := strings.Cut(field.Tag.Get("json"), ",")
if name == "" {
return field.Name
}
return name
}
// alertConfigKeySent matches keys the way encoding/json matches them to
// fields: exactly, or else case-insensitively.
func alertConfigKeySent(sent map[string]json.RawMessage, key string) bool {
if _, ok := sent[key]; ok {
return true
}
for sentKey := range sent {
if strings.EqualFold(sentKey, key) {
return true
}
}
return false
}