You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

createalertrulelogic.go 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package monitoring
  2. import (
  3. "context"
  4. v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  10. v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
  11. "k8s.io/apimachinery/pkg/util/intstr"
  12. "k8s.io/apimachinery/pkg/util/json"
  13. "strconv"
  14. )
  15. type CreateAlertRuleLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewCreateAlertRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAlertRuleLogic {
  21. return &CreateAlertRuleLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. type RuleSelectorResp struct {
  28. Code int `json:"code"`
  29. Msg string `json:"msg"`
  30. Prometheus v1.Prometheus `json:"data"`
  31. }
  32. func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) error {
  33. // save to db
  34. var alertRule models.AlertRule
  35. tool.Convert(req, &alertRule)
  36. alertRule.ClusterId, _ = strconv.ParseInt(req.CLusterId, 10, 64)
  37. alertRule.Id = tool.GenSnowflakeID()
  38. tx := l.svcCtx.DbEngin.Save(&alertRule)
  39. if tx.Error != nil {
  40. return tx.Error
  41. }
  42. // query server http url.
  43. var server string
  44. l.svcCtx.DbEngin.Raw("select ta.server from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and tc.name = ?", &req.ClusterName).Scan(&server)
  45. // rule selector
  46. var ruleSelectorResp RuleSelectorResp
  47. response, err := l.svcCtx.HttpClient.R().
  48. SetQueryParams(map[string]string{
  49. "clusterName": req.ClusterName,
  50. }).
  51. SetResult(&ruleSelectorResp).
  52. ForceContentType("application/json").
  53. Get(server + "/api/v1/monitoring/rule/selector")
  54. if err != nil || response.IsError() {
  55. logx.Error(response)
  56. return err
  57. }
  58. // Data Filling
  59. ruleDuration := v1.Duration(req.Duration)
  60. rule := &v1.PrometheusRule{
  61. TypeMeta: v12.TypeMeta{Kind: "PrometheusRule",
  62. APIVersion: "monitoring.coreos.com/v1"},
  63. ObjectMeta: v12.ObjectMeta{
  64. Name: req.Name,
  65. Namespace: ruleSelectorResp.Prometheus.ObjectMeta.Namespace,
  66. Labels: ruleSelectorResp.Prometheus.Spec.RuleSelector.MatchLabels,
  67. },
  68. Spec: v1.PrometheusRuleSpec{
  69. Groups: []v1.RuleGroup{
  70. {
  71. Name: "example-group",
  72. Rules: []v1.Rule{
  73. {
  74. Alert: req.Name,
  75. Expr: intstr.FromString(req.PromQL),
  76. For: &ruleDuration,
  77. Labels: map[string]string{
  78. "severity": req.AlertLevel,
  79. },
  80. Annotations: map[string]string{"description": req.Annotations},
  81. },
  82. },
  83. },
  84. },
  85. },
  86. }
  87. ruleBytes, err := json.Marshal(rule)
  88. if err != nil {
  89. return err
  90. }
  91. // create prometheus rule
  92. response, err = l.svcCtx.HttpClient.R().
  93. SetBody(&OperateStruct{
  94. ClusterName: req.ClusterName,
  95. YamlString: string(ruleBytes),
  96. }).
  97. ForceContentType("application/json").
  98. Post(server + "/api/v1/operate/apply")
  99. if err != nil || response.IsError() {
  100. return err
  101. }
  102. return nil
  103. }
  104. type OperateStruct struct {
  105. ClusterName string `json:"clusterName"`
  106. YamlString string `json:"yamlString"`
  107. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.