Skip to content
Snippets Groups Projects
Commit a2ebd7b2 authored by Erick Hitter's avatar Erick Hitter
Browse files

Rethink how webhooks are sent

parent 48ac0970
No related branches found
No related tags found
1 merge request!2Update how webhooks are sent
Checking pipeline status
......@@ -10,6 +10,7 @@ image: golang:latest
before_script:
- ln -s /builds /go/src/git.ethitter.com
- cd /go/src/git.ethitter.com/debian/eth-log-alerting
- go version
stages:
- test
......
......@@ -12,8 +12,8 @@ import (
"regexp"
"syscall"
"github.com/42wim/matterbridge/matterhook"
"github.com/asaskevich/govalidator"
"github.com/ashwanthkumar/slack-go-webhook"
"github.com/hpcloud/tail"
)
......@@ -33,15 +33,6 @@ type logConfig struct {
SearchRegex string `json:"search"`
}
type attachment struct {
Fallback string `json:"fallback,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text"`
Color string `json:"color,omitempty"`
}
type attachments []attachment
var (
configPath string
logConfigs []logConfig
......@@ -122,44 +113,48 @@ func tailLog(logCfg logConfig) {
return
}
mh := matterhook.New(logCfg.WebhookURL, matterhook.Config{DisableServer: true})
parseLinesAndSend(t, mh, logCfg)
parseLinesAndSend(t, logCfg)
t.Stop()
t.Cleanup()
}
func parseLinesAndSend(t *tail.Tail, mh *matterhook.Client, logCfg logConfig) {
func parseLinesAndSend(t *tail.Tail, logCfg logConfig) {
for line := range t.Lines {
if line.Err != nil {
continue
}
if len(logCfg.SearchRegex) == 0 {
go sendLine(line, mh, logCfg)
go sendLine(line, logCfg)
} else if matched, _ := regexp.MatchString(logCfg.SearchRegex, line.Text); matched {
go sendLine(line, mh, logCfg)
go sendLine(line, logCfg)
}
}
}
func sendLine(line *tail.Line, mh *matterhook.Client, logCfg logConfig) {
atts := attachments{
attachment{
Fallback: fmt.Sprintf("New entry in %s", logCfg.LogPath),
Pretext: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time),
Text: fmt.Sprintf(" %s", line.Text),
Color: logCfg.Color,
},
}
func sendLine(line *tail.Line, logCfg logConfig) {
text := fmt.Sprintf(" %s", line.Text)
att := slack.Attachment{}
att.AddField(slack.Field{Title: "Fallback", Value: fmt.Sprintf("New entry in %s", logCfg.LogPath)})
att.AddField(slack.Field{Title: "Pretext", Value: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time)})
att.AddField(slack.Field{Title: "Text", Value: text})
att.AddField(slack.Field{Title: "Color", Value: logCfg.Color})
mh.Send(matterhook.OMessage{
payload := slack.Payload{
Text: text,
Username: logCfg.Username,
Channel: logCfg.Channel,
UserName: logCfg.Username,
Attachments: atts,
IconURL: logCfg.IconURL,
})
IconEmoji: logCfg.IconURL,
Attachments: []slack.Attachment{att},
}
err := slack.Send(logCfg.WebhookURL, "", payload)
if len(err) > 0 {
fmt.Printf("error: %s\n", err)
}
}
func setUpLogger() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment