Skip to content

Commit b7e0908

Browse files
obdevob-robot
authored andcommitted
PullRequest: 548 Metric alarm (#1)
1 parent 883ce66 commit b7e0908

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+5749
-107
lines changed

Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PROCESSOR=2
1+
PROCESSOR=8
22
VERSION=4.3.1.0
33
NAME=obshell
44
RELEASE=0

agent/api/agent_route.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func InitOcsAgentRoutes(s *http2.State, r *gin.Engine, isLocalRoute bool) {
110110
InitBackupRoutes(v1, isLocalRoute)
111111
InitRestoreRoutes(v1, isLocalRoute)
112112
InitObproxyRoutes(v1, isLocalRoute)
113+
InitMetricRoutes(v1, isLocalRoute)
114+
InitAlarmRoutes(v1, isLocalRoute)
115+
116+
system := v1.Group(constant.URI_SYSTEM_GROUP)
117+
InitExternalRoutes(system, isLocalRoute)
113118

114119
// ob routes
115120
ob.POST(constant.URI_INIT, obInitHandler)

agent/api/alarm_handler.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"github.com/gin-gonic/gin"
21+
22+
"github.com/oceanbase/obshell/agent/api/common"
23+
"github.com/oceanbase/obshell/agent/constant"
24+
"github.com/oceanbase/obshell/agent/executor/alarm"
25+
"github.com/oceanbase/obshell/model/alarm/alert"
26+
"github.com/oceanbase/obshell/model/alarm/rule"
27+
"github.com/oceanbase/obshell/model/alarm/silence"
28+
)
29+
30+
// ListAlerts godoc
31+
// @ID ListAlerts
32+
// @Summary List all alerts
33+
// @Description List all alerts
34+
// @Tags alarm
35+
// @Accept json
36+
// @Produce json
37+
// @Param filter body alert.AlertFilter false "alert filter"
38+
// @Success 200 {object} http.OcsAgentResponse{data=[]alert.Alert}
39+
// @Router /api/v1/alarm/alerts [post]
40+
func ListAlerts(ctx *gin.Context) {
41+
filter := &alert.AlertFilter{}
42+
err := ctx.Bind(filter)
43+
if err != nil {
44+
common.SendResponse(ctx, nil, err)
45+
return
46+
}
47+
data, err := alarm.ListAlerts(ctx, filter)
48+
common.SendResponse(ctx, data, err)
49+
}
50+
51+
// ListSilencers godoc
52+
// @ID ListSilencers
53+
// @Summary List all silencers
54+
// @Description List all silencers
55+
// @Tags alarm
56+
// @Accept json
57+
// @Produce json
58+
// @Param filter body silence.SilencerFilter false "silencer filter"
59+
// @Success 200 {object} http.OcsAgentResponse{data=[]silence.SilencerResponse}
60+
// @Router /api/v1/alarm/silencers [post]
61+
func ListSilencers(ctx *gin.Context) {
62+
filter := &silence.SilencerFilter{}
63+
err := ctx.Bind(filter)
64+
if err != nil {
65+
common.SendResponse(ctx, nil, err)
66+
return
67+
}
68+
data, err := alarm.ListSilencers(ctx, filter)
69+
common.SendResponse(ctx, data, err)
70+
}
71+
72+
// GetSilencer godoc
73+
// @ID GetSilencer
74+
// @Summary Get a silencer
75+
// @Description Get a silencer by id
76+
// @Tags alarm
77+
// @Accept json
78+
// @Produce json
79+
// @Param id path string true "silencer id"
80+
// @Success 200 {object} http.OcsAgentResponse{data=silence.SilencerResponse}
81+
// @Router /api/v1/alarm/silencer/{id} [get]
82+
func GetSilencer(ctx *gin.Context) {
83+
id := ctx.Param("id")
84+
data, err := alarm.GetSilencer(ctx, id)
85+
common.SendResponse(ctx, data, err)
86+
}
87+
88+
// CreateOrUpdateSilencer godoc
89+
// @ID CreateOrUpdateSilencer
90+
// @Summary Create or update a silencer
91+
// @Description Create or update a silencer
92+
// @Tags alarm
93+
// @Accept json
94+
// @Produce json
95+
// @Param silencer body silence.SilencerParam true "silencer"
96+
// @Success 200 {object} http.OcsAgentResponse{data=silence.SilencerResponse}
97+
// @Router /api/v1/alarm/silencer [put]
98+
func CreateOrUpdateSilencer(ctx *gin.Context) {
99+
param := &silence.SilencerParam{}
100+
err := ctx.Bind(param)
101+
if err != nil {
102+
common.SendResponse(ctx, nil, err)
103+
return
104+
}
105+
data, err := alarm.CreateOrUpdateSilencer(ctx, param)
106+
common.SendResponse(ctx, data, err)
107+
}
108+
109+
// DeleteSilencer godoc
110+
// @ID DeleteSilencer
111+
// @Summary Delete a silencer
112+
// @Description Delete a silencer by id
113+
// @Tags alarm
114+
// @Accept json
115+
// @Produce json
116+
// @Param id path string true "silencer id"
117+
// @Success 200 {object} http.OcsAgentResponse
118+
// @Router /api/v1/alarm/silencer/{id} [delete]
119+
func DeleteSilencer(ctx *gin.Context) {
120+
id := ctx.Param("id")
121+
err := alarm.DeleteSilencer(ctx, id)
122+
common.SendResponse(ctx, nil, err)
123+
}
124+
125+
// ListRules godoc
126+
// @ID ListRules
127+
// @Summary List all rules
128+
// @Description List all rules
129+
// @Tags alarm
130+
// @Accept json
131+
// @Produce json
132+
// @Param filter body rule.RuleFilter false "rule filter"
133+
// @Success 200 {object} http.OcsAgentResponse{data=[]rule.RuleResponse}
134+
// @Router /api/v1/alarm/rules [post]
135+
func ListRules(ctx *gin.Context) {
136+
filter := &rule.RuleFilter{}
137+
err := ctx.Bind(filter)
138+
if err != nil {
139+
common.SendResponse(ctx, nil, err)
140+
return
141+
}
142+
data, err := alarm.ListRules(ctx, filter)
143+
common.SendResponse(ctx, data, err)
144+
}
145+
146+
// GetRule godoc
147+
// @ID GetRule
148+
// @Summary Get a rule
149+
// @Description Get a rule by name
150+
// @Tags alarm
151+
// @Accept json
152+
// @Produce json
153+
// @Param name path string true "rule name"
154+
// @Success 200 {object} http.OcsAgentResponse{data=rule.RuleResponse}
155+
// @Router /api/v1/alarm/rule/{name} [get]
156+
func GetRule(ctx *gin.Context) {
157+
name := ctx.Param(constant.URI_PARAM_NAME)
158+
data, err := alarm.GetRule(ctx, name)
159+
common.SendResponse(ctx, data, err)
160+
}

agent/api/alarm_route.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"github.com/gin-gonic/gin"
21+
22+
"github.com/oceanbase/obshell/agent/api/common"
23+
"github.com/oceanbase/obshell/agent/constant"
24+
)
25+
26+
func InitAlarmRoutes(parentGroup *gin.RouterGroup, isLocalRoute bool) {
27+
alarm := parentGroup.Group(constant.URI_ALARM_GROUP)
28+
29+
if !isLocalRoute {
30+
alarm.Use(common.Verify())
31+
}
32+
33+
// alerts
34+
alarm.POST(constant.URI_ALERTS, ListAlerts)
35+
36+
// silencers
37+
alarm.POST(constant.URI_SILENCERS, ListSilencers)
38+
alarm.GET(constant.URI_SILENCER+constant.URI_PATH_PARAM_ID, GetSilencer)
39+
alarm.PUT(constant.URI_SILENCER, CreateOrUpdateSilencer)
40+
alarm.DELETE(constant.URI_SILENCER+constant.URI_PATH_PARAM_ID, DeleteSilencer)
41+
42+
// rules
43+
alarm.POST(constant.URI_RULES, ListRules)
44+
alarm.GET(constant.URI_RULE+constant.URI_PATH_PARAM_NAME, GetRule)
45+
}

