Skip to content

Commit 4bc9b15

Browse files
committed
feat(prometheus): 实现Alertmanager兼容API并重构告警处理
- 新增Alertmanager API v2兼容接口用于接收Prometheus告警 - 重构告警服务架构,替换原有的轮询模式为推送模式 - 添加docker-compose配置支持Prometheus管理API - 移除过时的AlertWebhookService实现
1 parent b6e76bd commit 4bc9b15

File tree

9 files changed

+290
-290
lines changed

9 files changed

+290
-290
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api
2+
3+
import (
4+
"github.com/fox-gonic/fox"
5+
"github.com/qiniu/zeroops/internal/prometheus_adapter/service"
6+
)
7+
8+
// setupAlertmanagerRouters 设置 Alertmanager 兼容路由
9+
// 这些路由模拟 Alertmanager API,接收 Prometheus 的告警推送
10+
func (api *Api) setupAlertmanagerRouters(router *fox.Engine, alertmanagerService *service.AlertmanagerService) {
11+
// Alertmanager API v2 告警接收端点
12+
router.POST("/api/v2/alerts", func(c *fox.Context) {
13+
alertmanagerService.HandleAlertsV2(c.Writer, c.Request)
14+
})
15+
16+
// 健康检查端点
17+
router.GET("/-/healthy", func(c *fox.Context) {
18+
alertmanagerService.HandleHealthCheck(c.Writer, c.Request)
19+
})
20+
21+
// 就绪检查端点
22+
router.GET("/-/ready", func(c *fox.Context) {
23+
alertmanagerService.HandleReadyCheck(c.Writer, c.Request)
24+
})
25+
}

