Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert: Always convert prom recording rules to Chronosphere recording rules #48

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 9 additions & 56 deletions src/cmd/pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ var (
validNonPreferred = regexp.MustCompile("[_]+")
)

// RecordingGroup is a group of recording rules, used only when use_buckets=true
// and use_declarative_types is true.
type RecordingGroup struct {
Name string
Rules []*models.Configv1RecordingRule
}

// Notifier wraps a models.Configv1Notifier with additional fields that
// were not carried over to the config API.
type Notifier struct {
Expand All @@ -88,7 +81,6 @@ type ConvertedAlerts struct {
NotificationPolicy *models.Configv1NotificationPolicy
Receivers []*Notifier
Monitors []*models.Configv1Monitor
RecordingGroups []*RecordingGroup
RecordingRules []*models.Configv1RecordingRule
}

Expand All @@ -114,7 +106,6 @@ type Opts struct {
ExistsOpNotSupported bool
DisableContinueTrueExpansion bool
UseBuckets bool
UseDeclarativeTypes bool

groupTeamMatcher groupTeamMatcher
}
Expand Down Expand Up @@ -249,7 +240,6 @@ func ConvertPrometheus(
NotificationPolicy: notificationPolicy,
Receivers: alertReceivers,
Monitors: promResult.monitors,
RecordingGroups: promResult.recordingGroups,
RecordingRules: promResult.recordingRules,
}

Expand Down Expand Up @@ -1378,11 +1368,10 @@ func mergeNotifiers(trunk, toMerge *models.RoutesNotifierList) (*models.RoutesNo
}

type convertPrometheusRulesResult struct {
entityGroups []*models.Configv1Collection
collections []*models.Configv1Collection
monitors []*models.Configv1Monitor
recordingGroups []*RecordingGroup
recordingRules []*models.Configv1RecordingRule
entityGroups []*models.Configv1Collection
collections []*models.Configv1Collection
monitors []*models.Configv1Monitor
recordingRules []*models.Configv1RecordingRule
}

// convertPrometheusRuleGroups converts Prometheus rule groups to entity groups, monitors, and recording groups.
Expand All @@ -1405,7 +1394,6 @@ func convertPrometheusRuleGroups(

// used if opts.UseBuckets == true
var entityGroups []*models.Configv1Collection
var recGroups []*RecordingGroup

// used if opts.UseBuckets == false
var collections []*models.Configv1Collection
Expand Down Expand Up @@ -1460,29 +1448,18 @@ func convertPrometheusRuleGroups(
if err != nil {
return nil, fmt.Errorf("convert rules, use_collections=false: %w", err)
}

if len(groupRecRules) > 0 {
if opts.UseDeclarativeTypes {
recGroups = append(recGroups, &RecordingGroup{
Name: entityGroup.Name,
Rules: uniquifyRecordingRuleNames(groupRecRules),
})
} else {
recRules = append(recRules, groupRecRules...)
}
}
recRules = append(recRules, groupRecRules...)

monitors = append(monitors, uniquifyMonitorNames(groupMonitors)...)
}

}

return &convertPrometheusRulesResult{
entityGroups: entityGroups,
monitors: monitors,
recordingGroups: recGroups,
recordingRules: recRules,
collections: collections,
entityGroups: entityGroups,
monitors: monitors,
recordingRules: recRules,
collections: collections,
}, nil
}

Expand Down Expand Up @@ -1647,30 +1624,6 @@ func uniquifyMonitorNames(monitors []*models.Configv1Monitor) []*models.Configv1
return updatedMonitors
}

func uniquifyRecordingRuleNames(groupRules []*models.Configv1RecordingRule) []*models.Configv1RecordingRule {
names := make([]string, 0, len(groupRules))
rulesByName := make(map[string][]*models.Configv1RecordingRule)
updatedRules := make([]*models.Configv1RecordingRule, 0, len(groupRules))
for _, rule := range groupRules {
if _, ok := rulesByName[rule.Name]; !ok {
names = append(names, rule.Name)
}
rulesByName[rule.Name] = append(rulesByName[rule.Name], rule)
}

for _, name := range names {
rules := rulesByName[name]
if len(rules) > 1 {
for i, rule := range rules {
rule.Name = fmt.Sprintf("%s (%d)", rule.Name, i+1)
}
}
updatedRules = append(updatedRules, rules...)
}

return updatedRules
}

func setReceiverSlugs(
receivers []*Notifier,
opts Opts,
Expand Down
Loading