agent/api/common/middleware.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net"
2626
"net/http"
2727
"regexp"
28+
"runtime/debug"
2829
"strconv"
2930
"strings"
3031
"syscall"
@@ -53,12 +54,12 @@ const UNIX_CONNECT UNIX_CONNECT_TYPE = "unix_conn"
5354
const (
5455
statusURI = constant.URI_API_V1 + constant.URI_STATUS
5556

56-
localRouteKey = "localRoute"
57-
apiRouteKey = "apiRoute"
57+
localRouteKey = constant.LOCAL_ROUTE_KEY
58+
apiRouteKey = constant.API_ROUTE_KEY
5859

59-
originalBody = "ORIGINAL_BODY"
60+
originalBody = constant.ORIGINAL_BODY
6061

61-
ACCEPT_LANGUAGE = "Accept-Language"
62+
ACCEPT_LANGUAGE = constant.ACCEPT_LANGUAGE
6263
)
6364

6465
var (
@@ -352,7 +353,7 @@ func PostHandlers(excludeRoutes ...string) func(*gin.Context) {
352353

353354
// Recovery is a utility function meant to be used with the Gin middleware for panic recovery.
354355
func Recovery(c *gin.Context, err interface{}) {
355-
log.WithContext(NewContextWithTraceId(c)).Errorf("request context %+v, err:%+v", c, err)
356+
log.WithContext(NewContextWithTraceId(c)).Errorf("request context %+v, err:%+v, stack:%s", c, err, debug.Stack())
356357
c.JSON(recoveryResponse.Status, recoveryResponse)
357358
}
358359

agent/api/external_handler.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"github.com/gin-gonic/gin"
21+
22+
"github.com/oceanbase/obshell/agent/api/common"
23+
externalexecutor "github.com/oceanbase/obshell/agent/executor/external"
24+
"github.com/oceanbase/obshell/model/external"
25+
)
26+
27+
// @Summary Set Prometheus configuration
28+
// @Description Set Prometheus configuration
29+
// @Tags system
30+
// @Accept json
31+
// @Produce json
32+
// @Param config body external.PrometheusConfig true "Prometheus configuration"
33+
// @Success 200 {object} http.OcsAgentResponse
34+
// @Failure 400 {object} http.OcsAgentResponse
35+
// @Failure 500 {object} http.OcsAgentResponse
36+
// @Router /api/v1/system/external/prometheus [put]
37+
func SetPrometheusConfig(c *gin.Context) {
38+
var cfg external.PrometheusConfig
39+
if err := c.BindJSON(&cfg); err != nil {
40+
common.SendResponse(c, nil, err)
41+
return
42+
}
43+
common.SendResponse(c, nil, externalexecutor.SavePrometheusConfig(c, &cfg))
44+
}
45+
46+
// @Summary Get Prometheus configuration
47+
// @Description Get Prometheus configuration
48+
// @Tags system
49+
// @Accept json
50+
// @Produce json
51+
// @Success 200 {object} http.OcsAgentResponse{data=external.PrometheusConfig}
52+
// @Failure 500 {object} http.OcsAgentResponse
53+
// @Router /api/v1/system/external/prometheus [get]
54+
func GetPrometheusConfig(c *gin.Context) {
55+
cfg, err := externalexecutor.GetPrometheusConfig(c)
56+
if err != nil {
57+
common.SendResponse(c, nil, err)
58+
return
59+
}
60+
common.SendResponse(c, cfg.Address, nil)
61+
}
62+
63+
// @Summary Set Alertmanager configuration
64+
// @Description Set Alertmanager configuration
65+
// @Tags system
66+
// @Accept json
67+
// @Produce json
68+
// @Param config body external.AlertmanagerConfig true "Alertmanager configuration"
69+
// @Success 200 {object} http.OcsAgentResponse
70+
// @Failure 400 {object} http.OcsAgentResponse
71+
// @Failure 500 {object} http.OcsAgentResponse
72+
// @Router /api/v1/system/external/alertmanager [put]
73+
func SetAlertmanagerConfig(c *gin.Context) {
74+
var cfg external.AlertmanagerConfig
75+
if err := c.BindJSON(&cfg); err != nil {
76+
common.SendResponse(c, nil, err)
77+
return
78+
}
79+
common.SendResponse(c, nil, externalexecutor.SaveAlertmanagerConfig(c, &cfg))
80+
}
81+
82+
// @Summary Get Alertmanager configuration
83+
// @Description Get Alertmanager configuration
84+
// @Tags system
85+
// @Accept json
86+
// @Produce json
87+
// @Success 200 {object} http.OcsAgentResponse{data=external.AlertmanagerConfig}
88+
// @Failure 500 {object} http.OcsAgentResponse
89+
// @Router /api/v1/system/external/alertmanager [get]
90+
func GetAlertmanagerConfig(c *gin.Context) {
91+
cfg, err := externalexecutor.GetAlertmanagerConfig(c)
92+
if err != nil {
93+
common.SendResponse(c, nil, err)
94+
return
95+
}
96+
common.SendResponse(c, cfg.Address, nil)
97+
}

agent/api/external_route.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"github.com/gin-gonic/gin"
21+
22+
"github.com/oceanbase/obshell/agent/api/common"
23+
"github.com/oceanbase/obshell/agent/constant"
24+
)
25+
26+
func InitExternalRoutes(r *gin.RouterGroup, isLocalRoute bool) {
27+
external := r.Group(constant.URI_EXTERNAL_GROUP)
28+
29+
if !isLocalRoute {
30+
external.Use(common.Verify())
31+
}
32+
33+
external.PUT(constant.URI_PROMETHEUS, SetPrometheusConfig)
34+
external.GET(constant.URI_PROMETHEUS, GetPrometheusConfig)
35+
external.PUT(constant.URI_ALERTMANAGER, SetAlertmanagerConfig)
36+
external.GET(constant.URI_ALERTMANAGER, GetAlertmanagerConfig)
37+
}

0 commit comments

Comments
 (0)