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

vup #14

Merged
merged 4 commits into from
Oct 24, 2023
Merged

vup #14

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
9 changes: 6 additions & 3 deletions .github/workflows/gotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ jobs:
test:
strategy:
matrix:
go-version: [1.16.7, 1.17.4]
go-version: [mod, dev-latest]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
env:
GO111MODULE: on
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- name: Setup go
uses: actions/setup-go@v2
- uses: kevincobain2000/action-gobrew@v2
with:
go-version: ${{ matrix.go }}
- name: Test
Expand Down
134 changes: 41 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,80 @@
# golang-alertnotification

This library supports sending alert as email and as message card to Ms Teams' channel. It is built with `go version go1.12.9`
This library supports sending throttled alerts as email and as message card to Ms Teams channel.

## Installing

### *go get
## Usage

```bash
go get -u github.com/rakutentech/go-alertnotification
go install github.com/rakutentech/go-alertnotification@latest
```

## Configurations

* This package use golang environmrnt variable as setting. It uses `os.Getenv` to get the configuration values. You can use any enivronment setting package. However, in the unittest and example example use `godotenv` from `https://github.com/joho/godotenv`.
* This package use golang env variables as settings.

### General Configs

|No |Environment Variable |default |Explanation |
|---|---|---|---|
|1 |APP_ENV | "" | application envinronment to be appeared in email/teams message |
|2 |APP_NAME | "" | application name to be appeared in email/teams message |
|3 | | | |

| No | Environment Variable | default | Explanation |
| :-- | :------------------- | :------ | :------------------------------------------------------------- |
| 1 | APP_ENV | "" | application envinronment to be appeared in email/teams message |
| 2 | APP_NAME | "" | application name to be appeared in email/teams message |
| 3 | | | |

### Email Configs

|No |Environment Variable |default |Explanation |
|---|---|---|---|
|1 |EMAIL_ALERT_ENABLED |false |change to "true" to enable |
|2 |EMAIL_SENDER |"" | *require sender email address |
|3 |EMAIL_RECEIVERS | "" | *require receiver email addresses. Multiple address separated by comma. eg. [email protected], [email protected] |
|4 |SMTP_HOST |"" | SMTP server hostname |
|5 |SMTP_PORT |"" | SMTP server port |
|6 |EMAIL_USERNAME |"" |SMTP username |
|7 |EMAIL_PASSWORD |"" |SMTP user's passord |
| No | Environment Variable | default | Explanation |
| :-- | :------------------- | :------ | :-------------------------------------------------------------------------- |
| 1 | EMAIL_ALERT_ENABLED | false | change to "true" to enable |
| 2 | EMAIL_SENDER | "" | *require sender email address |
| 3 | EMAIL_RECEIVERS | "" | *require receiver email addresses. Eg. [email protected], [email protected] |
| 4 | SMTP_HOST | "" | SMTP server hostname |
| 5 | SMTP_PORT | "" | SMTP server port |
| 6 | EMAIL_USERNAME | "" | SMTP username |
| 7 | EMAIL_PASSWORD | "" | SMTP user's passord |

### Ms Teams Configs

|No |Environment Variable |default |Explanation |
|---|---|---|---|
|1 |MS_TEAMS_ALERT_ENABLED |false | change to "true" to enable |
|2 |MS_TEAMS_CARD_SUBJECT |"" | Ms teams card subject |
|3 |ALERT_CARD_SUBJECT |"" |Alert MessageCard subject |
|4 |ALERT_THEME_COLOR |"" |Themes color |
|5 |MS_TEAMS_WEBHOOK |"" |*require Ms Teams webhook. <https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/connectors/connectors-using> |
|6 |MS_TEAMS_PROXY_URL |"" |Work behind corporate proxy |
| No | Environment Variable | default | Explanation |
| :-- | :--------------------- | :------ | :-------------------------- |
| 1 | MS_TEAMS_ALERT_ENABLED | false | change to "true" to enable |
| 2 | MS_TEAMS_CARD_SUBJECT | "" | Ms teams card subject |
| 3 | ALERT_CARD_SUBJECT | "" | Alert MessageCard subject |
| 4 | ALERT_THEME_COLOR | "" | Themes color |
| 5 | MS_TEAMS_WEBHOOK | "" | *require Ms Teams webhook. |
| 6 | MS_TEAMS_PROXY_URL | "" | Work behind corporate proxy |

