Skip to content

Commit

Permalink
Merge pull request #792 from cloudflare/comment
Browse files Browse the repository at this point in the history
Move comments to a single slice per rule
  • Loading branch information
prymitive authored Nov 24, 2023
2 parents 77734be + eef6745 commit f11d931
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 98 deletions.
89 changes: 13 additions & 76 deletions internal/parser/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ func mergeComments(node *yaml.Node) (comments []string) {
if node.FootComment != "" {
comments = append(comments, node.FootComment)
}
for _, child := range node.Content {
comments = append(comments, mergeComments(child)...)
}
return comments
}

type YamlNode struct {
Position FilePosition
Value string
Comments []string
}

func newYamlNode(node *yaml.Node, offset int) *YamlNode {
return &YamlNode{
Position: NewFilePosition(nodeLines(node, offset)),
Value: node.Value,
Comments: mergeComments(node),
}
}

Expand Down Expand Up @@ -242,36 +243,6 @@ func (ar AlertingRule) Lines() (lines []int) {
return lines
}

func (ar AlertingRule) Comments() (comments []string) {
comments = append(comments, ar.Alert.Key.Comments...)
comments = append(comments, ar.Alert.Value.Comments...)
comments = append(comments, ar.Expr.Key.Comments...)
comments = append(comments, ar.Expr.Value.Comments...)
if ar.For != nil {
comments = append(comments, ar.For.Key.Comments...)
comments = append(comments, ar.For.Value.Comments...)
}
if ar.KeepFiringFor != nil {
comments = append(comments, ar.KeepFiringFor.Key.Comments...)
comments = append(comments, ar.KeepFiringFor.Value.Comments...)
}
if ar.Labels != nil {
comments = append(comments, ar.Labels.Key.Comments...)
for _, label := range ar.Labels.Items {
comments = append(comments, label.Key.Comments...)
comments = append(comments, label.Value.Comments...)
}
}
if ar.Annotations != nil {
comments = append(comments, ar.Annotations.Key.Comments...)
for _, annotation := range ar.Annotations.Items {
comments = append(comments, annotation.Key.Comments...)
comments = append(comments, annotation.Value.Comments...)
}
}
return comments
}

