Skip to content

Commit

Permalink
fix: export metrics for checks in kubernetes resources
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored Feb 25, 2025
1 parent a8868fa commit de62132
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions checks/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ func getLabelString(labels map[string]string) string {
return s
}

func ExportCheckMetrics(ctx *context.Context, results pkg.Results) {
func ExportCheckMetrics(ctx *context.Context, results pkg.Results, exportResultMetrics bool) {
if len(results) == 0 {
return
}

for _, r := range results {
checkCtx := ctx.WithCheckResult(r)
for _, metric := range r.Metrics {
if err := exportMetric(checkCtx, metric); err != nil {
r.ErrorMessage(err)
if exportResultMetrics {
if err := exportMetric(checkCtx, metric); err != nil {
r.ErrorMessage(err)
}
}
}
for _, spec := range r.Check.GetMetricsSpec() {
Expand Down
5 changes: 3 additions & 2 deletions checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func getDisabledChecks(ctx *context.Context) (map[string]struct{}, error) {
return result, nil
}

// Exec runs the actions specified and returns the results, without exporting any metrics
// Exec runs the actions specified and returns the results, without saving artifacts
func Exec(ctx *context.Context) ([]*pkg.CheckResult, error) {
var results []*pkg.CheckResult
disabledChecks, err := getDisabledChecks(ctx)
Expand All @@ -84,6 +84,7 @@ func Exec(ctx *context.Context) ([]*pkg.CheckResult, error) {
result := c.Run(ctx)
transformedResults := TransformResults(ctx, result)
results = append(results, transformedResults...)
ExportCheckMetrics(ctx, transformedResults, false)
}
return results, nil
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func RunChecks(ctx *context.Context) ([]*pkg.CheckResult, error) {
result := c.Run(ctx)
transformedResults := TransformResults(ctx, result)
results = append(results, transformedResults...)
ExportCheckMetrics(ctx, transformedResults)
ExportCheckMetrics(ctx, transformedResults, true)
}

if err := saveArtifacts(ctx, results); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func webhookHandler(ctx dutyContext.Context, id, authToken string, data CheckDat
scrapeCtx := context.New(ctx, *canary)
transformedResults := checks.TransformResults(scrapeCtx, results)

checks.ExportCheckMetrics(scrapeCtx, transformedResults)
checks.ExportCheckMetrics(scrapeCtx, transformedResults, true)
for _, result := range transformedResults {
_, err := cache.PostgresCache.Add(ctx, pkg.FromV1(result.Canary, result.Check), pkg.CheckStatusFromResult(*result))
if err != nil {
Expand Down

0 comments on commit de62132

Please sign in to comment.