pull/25/head
sunface 7 years ago
parent 1e53668872
commit b93493243a

@ -76,11 +76,11 @@ func timing(f echo.HandlerFunc) echo.HandlerFunc {
// 统计请求指标
apiID := c.Get("api_id").(string)
service := c.Get("service").(string)
label := c.Get("label").(string)
label := c.Get("app").(string)
stats.Req.With(prometheus.Labels{
"api_id": apiID,
"service": service,
"label": label,
"app": label,
}).Observe(float64(time.Now().Sub(ts).Nanoseconds() / 1e6))
err := c.Get("error_msg")

@ -49,7 +49,7 @@ func (f *Filter) BeforeRoute(r *req.Request) Result {
stats.Limits.With(prometheus.Labels{
"api_id": r.Api.APIID,
"service": r.Api.Service,
"label": r.Api.Label,
"app": r.Api.App,
}).Inc()
return Result{http.StatusForbidden, g.ForbiddenC, g.ForbiddenE}
}
@ -76,7 +76,7 @@ func (f *Filter) BeforeCall(r *req.Request) Result {
stats.Limits.With(prometheus.Labels{
"api_id": r.Api.APIID,
"service": r.Api.Service,
"label": r.Api.Label,
"app": r.Api.App,
}).Inc()
return Result{code, g.AccessLimitedC, err.Error()}
}

@ -20,8 +20,8 @@ func (p *ApiServer) loadData() {
now := time.Now()
date := talent.Time2StringSecond(time.Now())
version := talent.Time2Version(now)
g.DB.Exec(fmt.Sprintf("insert into api_release (service,api_id,description,mock_data,route_type,route_addr,create_date) values('admin','admin.test.get.v1','','',1,'http://httpbin.org/get','%s')", date))
g.DB.Exec(fmt.Sprintf("insert into api_define (service,api_id,description,mock_data,route_type,route_addr,revise_version,release_version,create_date) values('admin','admin.test.get.v1','','',1,'http://httpbin.org/get','%s','%s','%s')", version, version, date))
g.DB.Exec(fmt.Sprintf("insert into api_release (service,api_id,description,mock_data,route_type,route_addr,app,create_date) values('admin','admin.test.get.v1','','',1,'http://httpbin.org/get','admin','%s')", date))
g.DB.Exec(fmt.Sprintf("insert into api_define (service,api_id,description,mock_data,route_type,route_addr,revise_version,release_version,app,create_date) values('admin','admin.test.get.v1','','',1,'http://httpbin.org/get','%s','%s','admin','%s')", version, version, date))
lastLoadTime = time.Now()
// 加载所有数据

@ -144,9 +144,9 @@ func (m *Manage) DefineAPI(c echo.Context) error {
pr := g.B64.EncodeToString(talent.String2Bytes(*api.ParamTable))
if action == "create" {
query := fmt.Sprintf(`insert into api_define (api_id,path_type,service,description,route_type,route_addr,route_proto,bw_strategy,retry_strategy,traffic_strategy,mock_data,traffic_on,traffic_api,traffic_ratio,traffic_ips,verify_on,param_rules,cached_time,revise_version,create_date,label)
query := fmt.Sprintf(`insert into api_define (api_id,path_type,service,description,route_type,route_addr,route_proto,bw_strategy,retry_strategy,traffic_strategy,mock_data,traffic_on,traffic_api,traffic_ratio,traffic_ips,verify_on,param_rules,cached_time,revise_version,create_date,app)
values ('%s','%d','%s','%s','%d','%s','%d','%d','%d','%d','%s','%d','%s','%d','%s','%d','%s','%d','%s', '%s','%s')`,
api.APIID, api.PathType, api.Service, *api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, api.BwStrategy, api.RetryStrategy, api.TrafficStrategy, *api.MockData, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, pr, api.CachedTime, talent.Time2Version(now), date, api.Label)
api.APIID, api.PathType, api.Service, *api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, api.BwStrategy, api.RetryStrategy, api.TrafficStrategy, *api.MockData, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, pr, api.CachedTime, talent.Time2Version(now), date, api.App)
_, err := g.DB.Exec(query)
if err != nil {
if strings.Contains(err.Error(), g.DUP_KEY_ERR) {
@ -178,8 +178,8 @@ func (m *Manage) DefineAPI(c echo.Context) error {
}
audit.Log(c.FormValue("username"), api.Service, audit.TypeApi, api.APIID, audit.OpCreate, c.FormValue("api"), "")
} else {
query := fmt.Sprintf("update api_define set description='%s',route_type='%d',route_addr='%s',route_proto='%d',bw_strategy='%d',retry_strategy='%d',traffic_strategy='%d',mock_data='%s',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s',cached_time='%d',label='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, api.BwStrategy, api.RetryStrategy, api.TrafficStrategy, *api.MockData, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, pr, api.CachedTime, api.Label, api.APIID)
query := fmt.Sprintf("update api_define set description='%s',route_type='%d',route_addr='%s',route_proto='%d',bw_strategy='%d',retry_strategy='%d',traffic_strategy='%d',mock_data='%s',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s',cached_time='%d',app='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, api.BwStrategy, api.RetryStrategy, api.TrafficStrategy, *api.MockData, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, pr, api.CachedTime, api.App, api.APIID)
res, err := g.DB.Exec(query)
if err != nil {
g.L.Info("access database error", zap.Error(err), zap.String("query", query))
@ -449,8 +449,8 @@ func (m *Manage) APIRelease(c echo.Context) error {
}
// 更新release
query = fmt.Sprintf("update api_release set description='%s',route_type='%d',route_addr='%s',route_proto='%d',mock_data='%s',retry_strategy='%d',bw_strategy='%d',traffic_strategy='%d',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s', cached_time='%d',status='%d',label='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, *api.MockData, api.RetryStrategy, api.BwStrategy, api.TrafficStrategy, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, *api.ParamTable, api.CachedTime, misc.API_RELEASED, api.Label, api.APIID)
query = fmt.Sprintf("update api_release set description='%s',route_type='%d',route_addr='%s',route_proto='%d',mock_data='%s',retry_strategy='%d',bw_strategy='%d',traffic_strategy='%d',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s', cached_time='%d',status='%d',app='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, *api.MockData, api.RetryStrategy, api.BwStrategy, api.TrafficStrategy, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, *api.ParamTable, api.CachedTime, misc.API_RELEASED, api.App, api.APIID)
_, err = g.DB.Exec(query)
if err != nil {
@ -794,8 +794,8 @@ func (m *Manage) APIBatchRelease(c echo.Context) error {
}
// 更新release
query = fmt.Sprintf("update api_release set description='%s',route_type='%d',route_addr='%s',route_proto='%d',mock_data='%s',retry_strategy='%d',bw_strategy='%d',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s', cached_time='%d',status='%d',label='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, *api.MockData, api.RetryStrategy, api.BwStrategy, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, *api.ParamTable, api.CachedTime, misc.API_RELEASED, api.Label, api.APIID)
query = fmt.Sprintf("update api_release set description='%s',route_type='%d',route_addr='%s',route_proto='%d',mock_data='%s',retry_strategy='%d',bw_strategy='%d',traffic_on='%d',traffic_api='%s',traffic_ratio='%d',traffic_ips='%s',verify_on='%d',param_rules='%s', cached_time='%d',status='%d',app='%s' where api_id='%s'",
*api.Desc, api.RouteType, api.RouteAddr, api.RouteProto, *api.MockData, api.RetryStrategy, api.BwStrategy, api.TrafficOn, api.TrafficAPI, api.TrafficRatio, api.TrafficIPs, api.VerifyOn, *api.ParamTable, api.CachedTime, misc.API_RELEASED, api.App, api.APIID)
_, err = g.DB.Exec(query)
if err != nil {

@ -1,80 +0,0 @@
package manage
import (
"fmt"
"net/http"
"strings"
"github.com/mafanr/g"
"github.com/labstack/echo"
"go.uber.org/zap"
)
func (m *Manage) QueryLabels(c echo.Context) error {
service := c.FormValue("service")
if service == "" {
return c.JSON(http.StatusBadRequest, g.Result{
Status: http.StatusBadRequest,
ErrCode: g.ParamEmptyC,
Message: g.ParamEmptyE,
})
}
query := fmt.Sprintf("select name from labels where service='%s'", service)
rows, err := g.DB.Query(query)
if err != nil {
g.L.Info("access database error", zap.Error(err), zap.String("query", query))
return c.JSON(http.StatusInternalServerError, g.Result{
Status: http.StatusInternalServerError,
ErrCode: g.DatabaseC,
Message: g.DatabaseE,
})
}
labels := make([]string, 0)
for rows.Next() {
var l string
rows.Scan(&l)
labels = append(labels, l)
}
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: labels,
})
}
func (m *Manage) CreateLabel(c echo.Context) error {
service := c.FormValue("service")
name := c.FormValue("name")
if service == "" || name == "" {
return c.JSON(http.StatusBadRequest, g.Result{
Status: http.StatusBadRequest,
ErrCode: g.ParamEmptyC,
Message: g.ParamEmptyE,
})
}
query := fmt.Sprintf("insert into labels (service,name) values ('%s','%s')", service, name)
_, err := g.DB.Exec(query)
if err != nil {
if strings.Contains(err.Error(), g.DUP_KEY_ERR) {
return c.JSON(http.StatusConflict, g.Result{
Status: http.StatusConflict,
ErrCode: g.AlreadyExistC,
Message: g.AlreadyExistE,
})
}
g.L.Info("access database error", zap.Error(err), zap.String("query", query))
return c.JSON(http.StatusInternalServerError, g.Result{
Status: http.StatusInternalServerError,
ErrCode: g.DatabaseC,
Message: g.DatabaseE,
})
}
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
})
}

@ -53,10 +53,6 @@ func (m *Manage) Start() {
e.POST("/manage/auditLog/count", audit.Count, auth)
e.POST("/manage/auditLog/load", audit.Load, auth)
// 标签分组
e.POST("/manage/labels/query", m.QueryLabels, auth)
e.POST("/manage/labels/create", m.CreateLabel, auth)
e.Logger.Fatal(e.Start(":" + misc.Conf.Manage.Port))
}

@ -42,13 +42,13 @@ func (router *router) route(c echo.Context) error {
if err != nil {
c.Set("api_id", "error_api_id")
c.Set("service", "error_service")
c.Set("label", "error_label")
c.Set("app", "error_app")
c.Set("error_msg", err)
return c.JSON(http.StatusBadRequest, g.Result{r.Rid, http.StatusBadRequest, g.ParamInvalidC, err.Error(), nil})
}
c.Set("api_id", r.Api.APIID)
c.Set("service", r.Api.Service)
c.Set("label", r.Api.Label)
c.Set("app", r.Api.App)
g.Debug(r.DebugOn, misc.Conf.Common.LogLevel, "request content", zap.Int64("rid", r.Rid), zap.String("req", r.String()))
@ -148,7 +148,7 @@ func (rt *router) sync(r *req.Request) (int, []byte, error) {
"code": strconv.Itoa(resp.StatusCode()),
"api_id": r.Api.APIID,
"service": r.Api.Service,
"label": r.Api.Label,
"app": r.Api.App,
}).Inc()
break
}
@ -156,7 +156,7 @@ func (rt *router) sync(r *req.Request) (int, []byte, error) {
stats.Errors.With(prometheus.Labels{
"api_id": r.Api.APIID,
"service": r.Api.Service,
"label": r.Api.Label,
"app": r.Api.App,
}).Inc()
// 发生错误,进行重试
if retrys >= r.RetryStrategy.RetryTimes {

@ -28,8 +28,7 @@ CREATE TABLE IF NOT EXISTS `api_release` (
`cached_time` int(11) DEFAULT '0' COMMENT '为0表示不开启,其它值代表缓存的时间',
`label` varchar(255) DEFAUlT '' COMMENT '标签分组',
`app` varchar(255) DEFAUlT '' COMMENT '所属应用',
`status` int(11) DEFAULT '1' COMMENT '发布状态0代表未发布,1代表已发布',
`create_date` datetime DEFAULT NULL COMMENT '创建时间',
@ -67,7 +66,7 @@ CREATE TABLE IF NOT EXISTS `api_define` (
`cached_time` int(11) DEFAULT '0' COMMENT '为0表示不开启,其它值代表缓存的时间',
`label` varchar(255) DEFAUlT '' COMMENT '标签分组',
`app` varchar(255) DEFAUlT '' COMMENT '所属应用',
`revise_version` varchar(255) NOT NULL COMMENT 'api内容最新版本号',
`release_version` varchar(255) DEFAULT '' COMMENT '当前已发布的版本号',

@ -10,24 +10,24 @@ import (
var (
Req = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "req_distribution",
Name: "juz_req_stats",
Help: "Request stats",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, []string{"api_id", "service", "label"})
}, []string{"api_id", "service", "app"})
Limits = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "req_limits",
Name: "juz_req_limits",
Help: "Request blocked",
}, []string{"api_id", "service", "label"})
}, []string{"api_id", "service", "app"})
Errors = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "req_errors",
Name: "juz_req_errors",
Help: "Request error",
}, []string{"api_id", "service", "label"})
}, []string{"api_id", "service", "app"})
Codes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "req_codes",
Name: "juz_req_codes",
Help: "Reqeust http code",
},
[]string{"code", "api_id", "service", "label"},
[]string{"code", "api_id", "service", "app"},
)
)

@ -41,8 +41,8 @@ type API struct {
// 缓存
CachedTime int `db:"cached_time" json:"cached_time"`
// 标签分组
Label string `db:"label" json:"label"`
// 所属应用
App string `db:"app" json:"app"`
// API修订的版本号
ReviseVersion string `db:"revise_version" json:"revise_version"`

Loading…
Cancel
Save