internal/prometheus_adapter/api/api.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import (
1212

1313
// Api Prometheus Adapter API
1414
type Api struct {
15-
metricService *service.MetricService
16-
alertService *service.AlertService
17-
router *fox.Engine
15+
metricService *service.MetricService
16+
alertService *service.AlertService
17+
alertmanagerService *service.AlertmanagerService
18+
router *fox.Engine
1819
}
1920

2021
// NewApi 创建新的 API
21-
func NewApi(metricService *service.MetricService, alertService *service.AlertService, router *fox.Engine) (*Api, error) {
22+
func NewApi(metricService *service.MetricService, alertService *service.AlertService, alertmanagerService *service.AlertmanagerService, router *fox.Engine) (*Api, error) {
2223
api := &Api{
23-
metricService: metricService,
24-
alertService: alertService,
25-
router: router,
24+
metricService: metricService,
25+
alertService: alertService,
26+
alertmanagerService: alertmanagerService,
27+
router: router,
2628
}
2729

2830
api.setupRouters(router)
@@ -35,6 +37,8 @@ func (api *Api) setupRouters(router *fox.Engine) {
3537
api.setupMetricRouters(router)
3638
// 告警相关路由
3739
api.setupAlertRouters(router)
40+
// Alertmanager 兼容路由
41+
api.setupAlertmanagerRouters(router, api.alertmanagerService)
3842
}
3943

4044
// ========== 通用辅助方法 ==========

internal/prometheus_adapter/model/alert.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ type AlertRuleMeta struct {
1717
Labels string `json:"labels" gorm:"type:jsonb"` // 适用标签,如 {"service":"s3","version":"v1"},为空表示全局
1818
Threshold float64 `json:"threshold"` // 阈值(会被渲染成特定规则的 threshold metric 数值)
1919
}
20+
21+
// AlertmanagerAlert 符合 Alertmanager API v2 的告警格式
22+
type AlertmanagerAlert struct {
23+
Labels map[string]string `json:"labels"`
24+
Annotations map[string]string `json:"annotations,omitempty"`
25+
StartsAt string `json:"startsAt,omitempty"` // RFC3339 格式
26+
EndsAt string `json:"endsAt,omitempty"` // RFC3339 格式
27+
GeneratorURL string `json:"generatorURL,omitempty"`
28+
}

internal/prometheus_adapter/server.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515

1616
// PrometheusAdapterServer Prometheus Adapter 服务器
1717
type PrometheusAdapterServer struct {
18-
config *config.Config
19-
promConfig *promconfig.PrometheusAdapterConfig
20-
promClient *client.PrometheusClient
21-
metricService *service.MetricService
22-
alertService *service.AlertService
23-
alertWebhookService *service.AlertWebhookService
24-
api *api.Api
18+
config *config.Config
19+
promConfig *promconfig.PrometheusAdapterConfig
20+
promClient *client.PrometheusClient
21+
metricService *service.MetricService
22+
alertService *service.AlertService
23+
alertmanagerProxyService *service.AlertmanagerService
24+
api *api.Api
2525
}
2626

2727
// NewPrometheusAdapterServer 创建新的 Prometheus Adapter 服务器
@@ -44,22 +44,16 @@ func NewPrometheusAdapterServer(cfg *config.Config) (*PrometheusAdapterServer, e
4444
// 创建告警服务
4545
alertService := service.NewAlertService(promClient, promConfig)
4646

47-
// 创建告警 Webhook 服务
48-
alertWebhookService := service.NewAlertWebhookService(promClient, promConfig)
47+
// 创建 Alertmanager 代理服务
48+
alertmanagerProxyService := service.NewAlertmanagerProxyService(promConfig)
4949

5050
server := &PrometheusAdapterServer{
51-
config: cfg,
52-
promConfig: promConfig,
53-
promClient: promClient,
54-
metricService: metricService,
55-
alertService: alertService,
56-
alertWebhookService: alertWebhookService,
57-
}
58-
59-
// 启动告警 Webhook 服务
60-
if err := alertWebhookService.Start(); err != nil {
61-
log.Error().Err(err).Msg("Failed to start alert webhook service")
62-
// 不返回错误,允许服务继续运行
51+
config: cfg,
52+
promConfig: promConfig,
53+
promClient: promClient,
54+
metricService: metricService,
55+
alertService: alertService,
56+
alertmanagerProxyService: alertmanagerProxyService,
6357
}
6458

6559
log.Info().Str("prometheus_address", promConfig.Prometheus.Address).Msg("Prometheus Adapter initialized successfully")
@@ -69,23 +63,20 @@ func NewPrometheusAdapterServer(cfg *config.Config) (*PrometheusAdapterServer, e
6963
// UseApi 设置 API 路由
7064
func (s *PrometheusAdapterServer) UseApi(router *fox.Engine) error {
7165
var err error
72-
s.api, err = api.NewApi(s.metricService, s.alertService, router)
66+
s.api, err = api.NewApi(s.metricService, s.alertService, s.alertmanagerProxyService, router)
7367
if err != nil {
7468
return fmt.Errorf("failed to initialize API: %w", err)
7569
}
7670

71+
log.Info().Msg("All API endpoints registered")
72+
7773
return nil
7874
}
7975

8076
// Close 优雅关闭服务器
8177
func (s *PrometheusAdapterServer) Close(ctx context.Context) error {
8278
log.Info().Msg("Starting shutdown...")
8379

84-
// 停止告警 Webhook 服务
85-
if s.alertWebhookService != nil {
86-
s.alertWebhookService.Stop()
87-
}
88-
8980
// 调用 alertService 的 Shutdown 方法保存规则
9081
if s.alertService != nil {
9182
if err := s.alertService.Shutdown(); err != nil {

internal/prometheus_adapter/service/alert_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func (s *AlertService) buildExpression(rule *model.AlertRule, meta *model.AlertR
411411
if len(labelMatchers) > 0 {
412412
// 如果表达式包含{,说明已经有标签选择器
413413
if strings.Contains(expr, "{") {
414-
expr = strings.Replace(expr, "}", ","+strings.Join(labelMatchers, ",")+"}}", 1)
414+
expr = strings.Replace(expr, "}", ","+strings.Join(labelMatchers, ",")+"}", 1)
415415
} else {
416416
// 在指标名后添加标签选择器
417417
// 查找第一个非字母数字下划线的字符

0 commit comments

Comments
 (0)