修改infra除repository包
This commit is contained in:
@@ -2,11 +2,14 @@ package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,6 +21,8 @@ const (
|
||||
|
||||
// larkNotifier 实现了 Notifier 接口,用于通过飞书自建应用发送私聊消息。
|
||||
type larkNotifier struct {
|
||||
ctx context.Context
|
||||
|
||||
appID string // 应用 ID
|
||||
appSecret string // 应用密钥
|
||||
|
||||
@@ -29,8 +34,9 @@ type larkNotifier struct {
|
||||
|
||||
// NewLarkNotifier 创建一个新的 larkNotifier 实例。
|
||||
// 调用者需要注入飞书应用的 AppID 和 AppSecret。
|
||||
func NewLarkNotifier(appID, appSecret string) Notifier {
|
||||
func NewLarkNotifier(ctx context.Context, appID, appSecret string) Notifier {
|
||||
return &larkNotifier{
|
||||
ctx: ctx,
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
}
|
||||
@@ -38,9 +44,10 @@ func NewLarkNotifier(appID, appSecret string) Notifier {
|
||||
|
||||
// Send 向指定用户发送一条飞书消息卡片。
|
||||
// toAddr 参数是接收者的邮箱地址。
|
||||
func (l *larkNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
func (l *larkNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
notifierCtx := logs.AddFuncName(ctx, l.ctx, "Send")
|
||||
// 1. 获取有效的 tenant_access_token
|
||||
token, err := l.getAccessToken()
|
||||
token, err := l.getAccessToken(notifierCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,7 +122,7 @@ func (l *larkNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
}
|
||||
|
||||
// getAccessToken 获取并缓存 tenant_access_token,处理了线程安全和自动刷新。
|
||||
func (l *larkNotifier) getAccessToken() (string, error) {
|
||||
func (l *larkNotifier) getAccessToken(ctx context.Context) (string, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
)
|
||||
|
||||
// logNotifier 实现了 Notifier 接口,用于将告警信息记录到日志中。
|
||||
type logNotifier struct {
|
||||
logger *logs.Logger
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewLogNotifier 创建一个新的 logNotifier 实例。
|
||||
// 它接收一个日志记录器,用于实际的日志输出。
|
||||
func NewLogNotifier(logger *logs.Logger) Notifier {
|
||||
func NewLogNotifier(ctx context.Context) Notifier {
|
||||
return &logNotifier{
|
||||
logger: logger,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// Send 将告警内容以结构化的方式记录到日志中。
|
||||
// toAddr 参数在这里表示告警的预期接收者地址,也会被记录。
|
||||
func (l *logNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
l.logger.Infow("告警已记录到日志",
|
||||
func (l *logNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
logger := logs.TraceLogger(ctx, l.ctx, "Send")
|
||||
logger.Infow("告警已记录到日志",
|
||||
"notifierType", NotifierTypeLog,
|
||||
"title", content.Title,
|
||||
"message", content.Message,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
|
||||
// smtpNotifier 实现了 Notifier 接口,用于通过 SMTP 发送邮件通知。
|
||||
type smtpNotifier struct {
|
||||
ctx context.Context
|
||||
host string // SMTP 服务器地址
|
||||
port int // SMTP 服务器端口
|
||||
username string // 发件人邮箱地址
|
||||
@@ -17,8 +19,9 @@ type smtpNotifier struct {
|
||||
|
||||
// NewSMTPNotifier 创建一个新的 smtpNotifier 实例。
|
||||
// 调用者需要注入 SMTP 相关的配置。
|
||||
func NewSMTPNotifier(host string, port int, username, password, sender string) Notifier {
|
||||
func NewSMTPNotifier(ctx context.Context, host string, port int, username, password, sender string) Notifier {
|
||||
return &smtpNotifier{
|
||||
ctx: ctx,
|
||||
host: host,
|
||||
port: port,
|
||||
username: username,
|
||||
@@ -28,7 +31,7 @@ func NewSMTPNotifier(host string, port int, username, password, sender string) N
|
||||
}
|
||||
|
||||
// Send 使用 net/smtp 包发送一封邮件。
|
||||
func (s *smtpNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
func (s *smtpNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
// 1. 设置认证信息
|
||||
auth := smtp.PlainAuth("", s.username, s.password, s.host)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -19,6 +20,8 @@ const (
|
||||
|
||||
// wechatNotifier 实现了 Notifier 接口,用于通过企业微信自建应用发送私聊消息。
|
||||
type wechatNotifier struct {
|
||||
ctx context.Context
|
||||
|
||||
corpID string // 企业ID (CorpID)
|
||||
agentID string // 应用ID (AgentID)
|
||||
secret string // 应用密钥 (Secret)
|
||||
@@ -31,8 +34,9 @@ type wechatNotifier struct {
|
||||
|
||||
// NewWechatNotifier 创建一个新的 wechatNotifier 实例。
|
||||
// 调用者需要注入企业微信应用的 CorpID, AgentID 和 Secret。
|
||||
func NewWechatNotifier(corpID, agentID, secret string) Notifier {
|
||||
func NewWechatNotifier(ctx context.Context, corpID, agentID, secret string) Notifier {
|
||||
return &wechatNotifier{
|
||||
ctx: ctx,
|
||||
corpID: corpID,
|
||||
agentID: agentID,
|
||||
secret: secret,
|
||||
@@ -41,7 +45,7 @@ func NewWechatNotifier(corpID, agentID, secret string) Notifier {
|
||||
|
||||
// Send 向指定用户发送一条 markdown 格式的私聊消息。
|
||||
// toAddr 参数是接收者的 UserID 列表,用逗号或竖线分隔。
|
||||
func (w *wechatNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
func (w *wechatNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
// 1. 获取有效的 access_token
|
||||
token, err := w.getAccessToken()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user