### Throttling Configs

|No |Environment Variable |default |Explanation |
|---|---|---|---|
|1 |THROTTLE_DURATION | 7 | throttling duration in minute |
|2 |THROTTLE_DISKCACHE_DIR | /tmp/cache/{APP_NAME}_throttler_disk_cache | disk location for throttling |
|3 |THROTTLE_ENABLED | "true" | Enabled by default to avoid sending too many notification. Set it to "false" to disable. Enable this it will send notification only 1 for the same error within `THROTTLE_DURATION`. Otherwise, it will send each occurence of the error. Recommended to be enable. |
| No | Environment Variable | default | Explanation |
| :-- | :--------------------- | :----------------------------------------- | :---------------------------- |
| 1 | THROTTLE_DURATION | 7 | throttling duration in minute |
| 2 | THROTTLE_DISKCACHE_DIR | /tmp/cache/{APP_NAME}_throttler_disk_cache | disk location for throttling |
| 3 | THROTTLE_ENABLED | "true" | Disable all together |

* Reference for using message card :
<https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/cards/cards-reference>
<https://www.lee-ford.co.uk/send-message-cards-with-microsoft-teams/>

## Usage

### Without customized fields (will load subject or body of mail and teams from configuration)
```golang
### Simple

```go
//import
import n "github.com/rakutentech/go-alertnotification"
err := errors.New("Alert me")
ignoringErrs := []error{errors.New("Ignore 001"), errors.New("Ignore 002")};

//Create New Alert
alert := n.NewAlert(err, ignoringErr)

alert := n.NewAlert(err, ignoringErrs)
//Send notification
alert.Notify()

// To remove all current throttling
alert.RemoveCurrentThrotting()

```

### With customized fields
```golang
//import

```go
import n "github.com/rakutentech/go-alertnotification"

//Create expandos, can keep the field value as configured by removing that field from expandos
Expand All @@ -98,53 +95,4 @@ This library supports sending alert as email and as message card to Ms Teams' ch
// To remove all current throttling
alert.RemoveCurrentThrotting()

```

## Example

### Add configuration

* Create a `.env` file and add the setting value

```markdown
SMTP_HOST=localhost
SMTP_PORT=25
[email protected]
[email protected]
EMAIL_ALERT_ENABLED=true
MS_TEAMS_ALERT_ENABLED=

MS_TEAMS_CARD_SUBJECT=test subject
ALERT_THEME_COLOR=ff5864
ALERT_CARD_SUBJECT=Errror card
MS_TEAMS_CARD_SUBJECT=teams card
APP_ENV=local
APP_NAME=golang
MS_TEAMS_WEBHOOK=Teams webhook
```

### Send alert to both email and teams

```golang
package main;

import (
"errors"
"os"
n "github.com/rakutentech/go-alertnotification"
"github.com/joho/godotenv"
)
func setEnv() {
godotenv.Load()
}


func main() {
// set env variable
setEnv();
err := errors.New("To be alerted error");
ignoringErr := []error{errors.New("Ignore 001"), errors.New("Ignore 002")};
alert := n.NewAlert(err, ignoringErr);
alert.Notify();
}
```
```
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/rakutentech/go-alertnotification

go 1.15
go 1.21

require (
github.com/GitbookIO/diskache v0.0.0-20161028144708-bfb81bf58cb1
github.com/GitbookIO/syncgroup v0.0.0-20200915204659-4f0b2961ab10 // indirect
github.com/joho/godotenv v1.3.0
)

require github.com/GitbookIO/syncgroup v0.0.0-20200915204659-4f0b2961ab10 // indirect
Loading