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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. )
  14. type CreateAlertRuleLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewCreateAlertRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAlertRuleLogic {
  20. return &CreateAlertRuleLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. type RuleSelectorResp struct {
  27. Code int `json:"code"`
  28. Msg string `json:"msg"`
  29. Prometheus v1.Prometheus `json:"data"`
  30. }
  31. func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) error {
  32. // save to db
  33. var alertRule models.AlertRule
  34. tool.Convert(req, &alertRule)
  35. alertRule.Id = tool.GenSnowflakeID()
  36. tx := l.svcCtx.DbEngin.Save(&alertRule)
  37. if tx.Error != nil {
  38. return tx.Error
  39. }
  40. // query server http url.
  41. var server string
  42. 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)
  43. // rule selector
  44. var ruleSelectorResp RuleSelectorResp
  45. response, err := l.svcCtx.HttpClient.R().
  46. SetQueryParams(map[string]string{
  47. "clusterName": req.ClusterName,
  48. }).
  49. SetResult(&ruleSelectorResp).
  50. ForceContentType("application/json").
  51. Get(server + "/api/v1/monitoring/rule/selector")
  52. if err != nil || response.IsError() {
  53. return err
  54. }
  55. // Data Filling
  56. ruleDuration := v1.Duration(req.Duration)
  57. rule := &v1.PrometheusRule{
  58. TypeMeta: v12.TypeMeta{Kind: "PrometheusRule",
  59. APIVersion: "monitoring.coreos.com/v1"},
  60. ObjectMeta: v12.ObjectMeta{
  61. Name: req.Name,
  62. Namespace: ruleSelectorResp.Prometheus.ObjectMeta.Namespace,
  63. Labels: ruleSelectorResp.Prometheus.Spec.RuleSelector.MatchLabels,
  64. },
  65. Spec: v1.PrometheusRuleSpec{
  66. Groups: []v1.RuleGroup{
  67. {
  68. Name: "example-group",
  69. Rules: []v1.Rule{
  70. {
  71. Alert: req.Name,
  72. Expr: intstr.FromString(req.PromQL),
  73. For: &ruleDuration,
  74. Labels: map[string]string{
  75. "severity": req.AlertLevel,
  76. },
  77. Annotations: map[string]string{"description": req.Annotations},
  78. },
  79. },
  80. },
  81. },
  82. },
  83. }
  84. ruleBytes, err := json.Marshal(rule)
  85. if err != nil {
  86. return err
  87. }
  88. // create prometheus rule
  89. response, err = l.svcCtx.HttpClient.R().
  90. SetBody(&OperateStruct{
  91. ClusterName: req.ClusterName,
  92. YamlString: string(ruleBytes),
  93. }).
  94. ForceContentType("application/json").
  95. Post(server + "/api/v1/operate/apply")
  96. if err != nil || response.IsError() {
  97. return err
  98. }
  99. return nil
  100. }
  101. type OperateStruct struct {
  102. ClusterName string `json:"clusterName"`
  103. YamlString string `json:"yamlString"`
  104. }

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.