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

adds throttle grace #15

Closed
wants to merge 4 commits into from
Closed
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
74 changes: 43 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# go-alertnotification

This library supports sending throttled alerts as email and as message card to Ms Teams channel.
<p align="center">
<a href="https://github.com/rakutentech/go-alertnotification">
<img alt="go-alertnotification" src="logo.png" width="360">
</a>
</p>

<h3 align="center">Send Alert Notifications for Go Errors.<br>Notify when new error arrives.</h3>

<p align="center">
Throttle notifications to avoid overwhelming your inbox.
<br>
Grace period to notify if the same error occurs.
<br>
Supports multiple Emails, MS Teams and proxy support.
</p>

## Usage

Expand All @@ -10,48 +22,48 @@ go install github.com/rakutentech/go-alertnotification@latest

## Configurations

* This package use golang env variables as settings.
You need to set env variables to configure the alert notification behaviour.

### General Configs


| Env Variable | default | Description |
| :----------- | :------ | :------------------------------------------------------------ |
| APP_ENV | | application environment to be appeared in email/teams message |
| APP_NAME | | application name to be appeared in email/teams message |
| Env Variable | default | Description |
| :----------- | :------ | :--------------------------- |
| APP_ENV | | Appears in email/teams title |
| APP_NAME | | Appears in email/teams body |


### Email Configs

| Env Variable | default | Description |
| :------------------ | :------ | :------------------------------------------------------------------------------ |
| **EMAIL_SENDER** | | **required** sender email address |
| **EMAIL_RECEIVERS** | | **required** receiver email addresses. Eg. `[email protected]`,`[email protected]` |
| EMAIL_ALERT_ENABLED | false | change to "true" to enable |
| SMTP_HOST | | SMTP server hostname |
| SMTP_PORT | | SMTP server port |
| EMAIL_USERNAME | | SMTP username |
| EMAIL_PASSWORD | | SMTP password |
| Env Variable | default | Description |
| :------------------ | :------ | :----------------------------------------------------------------------- |
| **EMAIL_SENDER** | | **Required** one mail address |
| **EMAIL_RECEIVERS** | | **Required** multiple addresses. Eg. `[email protected]`,`[email protected]` |
| EMAIL_ALERT_ENABLED | false | |
| SMTP_HOST | | |
| SMTP_PORT | | |
| EMAIL_USERNAME | | |
| EMAIL_PASSWORD | | |

### Ms Teams Configs

| Env Variable | default | Description |
| :--------------------- | :------ | :----------------------------- |
| **MS_TEAMS_WEBHOOK** | | **required** Ms Teams webhook. |
| MS_TEAMS_ALERT_ENABLED | false | change to "true" to enable |
| MS_TEAMS_CARD_SUBJECT | | MS teams card subject |
| ALERT_CARD_SUBJECT | | Alert MessageCard subject |
| ALERT_THEME_COLOR | | Themes color |
| MS_TEAMS_PROXY_URL | | Work behind corporate proxy |
| Env Variable | default | Description |
| :--------------------- | :------ | :----------------- |
| **MS_TEAMS_WEBHOOK** | | **Required** |
| MS_TEAMS_ALERT_ENABLED | false | |
| MS_TEAMS_CARD_SUBJECT | | |
| ALERT_CARD_SUBJECT | | |
| ALERT_THEME_COLOR | | |
| MS_TEAMS_PROXY_URL | | HTTP proxy, if any |

### Throttling Configs

| Env Variable | default | Explanation |
| :--------------------- | :------------------------------------------- | :----------------------------- |
| THROTTLE_DURATION | 7 | throttling duration in minutes |
| THROTTLE_GRACE_SECONDS | 0 | throttling grace in seconds |
| THROTTLE_DISKCACHE_DIR | `/tmp/cache/{APP_NAME}_throttler_disk_cache` | disk location for throttling |
| THROTTLE_ENABLED | true | Disable all together |
| Env Variable | default | Explanation |
| :--------------------- | :------------------------------ | :------------------------------- |
| THROTTLE_DURATION | 7 | Throttling duration in (minutes) |
| THROTTLE_GRACE_SECONDS | 0 | Throttling grace in (seconds) |
| THROTTLE_DISKCACHE_DIR | `/tmp/cache/{APP_NAME}_thro...` | Disk location for cache |
| THROTTLE_ENABLED | true | Disable all together |

