Skip to content

Commit

Permalink
chore: truncate check status and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Dec 9, 2024
1 parent c127d39 commit 576fdb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
22 changes: 20 additions & 2 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/console"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/properties"
cUtils "github.com/flanksource/commons/utils"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -193,13 +194,30 @@ func FromExternalCheck(canary Canary, check external.Check) Check {
}
}

func ellipsis(str string, length int) string {
if length <= 3 || len(str) <= length {
return str
}
str = strings.TrimSpace(str)

return strings.TrimSpace(str[0:length-3]) + "..."
}

func TruncateMessage(s string) string {
return ellipsis(s, properties.Int(4*1024, "canary.status.max.message"))
}

func TruncateError(s string) string {
return ellipsis(s, properties.Int(128*1024, "canary.status.max.error"))
}

func CheckStatusFromResult(result CheckResult) CheckStatus {
cs := CheckStatus{
Status: result.Pass,
Invalid: result.Invalid,
Time: time.Now().UTC().Format(time.RFC3339),
Message: result.Message,
Error: result.Error,
Message: TruncateMessage(result.Message),
Error: TruncateError(result.Error),
Detail: result.Detail,
Check: &result.Check,
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/controllers/canary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/utils"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -244,8 +245,12 @@ func (r *CanaryReconciler) Report() {
canary.Status.LastTransitionedTime = payload.LastTransitionedTime
}

canary.Status.Message = &payload.Message
canary.Status.ErrorMessage = &payload.ErrorMessage
if payload.Message != "" {
canary.Status.Message = lo.ToPtr(pkg.TruncateMessage(payload.Message))
}
if payload.ErrorMessage != "" {
canary.Status.ErrorMessage = lo.ToPtr(pkg.TruncateError(payload.ErrorMessage))
}

canary.Status.LastCheck = &metav1.Time{Time: time.Now()}
canary.Status.ChecksStatus = payload.CheckStatus
Expand Down

0 comments on commit 576fdb2

Please sign in to comment.