-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"time" | ||
|
||
"github.com/devopsext/sre/common" | ||
"github.com/devopsext/sre/provider" | ||
) | ||
|
||
//go:embed slack.json | ||
var payload []byte | ||
|
||
var logs = common.NewLogs() | ||
var events = common.NewEvents() | ||
|
||
func test() { | ||
|
||
events.Now(":exclamation: First", nil) | ||
|
||
m := make(map[string]string) | ||
m["payload"] = string(payload) | ||
|
||
events.Now(":grey_exclamation:Second", m) | ||
events.Now(":grey_question: *Third*", nil) | ||
} | ||
|
||
func main() { | ||
|
||
defer logs.Stop() // finalize logs delivery | ||
defer events.Stop() // finalize events delivery | ||
|
||
// initialize Stdout logger | ||
stdout := provider.NewStdout(provider.StdoutOptions{ | ||
Format: "template", | ||
Level: "debug", | ||
Template: "{{.file}} {{.msg}}", | ||
TimestampFormat: time.RFC3339Nano, | ||
TextColors: true, | ||
}) | ||
// set caller offset for file:line proper usage | ||
stdout.SetCallerOffset(2) | ||
|
||
// add Stdout logger | ||
logs.Register(stdout) | ||
|
||
slack := provider.NewSlackEventer(provider.SlackOptions{ | ||
WebHook: "", | ||
Tags: "", | ||
Timeout: 2, | ||
}, logs, stdout) | ||
|
||
// add events | ||
events.Register(slack) | ||
|
||
test() | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"blocks": [ | ||
{ | ||
"type": "header", | ||
"text": { | ||
"type": "plain_text", | ||
"text": "Daily News", | ||
"emoji": true | ||
} | ||
}, | ||
{ | ||
"type": "divider" | ||
}, | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "`06:45 UTC (08:45 ETT)`:\t:grey_exclamation:\t| Germany_Industrial Orders MM" | ||
} | ||
}, | ||
{ | ||
"type": "section", | ||
"fields": [ | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Symbol*: EUR / EUR,CHF,XAG,XAU" | ||
}, | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Type*: Industry Sector" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package provider | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/devopsext/sre/common" | ||
"github.com/devopsext/utils" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type SlackOptions struct { | ||
WebHook string | ||
Tags string | ||
Timeout int | ||
} | ||
|
||
type SlackEventer struct { | ||
options SlackOptions | ||
logger common.Logger | ||
tags []string | ||
client *http.Client | ||
ctx context.Context | ||
} | ||
|
||
func (se *SlackEventer) Now(name string, attributes map[string]string) { | ||
se.At(name, attributes, time.Now()) | ||
} | ||
|
||
func (se *SlackEventer) At(name string, attributes map[string]string, when time.Time) { | ||
se.Interval(name, attributes, when, when) | ||
} | ||
|
||
func (se *SlackEventer) Interval(name string, attributes map[string]string, begin, end time.Time) { | ||
var body string | ||
|
||
body = fmt.Sprintf("{\"text\": \"%s\"}", name) | ||
if payload, ok := attributes["payload"]; ok { | ||
body = payload | ||
} | ||
|
||
resp, err := http.Post(se.options.WebHook, "application/json", bytes.NewBuffer([]byte(body))) | ||
if err != nil { | ||
se.logger.Error("slack post:", err) | ||
return | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
rBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
se.logger.Error("slack post response:", err) | ||
return | ||
} | ||
se.logger.Debug(string(rBody)) | ||
} | ||
|
||
func (se *SlackEventer) Stop() { | ||
se.client.CloseIdleConnections() | ||
se.logger.Info("Slack Eventer stopped.") | ||
} | ||
|
||
func NewSlackEventer(options SlackOptions, logger common.Logger, stdout *Stdout) *SlackEventer { | ||
if logger == nil { | ||
logger = stdout | ||
} | ||
|
||
if utils.IsEmpty(options.WebHook) { | ||
logger.Debug("Slack Eventer is disabled") | ||
return nil | ||
} | ||
|
||
logger.Info("Slack Eventer is up…") | ||
|
||
return &SlackEventer{ | ||
options: options, | ||
logger: logger, | ||
tags: common.MapToArray(common.GetKeyValues(options.Tags)), | ||
client: common.MakeHttpClient(options.Timeout), | ||
ctx: context.Background(), | ||
} | ||
} |