## Usage

Expand Down
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (t *Throttler) IsThrottled(ocError error) bool {
if err != nil {
return false
}

gracedTime, gracedSeconds := getGracePeriod(dc, ocError)

if isWithinGracePeriod(string(gracedTime), gracedSeconds) {
return true
}

cachedTime, throttled := dc.Get(ocError.Error())

if throttled && !isOverThrottleDuration(string(cachedTime), t.ThrottleDuration) {
Expand All @@ -63,6 +70,35 @@ func (t *Throttler) IsThrottled(ocError error) bool {
return false
}

func getGracePeriod(dc *diskache.Diskache, ocError error) ([]byte, int) {
graceKeyPrefix := "GRACE-"
now := time.Now().Format(time.RFC3339)
gracedTime, gracedCached := dc.Get(graceKeyPrefix + ocError.Error())
gracedSeconds, err := strconv.Atoi(os.Getenv("THROTTLE_GRACE_SECONDS"))
if err != nil {
return []byte{}, 0 // no grace period
}

if !gracedCached {
gracedTime = []byte(now)
err = dc.Set(graceKeyPrefix+ocError.Error(), gracedTime)
if err != nil {
return []byte{}, 0 // no grace period
}
}
return gracedTime, gracedSeconds
}

func isWithinGracePeriod(cachedTime string, gracedSeconds int) bool {
gracedTime, err := time.Parse(time.RFC3339, string(cachedTime))
if err != nil {
return false
}
now := time.Now()
diff := int(now.Sub(gracedTime).Seconds())
return diff < gracedSeconds
}

func isOverThrottleDuration(cachedTime string, throttleDuration int) bool {
throttledTime, err := time.Parse(time.RFC3339, string(cachedTime))
if err != nil {
Expand Down
90 changes: 90 additions & 0 deletions throttler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,96 @@ func TestThrottler_IsThrottled(t *testing.T) {
})
}
}
func TestThrottler_IsThrottledGraced(t *testing.T) {
type fields struct {
CacheOpt string
ThrottleDuration int
}
type args struct {
ocError error
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "default",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
},
args: args{
ocError: errors.New("test_throttling"),
},
want: true,
},
}

os.Setenv("THROTTLE_GRACE_SECONDS", "10")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &Throttler{
CacheOpt: tt.fields.CacheOpt,
ThrottleDuration: tt.fields.ThrottleDuration,
}
if got := th.IsThrottled(tt.args.ocError); got != tt.want {
t.Errorf("Throttler.IsThrottled() = %v, want %v", got, tt.want)
}
err := th.CleanThrottlingCache()
if err != nil {
t.Errorf("Cannot clean after test. %+v", err)
}

})
}
}
func TestThrottler_IsThrottledOverGraced(t *testing.T) {
type fields struct {
CacheOpt string
ThrottleDuration int
}
type args struct {
ocError error
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "default",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
},
args: args{
ocError: errors.New("test_throttling"),
},
want: false,
},
}

os.Setenv("THROTTLE_GRACE_SECONDS", "0")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &Throttler{
CacheOpt: tt.fields.CacheOpt,
ThrottleDuration: tt.fields.ThrottleDuration,
}
if got := th.IsThrottled(tt.args.ocError); got != tt.want {
t.Errorf("Throttler.IsThrottled() = %v, want %v", got, tt.want)
}
err := th.CleanThrottlingCache()
if err != nil {
t.Errorf("Cannot clean after test. %+v", err)
}

})
}
}

func TestThrottler_ThrottleError(t *testing.T) {
type fields struct {
Expand Down
Loading