Skip to content
Snippets Groups Projects
Commit 5a437b0f authored by Yu-Hsin Yang's avatar Yu-Hsin Yang
Browse files

add formated slack message in alert.go

parent 8e2d9a86
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,8 @@ import (
"log/slog"
"log/syslog"
"net/http"
"reflect"
"strings"
"time"
)
......@@ -107,6 +109,38 @@ func PostSlackHook(message string) {
log.Printf("Slack Response StatusCode: %d", resp.StatusCode)
}
// Format the canary alert message
func formatSlackMessage(canary interface{}) string {
v := reflect.ValueOf(canary)
t := v.Type()
var message strings.Builder
message.WriteString("*Canary Alert*\n\n")
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
typeField := t.Field(i)
if typeField.Name == "Canary" {
for j := 0; j < field.NumField(); j++ {
subField := field.Field(j)
subTypeField := field.Type().Field(j)
message.WriteString(fmt.Sprintf("*%s*: `%v`\n", subTypeField.Name, subField.Interface()))
}
} else {
message.WriteString(fmt.Sprintf("*%s*: `%v`\n", typeField.Name, field.Interface()))
}
}
return message.String()
}
func (h HTTPCanary) FormatSlackMessage() string {
return formatSlackMessage(h)
}
func (d DNSCanary) FormatSlackMessage() string {
return formatSlackMessage(d)
}
// Handler for HTTP canary alerts
func HTTPAlert(canaryinfo HTTPCanary, alertType string) {
if alertType == "log" || alertType == "all" {
......@@ -131,7 +165,10 @@ func HTTPAlert(canaryinfo HTTPCanary, alertType string) {
if alertType == "slack" || alertType == "all" {
//Slack alert if configured
PostSlackHook(fmt.Sprintf("``` %+v ```", canaryinfo))
// PostSlackHook(fmt.Sprintf("``` %+v ```", canaryinfo))
// Slack alert if configured
PostSlackHook(canaryinfo.FormatSlackMessage())
}
}
......@@ -156,7 +193,9 @@ func DNSAlert(canaryinfo DNSCanary, alertType string) {
if alertType == "slack" || alertType == "all" {
//Slack alert if configured
PostSlackHook(fmt.Sprintf("``` %+v ```", canaryinfo))
// PostSlackHook(fmt.Sprintf("``` %+v ```", canaryinfo))
// Slack alert if configured
PostSlackHook(canaryinfo.FormatSlackMessage())
}
}
......@@ -17,8 +17,10 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ipinfo/go/v2 v2.10.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
......@@ -31,6 +33,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
......
......@@ -10,6 +10,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ipinfo/go/v2 v2.10.0 h1:v9sFjaxnVVD+JVgpWpjgwols18Tuu4SgBDaHHaw0IXo=
github.com/ipinfo/go/v2 v2.10.0/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/landlock-lsm/go-landlock v0.0.0-20240216195629-efb66220540a h1:dz+a1MiMQksVhejeZwqJuzPawYQBwug74J8PPtkLl9U=
......@@ -20,6 +22,8 @@ github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs=
github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
......@@ -66,7 +70,9 @@ golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment