Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Disable adding offset to From when queuing replays #255

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ words:
- slos
- svcs
- tpng
- unmarshalling
- vuln
- vulns
- wrapf
1 change: 1 addition & 0 deletions internal/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (a ApplyCmd) runReplay(cmd *cobra.Command, objects []manifest.Object) error
return nil
}
replayCmd := ReplayCmd{client: a.client}
replayCmd.arePlaylistEnabled(cmd.Context())
replays := make([]ReplayConfig, 0, len(slos))
for _, slo := range slos {
replays = append(replays, ReplayConfig{
Expand Down
60 changes: 38 additions & 22 deletions internal/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import (
)

type ReplayCmd struct {
client *sdk.Client
from TimeValue
configPaths []string
sloName string
project string
deleteAll bool
client *sdk.Client
from TimeValue
configPaths []string
sloName string
project string
deleteAll bool
playlistsAvailable bool
}

//go:embed replay_example.sh
Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *ReplayCmd) Run(cmd *cobra.Command) error {
if r.client.Config.Project == "*" {
return errProjectWildcardIsNotAllowed
}
r.arePlaylistEnabled(cmd.Context())
replays, err := r.prepareConfigs()
if err != nil {
return err
Expand All @@ -90,9 +92,7 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
return 0, err
}

arePlaylistEnabled := r.arePlaylistEnabled(cmd.Context())

if arePlaylistEnabled {
if r.playlistsAvailable {
cmd.Println(colorstring.Color("[yellow]- Your organization has access to Replay queues!"))
cmd.Println(colorstring.Color("[yellow]- To learn more about Replay queues, follow this link: " +
"https://docs.nobl9.com/replay-canary/ [reset]"))
Expand All @@ -105,7 +105,7 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
i+1, len(replays), replay.SLO, replay.Project,
replay.From.Format(timeLayout), time.Now().In(replay.From.Location()).Format(timeLayout))))

if arePlaylistEnabled {
if r.playlistsAvailable {
cmd.Println("Replay is added to the queue...")
err = r.runReplay(cmd.Context(), replay)

Expand Down Expand Up @@ -135,7 +135,12 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
return len(failedIndexes), nil
}

func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) bool {
type PlaylistConfiguration struct {
EnabledPlaylists bool `json:"enabledPlaylists"`
}

func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) {
r.playlistsAvailable = true
data, _, err := r.doRequest(
ctx,
http.MethodGet,
Expand All @@ -144,17 +149,13 @@ func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) bool {
nil,
nil)
if err != nil {
return true
fmt.Printf("error checking playlist availability: %v\n", err)
}
var pc PlaylistConfiguration
if err = json.Unmarshal(data, &pc); err != nil {
return true
fmt.Printf("error unmarshalling playlist configuration: %v\n", err)
}
return pc.EnabledPlaylists
}

type PlaylistConfiguration struct {
EnabledPlaylists bool `json:"enabledPlaylists"`
r.playlistsAvailable = pc.EnabledPlaylists
}

type ReplayConfig struct {
Expand Down Expand Up @@ -346,16 +347,28 @@ outer:
}

// Check Replay availability.
if err := r.checkReplayAvailability(ctx, replays); err != nil {
return err
}

return nil
}

func (r *ReplayCmd) checkReplayAvailability(ctx context.Context, replays []ReplayConfig) error {
notAvailable := make([]string, 0)
mu := sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(10)

for i := range replays {
eg.Go(func() error {
c := replays[i]
timeNow := time.Now()
tt := c.ToReplay(timeNow)
offset := i * int(averageReplayDuration.Minutes())
offset := 0
if !r.playlistsAvailable {
offset = i * int(averageReplayDuration.Minutes())
}
expectedDuration := offset + tt.Duration.Value
av, err := r.getReplayAvailability(ctx, c, tt.Duration.Unit, expectedDuration)
if err != nil {
Expand All @@ -364,6 +377,7 @@ outer:
}
if !av.Available {
mu.Lock()
defer mu.Unlock()
notAvailable = append(notAvailable,
fmt.Sprintf("['%s' SLO in '%s' Project] %s",
c.SLO, c.Project, r.replayUnavailabilityReasonExplanation(
Expand All @@ -372,18 +386,20 @@ outer:
time.Duration(expectedDuration)*time.Minute,
time.Duration(offset)*time.Minute,
timeNow)))
mu.Unlock()
}
return nil
})
}
if err = eg.Wait(); err != nil {

if err := eg.Wait(); err != nil {
return err
}

if len(notAvailable) > 0 {
return errors.Errorf("The following SLOs are not available for Replay: \n - %s",
strings.Join(notAvailable, "\n - "))
}

return nil
}

Expand Down Expand Up @@ -543,7 +559,7 @@ func (r *ReplayCmd) replayUnavailabilityReasonExplanation(
" + %dm (start offset to ensure Replay covers the desired time window) %s."+
" Edit the Data Source and run Replay once again.",
replay.metricSource.Name, replay.metricSource.Project, expectedDuration.String(),
timeNow.Format(timeLayout), r.from.Format(timeLayout), startOffsetMinutes, offsetNotice)
timeNow.Format(timeLayout), replay.From.Format(timeLayout), startOffsetMinutes, offsetNotice)
case sdkModels.ReplayConcurrentReplayRunsLimitExhausted:
return "You've exceeded the limit of concurrent Replay runs. Wait until the current Replay(s) are done."
case sdkModels.ReplayUnknownAgentVersion:
Expand Down
Loading