Skip to content

Commit

Permalink
feat: move check details query to duty
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 29, 2023
1 parent c250a7f commit 3e51651
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 417 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/flanksource/canary-checker/checks"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/jobs/canary"
"github.com/flanksource/canary-checker/pkg/prometheus"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/query"
gomplate "github.com/flanksource/gomplate/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -94,12 +94,12 @@ func ServerFlags(flags *pflag.FlagSet) {
flags.StringSliceVar(&runner.IncludeCanaries, "include-check", []string{}, "Run matching canaries - useful for debugging")
flags.StringSliceVar(&runner.IncludeTypes, "include-type", []string{}, "Check type to disable")
flags.StringSliceVar(&runner.IncludeNamespaces, "include-namespace", []string{}, "Check type to disable")
flags.IntVar(&cache.DefaultCacheCount, "maxStatusCheckCount", 5, "Maximum number of past checks in the in memory cache")
flags.IntVar(&query.DefaultCacheCount, "maxStatusCheckCount", 5, "Maximum number of past checks in the in memory cache")
flags.StringVar(&runner.RunnerName, "name", "local", "Server name shown in aggregate dashboard")
flags.StringVar(&prometheus.PrometheusURL, "prometheus", "", "URL of the prometheus server that is scraping this instance")
flags.StringVar(&db.ConnectionString, "db", "DB_URL", "Connection string for the postgres database. Use embedded:///path/to/dir to use the embedded database")
flags.IntVar(&db.DefaultExpiryDays, "cache-timeout", 90, "Cache timeout in days")
flags.StringVarP(&cache.DefaultWindow, "default-window", "", "1h", "Default search window")
flags.StringVarP(&query.DefaultCheckQueryWindow, "default-window", "", "1h", "Default search window")
flags.IntVar(&db.CheckStatusRetention, "check-status-retention-period", db.CheckStatusRetention, "Check status retention period in days")
flags.IntVar(&topology.CheckRetentionDays, "check-retention-period", topology.DefaultCheckRetentionDays, "Check retention period in days")
flags.IntVar(&topology.CanaryRetentionDays, "canary-retention-period", topology.DefaultCanaryRetentionDays, "Canary retention period in days")
Expand Down
3 changes: 1 addition & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/flanksource/canary-checker/pkg/echo"
"github.com/flanksource/canary-checker/pkg/jobs"
canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary"
"github.com/flanksource/duty"
echov4 "github.com/labstack/echo/v4"
"go.opentelemetry.io/otel"

