From c05138f162a5362fc976734ac98c9faa0c4dac4f Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Tue, 24 Oct 2023 14:24:54 +0900 Subject: [PATCH 1/4] vup --- .github/workflows/gotest.yml | 5 ++--- go.mod | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index e4fb555..4768ffe 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -4,15 +4,14 @@ jobs: test: strategy: matrix: - go-version: [1.16.7, 1.17.4] + go-version: [mode, dev-latest] os: [ubuntu-latest] runs-on: ${{ matrix.os }} env: GO111MODULE: on steps: - uses: actions/checkout@v2 - - name: Setup go - uses: actions/setup-go@v2 + - uses: kevincobain2000/action-gobrew@v2 with: go-version: ${{ matrix.go }} - name: Test diff --git a/go.mod b/go.mod index 4622a06..ef1598b 100644 --- a/go.mod +++ b/go.mod @@ -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 From a4d7cbd18da09716c528ca0fce78c3bfc662d44e Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Tue, 24 Oct 2023 14:25:25 +0900 Subject: [PATCH 2/4] mod --- .github/workflows/gotest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index 4768ffe..cbb8b35 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [mode, dev-latest] + go-version: [mod, dev-latest] os: [ubuntu-latest] runs-on: ${{ matrix.os }} env: From cb0c66e7685491b34b43ead21f352d3da7231d0c Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Tue, 24 Oct 2023 14:26:10 +0900 Subject: [PATCH 3/4] cancel prev --- .github/workflows/gotest.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index cbb8b35..006ee95 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -10,6 +10,10 @@ jobs: env: GO111MODULE: on steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} - uses: actions/checkout@v2 - uses: kevincobain2000/action-gobrew@v2 with: From 1f54c5c469699f77ff8c525a6aa07d6458cd0c88 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Tue, 24 Oct 2023 14:34:35 +0900 Subject: [PATCH 4/4] simplify README --- README.md | 134 +++++++++++++++++------------------------------------- 1 file changed, 41 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 7dca8e1..5f99098 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,56 @@ # 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. test1@example.com, test2@example.com | -|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. test1@example.com, test2@example.com | +| 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. | -|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 : @@ -59,25 +58,23 @@ This library supports sending alert as email and as message card to Ms Teams' ch ## 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 @@ -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_SENDER=test@example.com -EMAIL_RECEIVERS=receiver.test@exmaple.com -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(); -} -``` +``` \ No newline at end of file