Skip to content

Commit

Permalink
feat: limit check status duration to maxInt32 milliseconds. (#2181)
Browse files Browse the repository at this point in the history
* feat: use int64 for check status duration

* fix: limit to maxint32
  • Loading branch information
adityathebe authored Sep 30, 2024
1 parent 5db5fab commit 37b0218
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
3 changes: 1 addition & 2 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func init() {
Push.Flags().StringVar(&status.Error, "error", "", "Error of check")
Push.Flags().StringVar(&status.Message, "message", "", "Message of check")
Push.Flags().StringVar(&details, "detail", "", "Detail of check")
Push.Flags().IntVar(&status.Duration, "duration", 0, "Duration of check in milliseconds")
// Push.Flags().StringVar(&status.Time, "time", "", "Time of check")
Push.Flags().Int32Var(&status.DurationMs, "duration", 0, "Duration of check in milliseconds")
Push.Flags().BoolVar(&status.Status, "passed", true, "Passed status of check")
Root.AddCommand(Push)
}
43 changes: 26 additions & 17 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg
import (
"fmt"
"io"
"math"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -50,14 +51,14 @@ func (t *JSONTime) UnmarshalJSON(b []byte) error {
}

type CheckStatus struct {
Status bool `json:"status"`
Invalid bool `json:"invalid,omitempty"`
Time string `json:"time"`
Duration int `json:"duration"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Detail interface{} `json:"-"`
Check *external.Check `json:"check,omitempty"`
Status bool `json:"status"`
Invalid bool `json:"invalid,omitempty"`
Time string `json:"time"`
DurationMs int32 `json:"duration"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Detail interface{} `json:"-"`
Check *external.Check `json:"check,omitempty"`
}

func (s CheckStatus) GetTime() (time.Time, error) {
Expand Down Expand Up @@ -193,16 +194,24 @@ func FromExternalCheck(canary Canary, check external.Check) Check {
}

func CheckStatusFromResult(result CheckResult) CheckStatus {
return CheckStatus{
Status: result.Pass,
Invalid: result.Invalid,
Duration: int(result.Duration),
Time: time.Now().UTC().Format(time.RFC3339),
Message: result.Message,
Error: result.Error,
Detail: result.Detail,
Check: &result.Check,
cs := CheckStatus{
Status: result.Pass,
Invalid: result.Invalid,
Time: time.Now().UTC().Format(time.RFC3339),
Message: result.Message,
Error: result.Error,
Detail: result.Detail,
Check: &result.Check,
}

// For check duration over ~25 days, we limit it to MaxInt32 milliseconds.
if result.Duration > math.MaxInt32 && false {
cs.DurationMs = math.MaxInt32
} else {
cs.DurationMs = int32(result.Duration)
}

return cs
}

func FromV1(canary v1.Canary, check external.Check, statuses ...CheckStatus) Check {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *postgresCache) AddCheckStatus(conn *gorm.DB, check pkg.Check, status pk
`,
checks[0].ID,
string(jsonDetails),
status.Duration,
status.DurationMs,
status.Error,
status.Invalid,
status.Message,
Expand Down

0 comments on commit 37b0218

Please sign in to comment.