Expand Down Expand Up @@ -62,7 +61,7 @@ func setup() dutyContext.Context {

cache.PostgresCache = cache.NewPostgresCache(apicontext.DefaultContext)

if err := duty.UpdatePropertiesFromFile(apicontext.DefaultContext, propertiesFile); err != nil {
if err := dutyContext.LoadPropertiesFromFile(apicontext.DefaultContext, propertiesFile); err != nil {
logger.Fatalf("Error setting properties in database: %v", err)
}
return apicontext.DefaultContext
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/flanksource/artifacts v1.0.3
github.com/flanksource/commons v1.19.3
github.com/flanksource/duty v1.0.258
github.com/flanksource/duty v1.0.261
github.com/flanksource/gomplate/v3 v3.20.26
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7
github.com/flanksource/kommons v0.31.4
Expand Down Expand Up @@ -284,6 +284,4 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

// replace github.com/flanksource/commons => ../commons

// replace github.com/flanksource/duty => ../duty
// replace "github.com/flanksource/duty" => ../duty
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,8 @@ github.com/flanksource/artifacts v1.0.3 h1:Ci26mDhVFyxPefX96tgPGTe5fR0wfntXotSxa
github.com/flanksource/artifacts v1.0.3/go.mod h1:KfDDG7B4wsiGqJYOamcRU19u5hBvpYjlru3GfcORvko=
github.com/flanksource/commons v1.19.3 h1:J4s5WWicUMNavEDCC5AIJQWBDaaloJ/tgztWUXTtRXc=
github.com/flanksource/commons v1.19.3/go.mod h1:ZUgFy0Wwrm2LxV/AltBx36FShzXcn7wPmXJldK8Aa0E=
github.com/flanksource/duty v1.0.258 h1:+51Z8arzzks9hWHhUwV0A606UTvNMk0up9rj0asai0Y=
github.com/flanksource/duty v1.0.258/go.mod h1:tXMyMlcSGOXvXoP5KZApwzSDFs07NDQGhnJi9ArjWf4=
github.com/flanksource/duty v1.0.261 h1:G8qfPPk8ABIIQ/Bwv4OyAU7l6NrIuBN4eYJ9MYrzSJ0=
github.com/flanksource/duty v1.0.261/go.mod h1:4r5cU+iu89HXK2NtQpPw3neE6CRxrqZLshrKLN2G3CU=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.20.26 h1:85lUzlKgZjb1uIkzoa4zN03OcdOnFPG+oWxshZTYqz4=
github.com/flanksource/gomplate/v3 v3.20.26/go.mod h1:GKmptFMdr2LbOuqwQZrmo9a/UygyZ0pbXffks8MuYhE=
Expand Down
3 changes: 2 additions & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/antonmedv/expr v1.15.5 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/asecurityteam/rolling v2.0.4+incompatible // indirect
github.com/aws/aws-sdk-go v1.48.15 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/exaring/otelpgx v0.5.2 // indirect
github.com/flanksource/duty v1.0.258 // indirect
github.com/flanksource/duty v1.0.261 // indirect
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 // indirect
github.com/flanksource/kommons v0.31.4 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmms
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asecurityteam/rolling v2.0.4+incompatible h1:WOSeokINZT0IDzYGc5BVcjLlR9vPol08RvI2GAsmB0s=
github.com/asecurityteam/rolling v2.0.4+incompatible/go.mod h1:2D4ba5ZfYCWrIMleUgTvc8pmLExEuvu3PDwl+vnG58Q=
github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.48.15 h1:Gad2C4pLzuZDd5CA0Rvkfko6qUDDTOYru145gkO7w/Y=
github.com/aws/aws-sdk-go v1.48.15/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
Expand Down Expand Up @@ -717,8 +719,8 @@ github.com/exaring/otelpgx v0.5.2/go.mod h1:4dBiAqwzDNmpj3TwX5Syti1/Nw2bIoDQItdL
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flanksource/commons v1.19.3 h1:J4s5WWicUMNavEDCC5AIJQWBDaaloJ/tgztWUXTtRXc=
github.com/flanksource/commons v1.19.3/go.mod h1:ZUgFy0Wwrm2LxV/AltBx36FShzXcn7wPmXJldK8Aa0E=
github.com/flanksource/duty v1.0.258 h1:+51Z8arzzks9hWHhUwV0A606UTvNMk0up9rj0asai0Y=
github.com/flanksource/duty v1.0.258/go.mod h1:tXMyMlcSGOXvXoP5KZApwzSDFs07NDQGhnJi9ArjWf4=
github.com/flanksource/duty v1.0.261 h1:G8qfPPk8ABIIQ/Bwv4OyAU7l6NrIuBN4eYJ9MYrzSJ0=
github.com/flanksource/duty v1.0.261/go.mod h1:4r5cU+iu89HXK2NtQpPw3neE6CRxrqZLshrKLN2G3CU=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.20.26 h1:85lUzlKgZjb1uIkzoa4zN03OcdOnFPG+oWxshZTYqz4=
github.com/flanksource/gomplate/v3 v3.20.26/go.mod h1:GKmptFMdr2LbOuqwQZrmo9a/UygyZ0pbXffks8MuYhE=
Expand Down
60 changes: 2 additions & 58 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,6 @@ func (s CheckStatus) GetTime() (time.Time, error) {
return time.Parse("2006-01-02 15:04:05", s.Time)
}

type Latency struct {
Percentile99 float64 `json:"p99,omitempty" db:"p99"`
Percentile97 float64 `json:"p97,omitempty" db:"p97"`
Percentile95 float64 `json:"p95,omitempty" db:"p95"`
Rolling1H float64 `json:"rolling1h"`
}

func (l Latency) String() string {
s := ""
if l.Percentile99 != 0 {
s += fmt.Sprintf("p99=%s", utils.Age(time.Duration(l.Percentile99)*time.Millisecond))
}
if l.Percentile95 != 0 {
s += fmt.Sprintf("p95=%s", utils.Age(time.Duration(l.Percentile95)*time.Millisecond))
}
if l.Percentile97 != 0 {
s += fmt.Sprintf("p97=%s", utils.Age(time.Duration(l.Percentile97)*time.Millisecond))
}
if l.Rolling1H != 0 {
s += fmt.Sprintf("rolling1h=%s", utils.Age(time.Duration(l.Rolling1H)*time.Millisecond))
}
return s
}

type Uptime struct {
Passed int `json:"passed"`
Failed int `json:"failed"`
P100 float64 `json:"p100,omitempty"`
LastPass *time.Time `json:"last_pass,omitempty"`
LastFail *time.Time `json:"last_fail,omitempty"`
}

func (u Uptime) String() string {
if u.Passed == 0 && u.Failed == 0 {
return ""
}
if u.Passed == 0 {
return fmt.Sprintf("0/%d 0%%", u.Failed)
}
percentage := 100.0 * (1 - (float64(u.Failed) / float64(u.Passed+u.Failed)))
return fmt.Sprintf("%d/%d (%0.1f%%)", u.Passed, u.Passed+u.Failed, percentage)
}

type Timeseries struct {
Key string `json:"key,omitempty"`
Time string `json:"time,omitempty"`
Status bool `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Duration int `json:"duration"`
// Count is the number of times the check has been run in the specified time window
Count int `json:"count,omitempty"`
Passed int `json:"passed,omitempty"`
Failed int `json:"failed,omitempty"`
}

type Canary struct {
ID uuid.UUID `gorm:"default:generate_ulid()"`
AgentID uuid.UUID
Expand Down Expand Up @@ -212,8 +156,8 @@ type Check struct {
Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
Uptime Uptime `json:"uptime" gorm:"-"`
Latency Latency `json:"latency" gorm:"-"`
Uptime types.Uptime `json:"uptime" gorm:"-"`
Latency types.Latency `json:"latency" gorm:"-"`
Statuses []CheckStatus `json:"checkStatuses" gorm:"-"`
Owner string `json:"owner,omitempty"`
Severity string `json:"severity,omitempty"`
Expand Down
36 changes: 11 additions & 25 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/labstack/echo/v4"

"github.com/flanksource/canary-checker/pkg"
Expand Down Expand Up @@ -44,11 +45,11 @@ type Response struct {
}

type DetailResponse struct {
Duration int `json:"duration,omitempty"`
RunnerName string `json:"runnerName"`
Status []pkg.Timeseries `json:"status"`
Latency pkg.Latency `json:"latency"`
Uptime pkg.Uptime `json:"uptime"`
Duration int `json:"duration,omitempty"`
RunnerName string `json:"runnerName"`
Status []query.Timeseries `json:"status"`
Latency types.Latency `json:"latency"`
Uptime types.Uptime `json:"uptime"`
}

func About(c echo.Context) error {
Expand All @@ -60,30 +61,15 @@ func About(c echo.Context) error {
}

func CheckDetails(c echo.Context) error {
q, err := cache.ParseQuery(c)
if err != nil {
ctx := c.Request().Context().(context.Context)

var q query.CheckQueryParams
if err := q.Init(c.QueryParams()); err != nil {
return errorResponse(c, err, http.StatusBadRequest)
}

start := time.Now()

end := q.GetEndTime()
since := q.GetStartTime()
timeRange := end.Sub(*since)

if timeRange <= time.Hour*2 {
q.WindowDuration = time.Minute
} else if timeRange >= time.Hour*24 {
q.WindowDuration = time.Minute * 15
} else if timeRange >= time.Hour*24*7 {
q.WindowDuration = time.Minute * 60
} else {
q.WindowDuration = time.Hour * 4
}

ctx := c.Request().Context().(context.Context)

results, uptime, latency, err := q.ExecuteDetails(ctx, ctx.Pool())
results, uptime, latency, err := q.ExecuteDetails(ctx)
if err != nil {
return errorResponse(c, err, http.StatusInternalServerError)
}
Expand Down
135 changes: 0 additions & 135 deletions pkg/cache/cache.go

This file was deleted.

Loading

0 comments on commit 3e51651

Please sign in to comment.