fix notification fields order

This commit is contained in:
yusing 2025-02-01 01:42:49 +08:00
parent c00395196f
commit 49fb716135
3 changed files with 23 additions and 24 deletions

View file

@ -14,10 +14,15 @@ type (
logCh chan *LogMessage logCh chan *LogMessage
providers F.Set[Provider] providers F.Set[Provider]
} }
LogField struct {
Name string `json:"name"`
Value string `json:"value"`
}
LogFields []LogField
LogMessage struct { LogMessage struct {
Level zerolog.Level Level zerolog.Level
Title string Title string
Extras map[string]any Extras LogFields
Color Color Color Color
} }
) )
@ -48,6 +53,10 @@ func Notify(msg *LogMessage) {
} }
} }
func (f *LogFields) Add(name, value string) {
*f = append(*f, LogField{Name: name, Value: value})
}
func (disp *Dispatcher) RegisterProvider(cfg *NotificationConfig) { func (disp *Dispatcher) RegisterProvider(cfg *NotificationConfig) {
disp.providers.Add(cfg.Provider) disp.providers.Add(cfg.Provider)
} }

View file

@ -3,32 +3,22 @@ package notif
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
func formatMarkdown(extras map[string]interface{}) string { func formatMarkdown(extras LogFields) string {
msg := bytes.NewBufferString("") msg := bytes.NewBufferString("")
for k, v := range extras { for _, field := range extras {
msg.WriteString("#### ") msg.WriteString("#### ")
msg.WriteString(k) msg.WriteString(field.Name)
msg.WriteRune('\n') msg.WriteRune('\n')
msg.WriteString(fmt.Sprintf("%v", v)) msg.WriteString(field.Value)
msg.WriteRune('\n') msg.WriteRune('\n')
} }
return msg.String() return msg.String()
} }
func formatDiscord(extras map[string]interface{}) (string, error) { func formatDiscord(extras LogFields) (string, error) {
fieldsMap := make([]map[string]any, len(extras)) fields, err := json.Marshal(extras)
i := 0
for k, extra := range extras {
fieldsMap[i] = map[string]any{
"name": k,
"value": extra,
}
i++
}
fields, err := json.Marshal(fieldsMap)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -198,22 +198,22 @@ func (mon *monitor) checkUpdateHealth() error {
status = health.StatusUnhealthy status = health.StatusUnhealthy
} }
if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) { if result.Healthy != (mon.status.Swap(status) == health.StatusHealthy) {
extras := map[string]any{ extras := notif.LogFields{
"Service Name": mon.service, {Name: "Service Name", Value: mon.service},
"Time": strutils.FormatTime(time.Now()), {Name: "Time", Value: strutils.FormatTime(time.Now())},
} }
if !result.Healthy { if !result.Healthy {
extras["Last Seen"] = strutils.FormatLastSeen(GetLastSeen(mon.service)) extras.Add("Last Seen", strutils.FormatLastSeen(GetLastSeen(mon.service)))
} }
if !mon.url.Load().Nil() { if !mon.url.Load().Nil() {
extras["Service URL"] = mon.url.Load().String() extras.Add("Service URL", mon.url.Load().String())
} }
if result.Detail != "" { if result.Detail != "" {
extras["Detail"] = result.Detail extras.Add("Detail", result.Detail)
} }
if result.Healthy { if result.Healthy {
logger.Info().Msg("service is up") logger.Info().Msg("service is up")
extras["Ping"] = fmt.Sprintf("%d ms", result.Latency.Milliseconds()) extras.Add("Ping", fmt.Sprintf("%d ms", result.Latency.Milliseconds()))
notif.Notify(&notif.LogMessage{ notif.Notify(&notif.LogMessage{
Title: "✅ Service is up ✅", Title: "✅ Service is up ✅",
Extras: extras, Extras: extras,