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

feat: add metric check methods for e2e engine #1839

Merged
merged 2 commits into from
Oct 29, 2024
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
2 changes: 2 additions & 0 deletions test/engine/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func ScenarioInitializer(ctx *godog.ScenarioContext) {

// Then
ctx.Then(`^there is \{(\d+)\} logs$`, verify.LogCount)
ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds and the value is greater than \{(\d+)\} and less than \{(\d+)\}$`, verify.MetricCountAndValueCompare)
ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds and the value is \{(\d+)\}$`, verify.MetricCountAndValueEqual)
ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds$`, verify.MetricCount)
ctx.Then(`^there is at least \{(\d+)\} logs$`, verify.LogCountAtLeast)
ctx.Then(`^there is at least \{(\d+)\} logs with filter key \{(.*)\} value \{(.*)\}$`, verify.LogCountAtLeastWithFilter)
Expand Down
66 changes: 65 additions & 1 deletion test/engine/verify/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package verify
import (
"context"
"fmt"
"strconv"
"time"

"github.com/avast/retry-go/v4"
Expand Down Expand Up @@ -70,7 +71,7 @@ func LogCount(ctx context.Context, expect int) (context.Context, error) {
return ctx, nil
}

func MetricCount(ctx context.Context, expect int, duration int64) (context.Context, error) {
func MetricCheck(ctx context.Context, expect int, duration int64, checker func([]*protocol.LogGroup) error) (context.Context, error) {
timeoutCtx, cancel := context.WithTimeout(context.TODO(), config.TestConfig.RetryTimeout)
defer cancel()
var groups []*protocol.LogGroup
Expand All @@ -94,6 +95,9 @@ func MetricCount(ctx context.Context, expect int, duration int64) (context.Conte
if expect == 0 {
return fmt.Errorf("metric count is 0")
}
if err = checker(groups); err != nil {
return err
}
return nil
},
retry.Context(timeoutCtx),
Expand All @@ -109,6 +113,66 @@ func MetricCount(ctx context.Context, expect int, duration int64) (context.Conte
return ctx, nil
}

func MetricCount(ctx context.Context, expect int, duration int64) (context.Context, error) {
return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error {
return nil
})
}

func MetricCountAndValueCompare(ctx context.Context, expect int, duration int64, minValue int64, maxValue int64) (context.Context, error) {
return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error {
lessCount := 0
greaterCount := 0
for _, group := range groups {
for _, log := range group.Logs {
for _, content := range log.Contents {
if content.Key == "value" {
value, err := strconv.ParseFloat(content.Value, 64)
if err != nil {
return fmt.Errorf("parse value failed: %v", err)
}
if value < float64(minValue) {
lessCount++
}
if value > float64(maxValue) {
greaterCount++
}
}
}
}
}
if lessCount > 0 || greaterCount > 0 {
return fmt.Errorf("metric value not match, lessCount %d, greaterCount %d", lessCount, greaterCount)
}
return nil
})
}

func MetricCountAndValueEqual(ctx context.Context, expect int, duration int64, expectValue int64) (context.Context, error) {
return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error {
notEqualCount := 0
for _, group := range groups {
for _, log := range group.Logs {
for _, content := range log.Contents {
if content.Key == "value" {
value, err := strconv.ParseFloat(content.Value, 64)
if err != nil {
return fmt.Errorf("parse value failed: %v", err)
}
if value != float64(expectValue) {
notEqualCount++
}
}
}
}
}
if notEqualCount > 0 {
return fmt.Errorf("metric value not match, not equal count %d", notEqualCount)
}
return nil
})
}

func LogCountAtLeast(ctx context.Context, expect int) (context.Context, error) {
var from int32
value := ctx.Value(config.StartTimeContextKey)
Expand Down
Loading