Skip to content

Commit

Permalink
Add emitting of e2e test instance metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
d8660091 committed May 1, 2024
1 parent 305e2bb commit 68c2791
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions internal/test/e2e/cloudwatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package e2e

import (
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"

"github.com/aws/eks-anywhere/pkg/logger"
)

var svc *cloudwatch.CloudWatch

func init() {
if s, err := session.NewSession(); err == nil {
svc = cloudwatch.New(s)
} else {
fmt.Println("Cannot create CloudWatch service", err)
}
}

func putInstanceTestResultMetrics(r instanceTestsResults) {
if svc == nil {
logger.Info("Cannot publish metrics as cloudwatch service was not initialized")
return
}

logger.Info("Publishing instance test result metrics")
erroredCount, failedCount, succeededCount := 0, 0, 0
if r.err != nil {
erroredCount = 1
} else if !r.testCommandResult.Successful() {
failedCount = 1
} else {
succeededCount = 1
}

data := &cloudwatch.MetricDatum{
Unit: aws.String("Count"),
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("Provider"),
Value: aws.String(getProviderName(r.conf.Regex)),
},
{
Name: aws.String("BranchName"),
Value: aws.String(r.conf.BranchName),
},
},
Timestamp: aws.Time(time.Now()),
}
putMetric(data, "ErroredInstanceTests", erroredCount)
putMetric(data, "FailedInstanceTests", failedCount)
putMetric(data, "SucceededInstanceTests", succeededCount)

// TODO: publish time metrics
logger.Info("Test instance metrics published")
}

func getProviderName(testRe string) string {
providerRe := regexp.MustCompile(`Test([A-Z].*?)[A-Z]`)
provider := []byte("Unknown")
t := providerRe.FindSubmatch([]byte(testRe))
if len(t) > 1 {
provider = t[1]
}
return string(provider)
}

func putMetric(data *cloudwatch.MetricDatum, metricName string, value int) {
data.MetricName = aws.String(metricName)
data.Value = aws.Float64(float64(value))

if _, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
Namespace: aws.String("EksaE2ETests"),
MetricData: []*cloudwatch.MetricDatum{data},
}); err != nil {
logger.Error(err, "Cannot put metrics to cloudwatch")
} else {
logger.Info("Instance test result metrics published")
}
}
1 change: 1 addition & 0 deletions internal/test/e2e/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func RunTestsInParallel(conf ParallelRunConf) error {
"completedInstances", completedInstances,
"totalInstances", totalInstances,
)
putInstanceTestResultMetrics(r)
}

if failedInstances > 0 {
Expand Down

0 comments on commit 68c2791

Please sign in to comment.