type RecordingRule struct {
Record YamlKeyValue
Expr PromQLExpr
Expand All @@ -288,21 +259,6 @@ func (rr RecordingRule) Lines() (lines []int) {
return lines
}

func (rr RecordingRule) Comments() (comments []string) {
comments = append(comments, rr.Record.Key.Comments...)
comments = append(comments, rr.Record.Value.Comments...)
comments = append(comments, rr.Expr.Key.Comments...)
comments = append(comments, rr.Expr.Value.Comments...)
if rr.Labels != nil {
comments = append(comments, rr.Labels.Key.Comments...)
for _, label := range rr.Labels.Items {
comments = append(comments, label.Key.Comments...)
comments = append(comments, label.Value.Comments...)
}
}
return comments
}

type ParseError struct {
Fragment string
Err error
Expand All @@ -312,6 +268,7 @@ type ParseError struct {
type Rule struct {
AlertingRule *AlertingRule
RecordingRule *RecordingRule
Comments []string
Error ParseError
}

Expand All @@ -325,11 +282,13 @@ func (r Rule) ToYAML() string {
}

var b strings.Builder

for _, c := range r.Comments {
b.WriteString(c)
b.WriteRune('\n')
}

if r.AlertingRule != nil {
for _, c := range r.AlertingRule.Comments() {
b.WriteString(c)
b.WriteRune('\n')
}
b.WriteString("- ")
b.WriteString(r.AlertingRule.Alert.Key.Value)
b.WriteString(": ")
Expand Down Expand Up @@ -382,10 +341,6 @@ func (r Rule) ToYAML() string {
return b.String()
}

for _, c := range r.RecordingRule.Comments() {
b.WriteString(c)
b.WriteRune('\n')
}
b.WriteString("- ")
b.WriteString(r.RecordingRule.Record.Key.Value)
b.WriteString(": ")
Expand Down Expand Up @@ -482,13 +437,7 @@ func (r Rule) LineRange() []int {
}

func (r Rule) HasComment(comment string) bool {
var comments []string
if r.RecordingRule != nil {
comments = r.RecordingRule.Comments()
} else if r.AlertingRule != nil {
comments = r.AlertingRule.Comments()
}
for _, c := range comments {
for _, c := range r.Comments {
if hasComment(c, comment) {
return true
}
Expand All @@ -497,13 +446,7 @@ func (r Rule) HasComment(comment string) bool {
}

func (r Rule) GetComment(comment ...string) (s Comment, ok bool) {
var comments []string
if r.RecordingRule != nil {
comments = r.RecordingRule.Comments()
} else if r.AlertingRule != nil {
comments = r.AlertingRule.Comments()
}
for _, c := range comments {
for _, c := range r.Comments {
var val Comment
if val, ok = GetLastComment(c, comment...); ok {
return val, ok
Expand All @@ -513,13 +456,7 @@ func (r Rule) GetComment(comment ...string) (s Comment, ok bool) {
}

func (r Rule) GetComments(key string) (cs []Comment) {
var comments []string
if r.RecordingRule != nil {
comments = r.RecordingRule.Comments()
} else if r.AlertingRule != nil {
comments = r.AlertingRule.Comments()
}
for _, c := range comments {
for _, c := range r.Comments {
sc := bufio.NewScanner(strings.NewReader(c))
for sc.Scan() {
if val, ok := GetLastComment(sc.Text(), key); ok {
Expand Down
40 changes: 25 additions & 15 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ func parseRule(content []byte, node *yaml.Node, offset int) (rule Rule, _ bool,
var key *yaml.Node
unknownKeys := []*yaml.Node{}

var comments []string

for i, part := range unpackNodes(node) {
if i == 0 && node.HeadComment != "" {
if i == 0 && node.HeadComment != "" && part.HeadComment == "" {
part.HeadComment = node.HeadComment
}
if i == len(node.Content)-1 && node.FootComment != "" {
if i == len(node.Content)-1 && node.FootComment != "" && part.HeadComment == "" {
part.FootComment = node.FootComment
}
comments = append(comments, mergeComments(part)...)

if i%2 == 0 {
key = part
} else {
Expand Down Expand Up @@ -229,23 +233,29 @@ func parseRule(content []byte, node *yaml.Node, offset int) (rule Rule, _ bool,
}

if recordPart != nil && exprPart != nil {
rule = Rule{RecordingRule: &RecordingRule{
Record: *recordPart,
Expr: *exprPart,
Labels: labelsPart,
}}
rule = Rule{
RecordingRule: &RecordingRule{
Record: *recordPart,
Expr: *exprPart,
Labels: labelsPart,
},
Comments: comments,
}
return rule, false, err
}

if alertPart != nil && exprPart != nil {
rule = Rule{AlertingRule: &AlertingRule{
Alert: *alertPart,
Expr: *exprPart,
For: forPart,
KeepFiringFor: keepFiringForPart,
Labels: labelsPart,
Annotations: annotationsPart,
}}
rule = Rule{
AlertingRule: &AlertingRule{
Alert: *alertPart,
Expr: *exprPart,
For: forPart,
KeepFiringFor: keepFiringForPart,
Labels: labelsPart,
Annotations: annotationsPart,
},
Comments: comments,
}
return rule, false, err
}

Expand Down
16 changes: 9 additions & 7 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,23 @@ func TestParse(t *testing.T) {
`),
output: []parser.Rule{
{
Comments: []string{
"# head comment",
"# record comment",
"# expr comment",
"# pre-labels comment",
"# pre-foo comment",
"# post-foo comment",
},
RecordingRule: &parser.RecordingRule{
Record: parser.YamlKeyValue{
Key: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{3}},
Value: "record",
Comments: []string{"# head comment"},
},
Value: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{3}},
Value: "foo",
Comments: []string{"# record comment"},
},
},
Expr: parser.PromQLExpr{
Expand All @@ -273,7 +279,6 @@ func TestParse(t *testing.T) {
Value: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{4}},
Value: "foo offset 10m",
Comments: []string{"# expr comment"},
},
Query: &parser.PromQLNode{
Expr: "foo offset 10m",
Expand All @@ -283,14 +288,12 @@ func TestParse(t *testing.T) {
Key: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{6}},
Value: "labels",
Comments: []string{"# pre-labels comment"},
},
Items: []*parser.YamlKeyValue{
{
Key: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{8}},
Value: "foo",
Comments: []string{"# pre-foo comment"},
},
Value: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{8}},
Expand All @@ -301,7 +304,6 @@ func TestParse(t *testing.T) {
Key: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{10}},
Value: "bob",
Comments: []string{"# post-foo comment"},
},
Value: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{10}},
Expand Down Expand Up @@ -1164,6 +1166,7 @@ data:
},
},
{
Comments: []string{"# foot comment"},
RecordingRule: &parser.RecordingRule{
Record: parser.YamlKeyValue{
Key: &parser.YamlNode{
Expand All @@ -1190,7 +1193,6 @@ data:
Key: &parser.YamlNode{
Position: parser.FilePosition{Lines: []int{11}},
Value: "labels",
Comments: []string{"# foot comment"},
},
Items: []*parser.YamlKeyValue{
{
Expand Down

0 comments on commit f11d931

Please sign in to comment.