|
@@ -5,18 +5,23 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
amodels "go-admin/app/admin/models"
|
|
amodels "go-admin/app/admin/models"
|
|
omodels "go-admin/app/observe/models"
|
|
omodels "go-admin/app/observe/models"
|
|
|
|
+ "log"
|
|
|
|
+ "sync"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
+ "github.com/prometheus/client_golang/prometheus"
|
|
|
|
+
|
|
"github.com/go-admin-team/go-admin-core/logger"
|
|
"github.com/go-admin-team/go-admin-core/logger"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm"
|
|
)
|
|
)
|
|
|
|
|
|
type EventHandler struct {
|
|
type EventHandler struct {
|
|
- RP *omodels.OtRulesPolicy `json:"result_policy"`
|
|
|
|
- AC *omodels.AlertCondition `json:"alert_condition"`
|
|
|
|
- JR *omodels.JudgeResult `json:"judge_result"`
|
|
|
|
- Emo *omodels.Events
|
|
|
|
- Errs map[string]error `json:"_"`
|
|
|
|
|
|
+ RP *omodels.OtRulesPolicy `json:"result_policy"`
|
|
|
|
+ AC *omodels.AlertCondition `json:"alert_condition"`
|
|
|
|
+ JR *omodels.JudgeResult `json:"judge_result"`
|
|
|
|
+ Emo *omodels.Events
|
|
|
|
+ promMap *sync.Map
|
|
|
|
+ Errs map[string]error `json:"_"`
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -24,7 +29,7 @@ type EventHandler struct {
|
|
rows: list值,出现代表有异常,立即写入异常事件监控,但不一定告警,需要结合频率,周期进行判断
|
|
rows: list值,出现代表有异常,立即写入异常事件监控,但不一定告警,需要结合频率,周期进行判断
|
|
*/
|
|
*/
|
|
|
|
|
|
-func InitEventHandler() *EventHandler {
|
|
|
|
|
|
+func InitEventHandler(pMap *sync.Map) *EventHandler {
|
|
e := new(EventHandler)
|
|
e := new(EventHandler)
|
|
e.RP = new(omodels.OtRulesPolicy)
|
|
e.RP = new(omodels.OtRulesPolicy)
|
|
e.AC = new(omodels.AlertCondition)
|
|
e.AC = new(omodels.AlertCondition)
|
|
@@ -32,6 +37,7 @@ func InitEventHandler() *EventHandler {
|
|
e.JR.AlertStatus = INACTIVE
|
|
e.JR.AlertStatus = INACTIVE
|
|
//eg:@@UNSET_apdex::health:5m_{"condition":"<","point":0.7,"point_type":"float","tigger_hz":3,"interval":5}
|
|
//eg:@@UNSET_apdex::health:5m_{"condition":"<","point":0.7,"point_type":"float","tigger_hz":3,"interval":5}
|
|
e.Errs = make(map[string]error)
|
|
e.Errs = make(map[string]error)
|
|
|
|
+ e.promMap = pMap
|
|
return e
|
|
return e
|
|
}
|
|
}
|
|
|
|
|
|
@@ -44,8 +50,14 @@ func (e *EventHandler) JudgeRow() *EventHandler {
|
|
e.JR.IsException = true
|
|
e.JR.IsException = true
|
|
e.JR.AlertStatus = PENDING
|
|
e.JR.AlertStatus = PENDING
|
|
}
|
|
}
|
|
|
|
+ /*
|
|
|
|
+ 不处理rows类型
|
|
|
|
+ 取消该类型的解析,在prometheus中进行数量判断及相关收敛规则
|
|
|
|
+ */
|
|
case "row":
|
|
case "row":
|
|
e.JR.CompareV = e.JR.RowResult
|
|
e.JR.CompareV = e.JR.RowResult
|
|
|
|
+ default:
|
|
|
|
+ e.JR.CompareV = e.JR.RowResult
|
|
}
|
|
}
|
|
return e
|
|
return e
|
|
}
|
|
}
|
|
@@ -182,6 +194,58 @@ func (e *EventHandler) SetUID() *EventHandler {
|
|
return e
|
|
return e
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (e *EventHandler) RegisterPrometheusGauge() *EventHandler {
|
|
|
|
+ gauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
+ Name: e.RP.RuleMonitorAlias,
|
|
|
|
+ Help: e.RP.RuleName,
|
|
|
|
+ }, []string{"app_alias", "kind", "uid"})
|
|
|
|
+ if err := prometheus.Register(gauge); err != nil {
|
|
|
|
+ if err.Error() == "duplicate metrics collector registration attempted" {
|
|
|
|
+ logger.Errorf("Failed to register gauge: %v", err)
|
|
|
|
+ return e
|
|
|
|
+ } else {
|
|
|
|
+ log.Fatalf("Failed to register gauge: %v", err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ e.promMap.Store(e.RP.RuleMonitorAlias, gauge)
|
|
|
|
+ return e
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (e *EventHandler) SetPromKV() *EventHandler {
|
|
|
|
+ e.GetRegisteredGauge(e.RP.RuleMonitorAlias).WithLabelValues(
|
|
|
|
+ e.RP.AppAlias, e.RP.RuleKind, e.JR.UID,
|
|
|
|
+ ).Set(float64(e.JR.RowGaugeResult))
|
|
|
|
+ logger.Debugf("e.JR.RowGaugeResult", e.JR.RowGaugeResult)
|
|
|
|
+ return e
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// checkCollectorExists 检查注册表中是否存在具有给定名称的collector
|
|
|
|
+func CheckCollectorExists(reg *prometheus.Registry, name string) error {
|
|
|
|
+ merticF, err := reg.Gather()
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Errorf("checkCollector, error: %s", err.Error())
|
|
|
|
+ }
|
|
|
|
+ for _, m := range merticF {
|
|
|
|
+ fmt.Println("metricFamily: ", m)
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (e *EventHandler) GetRegisteredGauge(gaugeName string) *prometheus.GaugeVec {
|
|
|
|
+ logger.Debugf("guageName: %s", gaugeName)
|
|
|
|
+ logger.Debug(e.promMap)
|
|
|
|
+ promk, ok := e.promMap.Load(gaugeName)
|
|
|
|
+ if ok {
|
|
|
|
+ logger.Debug("has guage name")
|
|
|
|
+ return promk.(*prometheus.GaugeVec)
|
|
|
|
+ } else {
|
|
|
|
+ e.RegisterPrometheusGauge()
|
|
|
|
+ logger.Debug("recreate prometh guagename")
|
|
|
|
+ return e.GetRegisteredGauge(gaugeName)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func (e *EventHandler) CreateEventRecord(chdb *gorm.DB) *EventHandler {
|
|
func (e *EventHandler) CreateEventRecord(chdb *gorm.DB) *EventHandler {
|
|
if e.JR.IsException {
|
|
if e.JR.IsException {
|
|
row := "UNSET"
|
|
row := "UNSET"
|