wood chen 68c27b544b feat(metrics): enhance metrics functionality and configuration
- Added new dependency on github.com/mattn/go-sqlite3 for improved metrics storage.
- Updated main.go to initialize metrics collector with a new database path and configuration settings.
- Enhanced config.json to include additional metrics settings such as alert configurations and latency thresholds.
- Refactored internal metrics handling to support new metrics structures and improve data retrieval.
- Introduced a new metrics history endpoint for retrieving historical data, enhancing monitoring capabilities.
- Improved UI for metrics dashboard to include historical data visualization options.
2024-12-03 17:48:11 +08:00

82 lines
1.7 KiB
Go

package monitor
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
)
type FeishuHandler struct {
webhookURL string
client *http.Client
cardPool sync.Pool
}
func NewFeishuHandler(webhookURL string) *FeishuHandler {
h := &FeishuHandler{
webhookURL: webhookURL,
client: &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
},
}
h.cardPool = sync.Pool{
New: func() interface{} {
return &FeishuCard{}
},
}
return h
}
type FeishuCard struct {
MsgType string `json:"msg_type"`
Card struct {
Header struct {
Title struct {
Content string `json:"content"`
Tag string `json:"tag"`
} `json:"title"`
} `json:"header"`
Elements []interface{} `json:"elements"`
} `json:"card"`
}
func (h *FeishuHandler) HandleAlert(alert Alert) {
card := h.cardPool.Get().(*FeishuCard)
// 设置标题
card.Card.Header.Title.Tag = "plain_text"
card.Card.Header.Title.Content = fmt.Sprintf("[%s] 监控告警", alert.Level)
// 添加告警内容
content := map[string]interface{}{
"tag": "div",
"text": map[string]interface{}{
"content": fmt.Sprintf("**告警时间**: %s\n**告警内容**: %s",
alert.Time.Format("2006-01-02 15:04:05"),
alert.Message),
"tag": "lark_md",
},
}
card.Card.Elements = []interface{}{content}
// 发送请求
payload, _ := json.Marshal(card)
resp, err := h.client.Post(h.webhookURL, "application/json", bytes.NewBuffer(payload))
if err != nil {
fmt.Printf("Failed to send Feishu alert: %v\n", err)
return
}
defer resp.Body.Close()
h.cardPool.Put(card)
}