Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions pkg/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
ErrRootNodeWithoutEventSrc = errors.New("root node has no event source")
ErrInvalidWindow = errors.New("invalid window")
ErrMissingOrigin = errors.New("missing origin event")
ErrMultipleOrigin = errors.New("multiple origin events")
ErrInvalidAnchor = errors.New("invalid negate anchor")
ErrNoTermIdx = errors.New("no term idx")
)
Expand Down Expand Up @@ -87,14 +88,14 @@ type AstEventT struct {
type builderT struct {
CurrentNodeId uint32
CurrentDepth uint32
HasOrigin bool
OriginCnt int
}

func NewBuilder() *builderT {
return &builderT{
CurrentNodeId: uint32(0),
CurrentDepth: uint32(0),
HasOrigin: false,
OriginCnt: 0,
}
}

Expand Down Expand Up @@ -140,8 +141,11 @@ func BuildTree(tree *parser.TreeT) (*AstT, error) {
return nil, err
}

if !rb.HasOrigin {
switch {
case rb.OriginCnt == 0:
return nil, parserNode.WrapError(ErrMissingOrigin)
case rb.OriginCnt > 1:
return nil, parserNode.WrapError(ErrMultipleOrigin)
}

ast.Nodes = append(ast.Nodes, rule)
Expand Down Expand Up @@ -235,7 +239,7 @@ func (b *builderT) buildMatcherChildren(parserNode *parser.NodeT, machineAddress
}

// Implied that the root node has an origin event
b.HasOrigin = true
b.OriginCnt++
parserNode.Metadata.Event.Origin = true

err = b.descendTree(func() error {
Expand Down Expand Up @@ -316,7 +320,7 @@ func (b *builderT) buildMachineChildren(parserNode *parser.NodeT, machineAddress
// If the child has an event/data source, then it is not a state machine. Build it via buildMatcherNodes

if parserChildNode.Metadata.Event.Origin {
b.HasOrigin = true
b.OriginCnt++
}

if parserChildNode.Metadata.Event.Source == "" {
Expand Down
3 changes: 0 additions & 3 deletions pkg/ast/ast_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func (b *builderT) buildPromQLNode(parserNode *parser.NodeT, machineAddress *Ast
}

if parserNode.Metadata.Event != nil {
if parserNode.Metadata.Event.Origin {
b.HasOrigin = true
}
pn.Event = &AstEventT{
Source: parserNode.Metadata.Event.Source,
Origin: parserNode.Metadata.Event.Origin,
Expand Down
6 changes: 6 additions & 0 deletions pkg/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ func TestAstFail(t *testing.T) {
line: 11,
col: 9,
},
"Fail_MultipleOrigin": {
rule: testdata.TestFailMultipleOrigin,
err: ErrMultipleOrigin,
line: 11,
col: 17,
},
}

for name, test := range tests {
Expand Down
26 changes: 26 additions & 0 deletions pkg/testdata/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,3 +1106,29 @@ rules:
match:
- regex: "io.vertx.core.VertxException: Thread blocked"
`

var TestFailMultipleOrigin = `
rules:
- cre:
id: TestFailMultipleOrigin
metadata:
id: "J7uRQTGpGMyL1iFpssnB3S"
hash: "rdJLgqYgkEp8jg8Qks1qqq"
generation: 1
rule:
set:
window: 50s
match:
- promql:
event:
source: cre.metrics
origin: true
expr: 'sum(rate(http_requests_total[5m])) by (service)'
interval: 10s
- set:
event:
source: kafka
origin: true
match:
- regex: "io.vertx.core.VertxException: Thread blocked"
`