129 lines
3.9 KiB
Go
129 lines
3.9 KiB
Go
package monitor
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"eino-project/internal/conf"
|
||
|
||
cozeloop "github.com/coze-dev/cozeloop-go"
|
||
"github.com/go-kratos/kratos/v2/log"
|
||
)
|
||
|
||
// cozeLoopMonitor 是一个包装器:在调用本地监控逻辑的同时,将关键指标以 Trace 形式上报到 Coze Loop
|
||
type cozeLoopMonitor struct {
|
||
base Monitor
|
||
client cozeloop.Client
|
||
log *log.Helper
|
||
}
|
||
|
||
// NewCozeLoopMonitor 根据配置初始化 Coze Loop 客户端并返回包装后的监控器
|
||
func NewCozeLoopMonitor(base Monitor, cfg *conf.Monitoring_CozeLoop, logger log.Logger) Monitor {
|
||
helper := log.NewHelper(logger)
|
||
|
||
ctx := context.Background()
|
||
|
||
// 按官方 simple demo 风格,直接从环境变量初始化客户端
|
||
client, err := cozeloop.NewClient()
|
||
if err != nil {
|
||
helper.WithContext(ctx).Warnf("init CozeLoop client failed: %v, fallback to base monitor", err)
|
||
return base
|
||
}
|
||
|
||
m := &cozeLoopMonitor{
|
||
base: base,
|
||
client: client,
|
||
log: helper,
|
||
}
|
||
|
||
return m
|
||
}
|
||
|
||
// RecordRequest 记录请求并上报简化 Trace
|
||
func (m *cozeLoopMonitor) RecordRequest(ctx context.Context, requestType string, duration time.Duration, success bool) error {
|
||
ctx, span := m.client.StartSpan(ctx, "RecordRequest", requestType)
|
||
|
||
span.Finish(ctx)
|
||
m.client.Close(ctx)
|
||
|
||
// if err := m.base.RecordRequest(ctx, requestType, duration, success); err != nil {
|
||
// return err
|
||
// }
|
||
// 简化:不生成trace
|
||
return nil
|
||
}
|
||
|
||
// RecordAIRequest 记录 AI 请求并上报简化 Trace
|
||
func (m *cozeLoopMonitor) RecordAIRequest(ctx context.Context, duration time.Duration, success bool) error {
|
||
if err := m.base.RecordAIRequest(ctx, duration, success); err != nil {
|
||
return err
|
||
}
|
||
// 简化:不生成trace
|
||
return nil
|
||
}
|
||
|
||
// RecordVectorOperation 记录向量操作并上报简化 Trace
|
||
func (m *cozeLoopMonitor) RecordVectorOperation(ctx context.Context, operation string, success bool) error {
|
||
if err := m.base.RecordVectorOperation(ctx, operation, success); err != nil {
|
||
return err
|
||
}
|
||
// 简化:不生成trace
|
||
return nil
|
||
}
|
||
|
||
// RecordSessionOperation 记录会话操作并上报简化 Trace
|
||
func (m *cozeLoopMonitor) RecordSessionOperation(ctx context.Context, operation string) error {
|
||
if err := m.base.RecordSessionOperation(ctx, operation); err != nil {
|
||
return err
|
||
}
|
||
// 简化:不生成trace
|
||
return nil
|
||
}
|
||
|
||
// RecordKnowledgeOperation 记录知识库操作并上报简化 Trace
|
||
func (m *cozeLoopMonitor) RecordKnowledgeOperation(ctx context.Context, operation string) error {
|
||
if err := m.base.RecordKnowledgeOperation(ctx, operation); err != nil {
|
||
return err
|
||
}
|
||
// 简化:不生成trace
|
||
return nil
|
||
}
|
||
|
||
// 透传LLM使用事件(不生成trace,不做周期性上报)
|
||
func (m *cozeLoopMonitor) RecordLLMUsage(ctx context.Context, usage *LLMUsage) error {
|
||
return m.base.RecordLLMUsage(ctx, usage)
|
||
}
|
||
|
||
// GetMetrics 透传本地指标(不直接从 Coze Loop 读取)
|
||
func (m *cozeLoopMonitor) GetMetrics(ctx context.Context) (*Metrics, error) {
|
||
return m.base.GetMetrics(ctx)
|
||
}
|
||
|
||
// CreateAlert 创建告警(不生成trace,保持简化)
|
||
func (m *cozeLoopMonitor) CreateAlert(ctx context.Context, alertType, level, title, message string, metadata map[string]interface{}) error {
|
||
if err := m.base.CreateAlert(ctx, alertType, level, title, message, metadata); err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// GetAlerts 透传本地告警
|
||
func (m *cozeLoopMonitor) GetAlerts(ctx context.Context, resolved bool) ([]*Alert, error) {
|
||
return m.base.GetAlerts(ctx, resolved)
|
||
}
|
||
|
||
// ResolveAlert 透传本地告警处理
|
||
func (m *cozeLoopMonitor) ResolveAlert(ctx context.Context, alertID string) error {
|
||
return m.base.ResolveAlert(ctx, alertID)
|
||
}
|
||
|
||
// HealthCheck 透传本地健康检查
|
||
func (m *cozeLoopMonitor) HealthCheck(ctx context.Context) error {
|
||
return m.base.HealthCheck(ctx)
|
||
}
|
||
|
||
// ResetMetrics 重置本地指标
|
||
func (m *cozeLoopMonitor) ResetMetrics(ctx context.Context) error {
|
||
return m.base.ResetMetrics(ctx)
|
||
}
|