Files
grafana/pkg/services/ngalert/migration/models/models.go
T
Matthew Jacobson 2b51f0e263 Alerting: In migration improve deduplication of title and group (#78351)
* Alerting: In migration improve deduplication of title and group

This change improves alert titles generated in the legacy migration 
that occur when we need to deduplicate titles. Now when duplicate 
titles are detected we will first attempt to append a sequential index, 
falling back to a random uid if none are unique within 10 attempts. 
This should cause shorter and more easily readable deduplicated 
titles in most cases.

In addition, groups are no longer deduplicated. Instead we set them 
to a combination of truncated dashboard name and humanized alert 
frequency. This way, alerts from the same dashboard share a group 
if they have the same evaluation interval. In the event that truncation 
causes overlap, it won't be a big issue as all alerts will still be in a 
group with the correct evaluation interval.
2023-11-29 10:05:00 -05:00

105 lines
3.5 KiB
Go

package models
import (
"fmt"
"strings"
"github.com/grafana/grafana/pkg/util"
)
// MaxDeduplicationAttempts is the maximum number of attempts to try to deduplicate a string using any
// individual method, such as sequential index suffixes or uids.
const MaxDeduplicationAttempts = 10
// Deduplicator is a utility for deduplicating strings. It keeps track of the strings it has seen and appends a unique
// suffix to strings that have already been seen. It can optionally truncate strings before appending the suffix to
// ensure that the resulting string is not longer than maxLen.
// This implementation will first try to deduplicate via a sequential index suffix of the form " #2", " #3", etc.
// If after MaxIncrementDeduplicationAttempts attempts it still cannot find a unique string, it will generate a new
// unique uid and append that to the string.
type Deduplicator struct {
set map[string]int
caseInsensitive bool
maxLen int
uidGenerator func() string
}
// NewDeduplicator creates a new deduplicator.
// caseInsensitive determines whether the string comparison should be case-insensitive.
// maxLen determines the maximum length of deduplicated strings. If the deduplicated string would be longer than
// maxLen, it will be truncated.
func NewDeduplicator(caseInsensitive bool, maxLen int) *Deduplicator {
return &Deduplicator{
set: make(map[string]int),
caseInsensitive: caseInsensitive,
maxLen: maxLen,
uidGenerator: util.GenerateShortUID,
}
}
// Deduplicate returns a unique string based on the given base string. If the base string has not already been seen by
// this deduplicator, it will be returned as-is. If the base string has already been seen, a unique suffix will be
// appended to the base string to make it unique.
func (s *Deduplicator) Deduplicate(base string) (string, error) {
if s.maxLen > 0 && len(base) > s.maxLen {
base = base[:s.maxLen]
}
cnt, ok := s.contains(base)
if !ok {
s.add(base, 0)
return base, nil
}
// Start at 2, so we get a, a_2, a_3, etc.
for i := 2 + cnt; i < 2+cnt+MaxDeduplicationAttempts; i++ {
dedup := s.appendSuffix(base, fmt.Sprintf(" #%d", i))
if _, ok := s.contains(dedup); !ok {
s.add(dedup, 0)
return dedup, nil
}
}
// None of the simple suffixes worked, so we generate a new uid. We try a few times, just in case, but this should
// almost always create a unique string on the first try.
for i := 0; i < MaxDeduplicationAttempts; i++ {
dedup := s.appendSuffix(base, fmt.Sprintf("_%s", s.uidGenerator()))
if _, ok := s.contains(dedup); !ok {
s.add(dedup, 0)
return dedup, nil
}
}
return "", fmt.Errorf("failed to deduplicate %q", base)
}
// contains checks whether the given string has already been seen by this deduplicator.
func (s *Deduplicator) contains(u string) (int, bool) {
dedup := u
if s.caseInsensitive {
dedup = strings.ToLower(dedup)
}
if s.maxLen > 0 && len(dedup) > s.maxLen {
dedup = dedup[:s.maxLen]
}
cnt, seen := s.set[dedup]
return cnt, seen
}
// appendSuffix appends the given suffix to the given base string. If the resulting string would be longer than maxLen,
// the base string will be truncated.
func (s *Deduplicator) appendSuffix(base, suffix string) string {
if s.maxLen > 0 && len(base)+len(suffix) > s.maxLen {
return base[:s.maxLen-len(suffix)] + suffix
}
return base + suffix
}
// add adds the given string to the deduplicator.
func (s *Deduplicator) add(uid string, cnt int) {
dedup := uid
if s.caseInsensitive {
dedup = strings.ToLower(dedup)
}
s.set[dedup] = cnt
}