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

feature/grace seconds #16

Merged

Conversation

IhorMarchenkoRakuten
Copy link
Contributor

This is a feature to add support for grace duration in seconds. Once grace is set, alerts will be ignored until the seconds are over and then start working usually with throttling. The default for grace is 0 sec.

@kevincobain2000
Copy link
Member

Doesn't work

func main() {
  os.Setenv("APP_NAME", "123")
  os.Setenv("MS_TEAMS_ALERT_ENABLED", "test")
  os.Setenv("MS_TEAMS_WEBHOOK", "true")
  os.Setenv("THROTTLE_DURATION", "1")
  os.Setenv("THROTTLE_GRACE_SECONDS", "20")
  t := n.NewThrottler()
  t.CleanThrottlingCache()

  err := errors.New("ignore 000")
  alert := n.NewAlert(err, nil)
  alert.Notify()

  err = errors.New("ignore 000")
  alert = n.NewAlert(err, nil)
  alert.Notify()
  alert.Notify()

  err = errors.New("ignore 000")
  alert = n.NewAlert(err, nil)
  alert.Notify()
} 

Output

func (a *Alert) Notify() (err error) {
  fmt.Println("should alert:", a.shouldAlert())
╰─$ go run main.go
should alert: true <-- this should have been false, because the grace is 20 seconds
should alert: false

throttler.go Outdated

cachedDetectionTime, hasCachedDetectionTime := dc.Get(fmt.Sprintf("%v_detectionTime", ocError.Error()))
Copy link

@chetra-tep chetra-tep Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the correct place here. Grace's cache key should be set when Throttle's key is set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, let me try it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chetra-tep,
I have optimized the function and tried different approaches for the Cache key. The key is set in the same function just before the Throttle key. So if I put the function into the Throttle function, then it will be more difficult to check the Grace period on the first iteration.

I think there is not much difference if it's in another function since they are still in the same parent function IsThrottledOrGraced, and will be executed sequentially. Is there any scenario when the key shouldn't be handled at that place?

Please let me know what you think or if I misunderstood anything

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ll test it first.

Copy link
Member

@kevincobain2000 kevincobain2000 Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work

func main() {
	os.Setenv("APP_NAME", "123")
	os.Setenv("MS_TEAMS_ALERT_ENABLED", "test")
	os.Setenv("MS_TEAMS_WEBHOOK", "true")
	os.Setenv("THROTTLE_DURATION", "1")
	os.Setenv("THROTTLE_GRACE_SECONDS", "5")
	t := n.NewThrottler()
	t.CleanThrottlingCache()

	err := errors.New("ignore 000")
	alert := n.NewAlert(err, nil)
	alert.Notify()              // false, caught by grace
	time.Sleep(6 * time.Second) // wait for grace to go over

	alert.Notify() // true, first alert
	alert.Notify() // false, caught by throttle
	time.Sleep(61 * time.Second)
	alert.Notify()              // false, caught by grace
	time.Sleep(6 * time.Second) // wait for grace to go over
	alert.Notify()              // true
	alert.Notify()              // false, caught by throttle
}
should alert: false
should alert: true
should alert: false
should alert: false
should alert: false <-- this should be true
should alert: false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, I've adjusted the grace reset, so it should work now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will test. Thanks.

Copy link
Member

@kevincobain2000 kevincobain2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@kevincobain2000 kevincobain2000 merged commit dcbcf67 into rakutentech:master Feb 8, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants