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