From 0730e91292f14c88f0e44b36eeba4f2db18da003 Mon Sep 17 00:00:00 2001 From: georgehao Date: Mon, 11 Sep 2023 19:22:19 +0800 Subject: [PATCH] feat(observability):add ready&health check api for k8s (#938) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: georgehao Co-authored-by: Péter Garamvölgyi --- .../internal/controller/batch_controller.go | 6 +-- .../internal/controller/controller.go | 9 +++- .../internal/controller/health_check.go | 30 +++++++++++ .../internal/controller/history_controller.go | 16 +++--- .../internal/controller/ready.go | 21 ++++++++ bridge-history-api/internal/route/route.go | 2 + .../internal/types/history_types.go | 27 ++++++++++ bridge-history-api/utils/database.go | 20 +++++-- common/database/db.go | 24 ++++++--- common/database/db_test.go | 27 ++++++++++ .../ginmetrics/bloom.go | 0 .../ginmetrics/metric.go | 0 .../ginmetrics/middleware.go | 0 .../ginmetrics/types.go | 0 .../{metrics => observability}/middleware.go | 4 +- common/observability/probes.go | 35 ++++++++++++ common/{metrics => observability}/server.go | 10 ++-- common/types/errno.go | 9 ++++ common/types/response.go | 53 +++++++++++++++++++ common/version/version.go | 2 +- coordinator/cmd/app/app.go | 4 +- .../internal/controller/api/controller.go | 3 -- .../internal/controller/api/get_task.go | 10 ++-- .../internal/controller/api/health_check.go | 23 -------- .../internal/controller/api/submit_proof.go | 10 ++-- coordinator/internal/middleware/jwt.go | 4 +- coordinator/internal/route/route.go | 6 +-- coordinator/internal/types/response.go | 29 ---------- coordinator/test/mock_prover.go | 14 ++--- go.work | 2 +- go.work.sum | 1 + prover-stats-api/cmd/app/app.go | 7 ++- prover-stats-api/go.mod | 9 +++- prover-stats-api/go.sum | 21 +++++++- prover-stats-api/internal/controller/auth.go | 4 +- .../internal/controller/prover_task.go | 18 +++---- prover-stats-api/internal/middleware/jwt.go | 4 +- prover-stats-api/internal/route/route.go | 7 ++- prover-stats-api/internal/types/errno.go | 12 ----- prover-stats-api/internal/types/response.go | 28 ---------- prover-stats-api/test/prover_task_test.go | 9 ++-- rollup/cmd/event_watcher/app/app.go | 4 +- rollup/cmd/gas_oracle/app/app.go | 4 +- rollup/cmd/rollup_relayer/app/app.go | 4 +- rollup/conf/config.json | 8 +-- 45 files changed, 361 insertions(+), 179 deletions(-) create mode 100644 bridge-history-api/internal/controller/health_check.go create mode 100644 bridge-history-api/internal/controller/ready.go rename common/{metrics => observability}/ginmetrics/bloom.go (100%) rename common/{metrics => observability}/ginmetrics/metric.go (100%) rename common/{metrics => observability}/ginmetrics/middleware.go (100%) rename common/{metrics => observability}/ginmetrics/types.go (100%) rename common/{metrics => observability}/middleware.go (85%) create mode 100644 common/observability/probes.go rename common/{metrics => observability}/server.go (83%) create mode 100644 common/types/response.go delete mode 100644 coordinator/internal/controller/api/health_check.go delete mode 100644 coordinator/internal/types/response.go delete mode 100644 prover-stats-api/internal/types/errno.go delete mode 100644 prover-stats-api/internal/types/response.go diff --git a/bridge-history-api/internal/controller/batch_controller.go b/bridge-history-api/internal/controller/batch_controller.go index be0c90f727..03620f13f9 100644 --- a/bridge-history-api/internal/controller/batch_controller.go +++ b/bridge-history-api/internal/controller/batch_controller.go @@ -24,14 +24,14 @@ func NewBatchController(db *gorm.DB) *BatchController { func (b *BatchController) GetWithdrawRootByBatchIndex(ctx *gin.Context) { var req types.QueryByBatchIndexRequest if err := ctx.ShouldBind(&req); err != nil { - types.RenderJSON(ctx, types.ErrParameterInvalidNo, err, nil) + types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } result, err := b.batchLogic.GetWithdrawRootByBatchIndex(ctx, req.BatchIndex) if err != nil { - types.RenderJSON(ctx, types.ErrGetWithdrawRootByBatchIndexFailure, err, nil) + types.RenderFailure(ctx, types.ErrGetWithdrawRootByBatchIndexFailure, err) return } - types.RenderJSON(ctx, types.Success, nil, result) + types.RenderSuccess(ctx, result) } diff --git a/bridge-history-api/internal/controller/controller.go b/bridge-history-api/internal/controller/controller.go index 1158595492..46eed1442b 100644 --- a/bridge-history-api/internal/controller/controller.go +++ b/bridge-history-api/internal/controller/controller.go @@ -10,7 +10,12 @@ var ( // HistoryCtrler is controller instance HistoryCtrler *HistoryController // BatchCtrler is controller instance - BatchCtrler *BatchController + BatchCtrler *BatchController + // HealthCheck the health check controller + HealthCheck *HealthCheckController + // Ready the ready controller + Ready *ReadyController + initControllerOnce sync.Once ) @@ -19,5 +24,7 @@ func InitController(db *gorm.DB) { initControllerOnce.Do(func() { HistoryCtrler = NewHistoryController(db) BatchCtrler = NewBatchController(db) + HealthCheck = NewHealthCheckController(db) + Ready = NewReadyController() }) } diff --git a/bridge-history-api/internal/controller/health_check.go b/bridge-history-api/internal/controller/health_check.go new file mode 100644 index 0000000000..fa00dd56cc --- /dev/null +++ b/bridge-history-api/internal/controller/health_check.go @@ -0,0 +1,30 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "bridge-history-api/internal/types" + "bridge-history-api/utils" +) + +// HealthCheckController is health check API +type HealthCheckController struct { + db *gorm.DB +} + +// NewHealthCheckController returns an HealthCheckController instance +func NewHealthCheckController(db *gorm.DB) *HealthCheckController { + return &HealthCheckController{ + db: db, + } +} + +// HealthCheck the api controller for coordinator health check +func (a *HealthCheckController) HealthCheck(c *gin.Context) { + if _, err := utils.Ping(a.db); err != nil { + types.RenderFatal(c, err) + return + } + types.RenderSuccess(c, nil) +} diff --git a/bridge-history-api/internal/controller/history_controller.go b/bridge-history-api/internal/controller/history_controller.go index 8d799c7fa0..02627962df 100644 --- a/bridge-history-api/internal/controller/history_controller.go +++ b/bridge-history-api/internal/controller/history_controller.go @@ -25,18 +25,18 @@ func NewHistoryController(db *gorm.DB) *HistoryController { func (c *HistoryController) GetAllClaimableTxsByAddr(ctx *gin.Context) { var req types.QueryByAddressRequest if err := ctx.ShouldBind(&req); err != nil { - types.RenderJSON(ctx, types.ErrParameterInvalidNo, err, nil) + types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } offset := (req.Page - 1) * req.PageSize limit := req.PageSize txs, total, err := c.historyLogic.GetClaimableTxsByAddress(ctx, common.HexToAddress(req.Address), offset, limit) if err != nil { - types.RenderJSON(ctx, types.ErrGetClaimablesFailure, err, nil) + types.RenderFailure(ctx, types.ErrGetClaimablesFailure, err) return } - types.RenderJSON(ctx, types.Success, nil, &types.ResultData{Result: txs, Total: total}) + types.RenderSuccess(ctx, &types.ResultData{Result: txs, Total: total}) } // GetAllTxsByAddr defines the http get method behavior @@ -50,23 +50,23 @@ func (c *HistoryController) GetAllTxsByAddr(ctx *gin.Context) { limit := req.PageSize message, total, err := c.historyLogic.GetTxsByAddress(ctx, common.HexToAddress(req.Address), offset, limit) if err != nil { - types.RenderJSON(ctx, types.ErrGetTxsByAddrFailure, err, nil) + types.RenderFailure(ctx, types.ErrGetTxsByAddrFailure, err) return } - types.RenderJSON(ctx, types.Success, nil, &types.ResultData{Result: message, Total: total}) + types.RenderSuccess(ctx, &types.ResultData{Result: message, Total: total}) } // PostQueryTxsByHash defines the http post method behavior func (c *HistoryController) PostQueryTxsByHash(ctx *gin.Context) { var req types.QueryByHashRequest if err := ctx.ShouldBindJSON(&req); err != nil { - types.RenderJSON(ctx, types.ErrParameterInvalidNo, err, nil) + types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } result, err := c.historyLogic.GetTxsByHashes(ctx, req.Txs) if err != nil { - types.RenderJSON(ctx, types.ErrGetTxsByHashFailure, err, nil) + types.RenderFailure(ctx, types.ErrGetTxsByHashFailure, err) return } - types.RenderJSON(ctx, types.Success, nil, &types.ResultData{Result: result, Total: 0}) + types.RenderSuccess(ctx, &types.ResultData{Result: result, Total: 0}) } diff --git a/bridge-history-api/internal/controller/ready.go b/bridge-history-api/internal/controller/ready.go new file mode 100644 index 0000000000..7e4c045d8c --- /dev/null +++ b/bridge-history-api/internal/controller/ready.go @@ -0,0 +1,21 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + + "bridge-history-api/internal/types" +) + +// ReadyController ready API +type ReadyController struct { +} + +// NewReadyController returns an ReadyController instance +func NewReadyController() *ReadyController { + return &ReadyController{} +} + +// Ready the api controller for coordinator ready +func (r *ReadyController) Ready(c *gin.Context) { + types.RenderSuccess(c, nil) +} diff --git a/bridge-history-api/internal/route/route.go b/bridge-history-api/internal/route/route.go index 7bdc3ed06b..cadf0328e0 100644 --- a/bridge-history-api/internal/route/route.go +++ b/bridge-history-api/internal/route/route.go @@ -25,4 +25,6 @@ func Route(router *gin.Engine, conf *config.Config) { r.POST("/txsbyhashes", controller.HistoryCtrler.PostQueryTxsByHash) r.GET("/claimable", controller.HistoryCtrler.GetAllClaimableTxsByAddr) r.GET("/withdraw_root", controller.BatchCtrler.GetWithdrawRootByBatchIndex) + r.GET("/health", controller.HealthCheck.HealthCheck) + r.GET("/ready", controller.Ready.Ready) } diff --git a/bridge-history-api/internal/types/history_types.go b/bridge-history-api/internal/types/history_types.go index ff02500006..2f690dd123 100644 --- a/bridge-history-api/internal/types/history_types.go +++ b/bridge-history-api/internal/types/history_types.go @@ -10,6 +10,8 @@ import ( const ( // Success shows OK. Success = 0 + // InternalServerError shows a fatal error in the server + InternalServerError = 500 // ErrParameterInvalidNo is invalid params ErrParameterInvalidNo = 40001 // ErrGetClaimablesFailure is getting all claimables txs error @@ -103,3 +105,28 @@ func RenderJSON(ctx *gin.Context, errCode int, err error, data interface{}) { } ctx.JSON(http.StatusOK, renderData) } + +// RenderSuccess renders success response with json +func RenderSuccess(ctx *gin.Context, data interface{}) { + RenderJSON(ctx, Success, nil, data) +} + +// RenderFailure renders failure response with json +func RenderFailure(ctx *gin.Context, errCode int, err error) { + RenderJSON(ctx, errCode, err, nil) +} + +// RenderFatal renders fatal response with json +func RenderFatal(ctx *gin.Context, err error) { + var errMsg string + if err != nil { + errMsg = err.Error() + } + renderData := Response{ + ErrCode: InternalServerError, + ErrMsg: errMsg, + Data: nil, + } + ctx.Set("errcode", InternalServerError) + ctx.JSON(http.StatusInternalServerError, renderData) +} diff --git a/bridge-history-api/utils/database.go b/bridge-history-api/utils/database.go index c85aa8b657..916372e042 100644 --- a/bridge-history-api/utils/database.go +++ b/bridge-history-api/utils/database.go @@ -2,6 +2,7 @@ package utils import ( "context" + "database/sql" "fmt" "time" @@ -67,18 +68,29 @@ func InitDB(config *config.DBConfig) (*gorm.DB, error) { if err != nil { return nil, err } - sqlDB, err := db.DB() - if err != nil { - return nil, err + + sqlDB, pingErr := Ping(db) + if pingErr != nil { + return nil, pingErr } sqlDB.SetMaxOpenConns(config.MaxOpenNum) sqlDB.SetMaxIdleConns(config.MaxIdleNum) + return db, nil +} + +// Ping check db status +func Ping(db *gorm.DB) (*sql.DB, error) { + sqlDB, err := db.DB() + if err != nil { + return nil, err + } + if err = sqlDB.Ping(); err != nil { return nil, err } - return db, nil + return sqlDB, nil } // CloseDB close the db handler. notice the db handler only can close when then program exit. diff --git a/common/database/db.go b/common/database/db.go index f7eac49f4c..8d5a54842a 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -2,6 +2,7 @@ package database import ( "context" + "database/sql" "fmt" "time" @@ -63,17 +64,15 @@ func InitDB(config *Config) (*gorm.DB, error) { if err != nil { return nil, err } - sqlDB, err := db.DB() - if err != nil { - return nil, err + + sqlDB, pingErr := Ping(db) + if pingErr != nil { + return nil, pingErr } sqlDB.SetMaxOpenConns(config.MaxOpenNum) sqlDB.SetMaxIdleConns(config.MaxIdleNum) - if err = sqlDB.Ping(); err != nil { - return nil, err - } return db, nil } @@ -88,3 +87,16 @@ func CloseDB(db *gorm.DB) error { } return nil } + +// Ping check db status +func Ping(db *gorm.DB) (*sql.DB, error) { + sqlDB, err := db.DB() + if err != nil { + return nil, err + } + + if err = sqlDB.Ping(); err != nil { + return nil, err + } + return sqlDB, nil +} diff --git a/common/database/db_test.go b/common/database/db_test.go index 5440effc0e..df6bcd6496 100644 --- a/common/database/db_test.go +++ b/common/database/db_test.go @@ -11,6 +11,10 @@ import ( "github.com/mattn/go-colorable" "github.com/mattn/go-isatty" "github.com/scroll-tech/go-ethereum/log" + "github.com/stretchr/testify/assert" + + "scroll-tech/common/docker" + "scroll-tech/common/version" ) func TestGormLogger(t *testing.T) { @@ -33,3 +37,26 @@ func TestGormLogger(t *testing.T) { gl.Info(context.Background(), "test %s warn:%v", "testInfo", errors.New("test info")) gl.Trace(context.Background(), time.Now(), func() (string, int64) { return "test trace", 1 }, nil) } + +func TestDB(t *testing.T) { + version.Version = "v4.1.98-aaa-bbb-ccc" + base := docker.NewDockerApp() + base.RunDBImage(t) + + dbCfg := &Config{ + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + } + + var err error + db, err := InitDB(dbCfg) + assert.NoError(t, err) + + sqlDB, err := Ping(db) + assert.NoError(t, err) + assert.NotNil(t, sqlDB) + + assert.NoError(t, CloseDB(db)) +} diff --git a/common/metrics/ginmetrics/bloom.go b/common/observability/ginmetrics/bloom.go similarity index 100% rename from common/metrics/ginmetrics/bloom.go rename to common/observability/ginmetrics/bloom.go diff --git a/common/metrics/ginmetrics/metric.go b/common/observability/ginmetrics/metric.go similarity index 100% rename from common/metrics/ginmetrics/metric.go rename to common/observability/ginmetrics/metric.go diff --git a/common/metrics/ginmetrics/middleware.go b/common/observability/ginmetrics/middleware.go similarity index 100% rename from common/metrics/ginmetrics/middleware.go rename to common/observability/ginmetrics/middleware.go diff --git a/common/metrics/ginmetrics/types.go b/common/observability/ginmetrics/types.go similarity index 100% rename from common/metrics/ginmetrics/types.go rename to common/observability/ginmetrics/types.go diff --git a/common/metrics/middleware.go b/common/observability/middleware.go similarity index 85% rename from common/metrics/middleware.go rename to common/observability/middleware.go index dadd4884d2..6ab482d2c1 100644 --- a/common/metrics/middleware.go +++ b/common/observability/middleware.go @@ -1,10 +1,10 @@ -package metrics +package observability import ( "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" - "scroll-tech/common/metrics/ginmetrics" + "scroll-tech/common/observability/ginmetrics" ) // Use register the gin metric diff --git a/common/observability/probes.go b/common/observability/probes.go new file mode 100644 index 0000000000..bbaabe1a9b --- /dev/null +++ b/common/observability/probes.go @@ -0,0 +1,35 @@ +package observability + +import ( + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "scroll-tech/common/database" + "scroll-tech/common/types" +) + +// ProbesController probe check controller +type ProbesController struct { + db *gorm.DB +} + +// NewProbesController returns an ProbesController instance +func NewProbesController(db *gorm.DB) *ProbesController { + return &ProbesController{ + db: db, + } +} + +// HealthCheck the api controller for health check +func (a *ProbesController) HealthCheck(c *gin.Context) { + if _, err := database.Ping(a.db); err != nil { + types.RenderFatal(c, err) + return + } + types.RenderSuccess(c, nil) +} + +// Ready the api controller for ready check +func (a *ProbesController) Ready(c *gin.Context) { + types.RenderSuccess(c, nil) +} diff --git a/common/metrics/server.go b/common/observability/server.go similarity index 83% rename from common/metrics/server.go rename to common/observability/server.go index 3d43ce8d46..735b04a206 100644 --- a/common/metrics/server.go +++ b/common/observability/server.go @@ -1,4 +1,4 @@ -package metrics +package observability import ( "errors" @@ -11,17 +11,17 @@ import ( "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/scroll-tech/go-ethereum/log" "github.com/urfave/cli/v2" + "gorm.io/gorm" "scroll-tech/common/utils" ) // Server starts the metrics server on the given address, will be closed when the given // context is canceled. -func Server(c *cli.Context, reg *prometheus.Registry) { +func Server(c *cli.Context, db *gorm.DB) { if !c.Bool(utils.MetricsEnabled.Name) { return } @@ -33,6 +33,10 @@ func Server(c *cli.Context, reg *prometheus.Registry) { promhttp.Handler().ServeHTTP(context.Writer, context.Request) }) + probeController := NewProbesController(db) + r.GET("/health", probeController.HealthCheck) + r.GET("/ready", probeController.Ready) + address := fmt.Sprintf(":%s", c.String(utils.MetricsPort.Name)) server := &http.Server{ Addr: address, diff --git a/common/types/errno.go b/common/types/errno.go index be807e00f8..a1981acaa0 100644 --- a/common/types/errno.go +++ b/common/types/errno.go @@ -3,12 +3,21 @@ package types const ( // Success shows OK. Success = 0 + // InternalServerError shows a fatal error in the server + InternalServerError = 500 // ErrJWTCommonErr jwt common error ErrJWTCommonErr = 50000 // ErrJWTTokenExpired jwt token expired ErrJWTTokenExpired = 50001 + // ErrProverStatsAPIParameterInvalidNo is invalid params + ErrProverStatsAPIParameterInvalidNo = 10001 + // ErrProverStatsAPIProverTaskFailure is getting prover task error + ErrProverStatsAPIProverTaskFailure = 10002 + // ErrProverStatsAPIProverTotalRewardFailure is getting total rewards error + ErrProverStatsAPIProverTotalRewardFailure = 10003 + // ErrCoordinatorParameterInvalidNo is invalid params ErrCoordinatorParameterInvalidNo = 20001 // ErrCoordinatorGetTaskFailure is getting prover task error diff --git a/common/types/response.go b/common/types/response.go new file mode 100644 index 0000000000..d616aa8013 --- /dev/null +++ b/common/types/response.go @@ -0,0 +1,53 @@ +package types + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// Response the response schema +type Response struct { + ErrCode int `json:"errcode"` + ErrMsg string `json:"errmsg"` + Data interface{} `json:"data"` +} + +// RenderJSON renders response with json +func RenderJSON(ctx *gin.Context, errCode int, err error, data interface{}) { + var errMsg string + if err != nil { + errMsg = err.Error() + } + renderData := Response{ + ErrCode: errCode, + ErrMsg: errMsg, + Data: data, + } + ctx.JSON(http.StatusOK, renderData) +} + +// RenderSuccess renders success response with json +func RenderSuccess(ctx *gin.Context, data interface{}) { + RenderJSON(ctx, Success, nil, data) +} + +// RenderFailure renders failure response with json +func RenderFailure(ctx *gin.Context, errCode int, err error) { + RenderJSON(ctx, errCode, err, nil) +} + +// RenderFatal renders fatal response with json +func RenderFatal(ctx *gin.Context, err error) { + var errMsg string + if err != nil { + errMsg = err.Error() + } + renderData := Response{ + ErrCode: InternalServerError, + ErrMsg: errMsg, + Data: nil, + } + ctx.Set("errcode", InternalServerError) + ctx.JSON(http.StatusInternalServerError, renderData) +} diff --git a/common/version/version.go b/common/version/version.go index 15f1473fd5..d2d052f091 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.3.3" +var tag = "v4.3.4" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/coordinator/cmd/app/app.go b/coordinator/cmd/app/app.go index 4215f6a8ff..16d7b385d2 100644 --- a/coordinator/cmd/app/app.go +++ b/coordinator/cmd/app/app.go @@ -16,7 +16,7 @@ import ( "gorm.io/gorm" "scroll-tech/common/database" - "scroll-tech/common/metrics" + "scroll-tech/common/observability" "scroll-tech/common/utils" "scroll-tech/common/version" @@ -58,7 +58,7 @@ func action(ctx *cli.Context) error { } registry := prometheus.DefaultRegisterer - metrics.Server(ctx, registry.(*prometheus.Registry)) + observability.Server(ctx, db) proofCollector := cron.NewCollector(subCtx, db, cfg, registry) defer func() { diff --git a/coordinator/internal/controller/api/controller.go b/coordinator/internal/controller/api/controller.go index 1f518a77b0..0ad4ff2d05 100644 --- a/coordinator/internal/controller/api/controller.go +++ b/coordinator/internal/controller/api/controller.go @@ -15,8 +15,6 @@ var ( GetTask *GetTaskController // SubmitProof the submit proof controller SubmitProof *SubmitProofController - // HealthCheck the health check controller - HealthCheck *HealthCheckController // Auth the auth controller Auth *AuthController @@ -32,7 +30,6 @@ func InitController(cfg *config.Config, db *gorm.DB, reg prometheus.Registerer) } Auth = NewAuthController(db) - HealthCheck = NewHealthCheckController() GetTask = NewGetTaskController(cfg, db, vf, reg) SubmitProof = NewSubmitProofController(cfg, db, vf, reg) }) diff --git a/coordinator/internal/controller/api/get_task.go b/coordinator/internal/controller/api/get_task.go index c6beee4379..91d1008982 100644 --- a/coordinator/internal/controller/api/get_task.go +++ b/coordinator/internal/controller/api/get_task.go @@ -42,7 +42,7 @@ func (ptc *GetTaskController) GetTasks(ctx *gin.Context) { var getTaskParameter coordinatorType.GetTaskParameter if err := ctx.ShouldBind(&getTaskParameter); err != nil { nerr := fmt.Errorf("prover task parameter invalid, err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorParameterInvalidNo, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorParameterInvalidNo, nerr) return } @@ -50,24 +50,24 @@ func (ptc *GetTaskController) GetTasks(ctx *gin.Context) { proverTask, isExist := ptc.proverTasks[proofType] if !isExist { nerr := fmt.Errorf("parameter wrong proof type:%v", proofType) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorParameterInvalidNo, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorParameterInvalidNo, nerr) return } result, err := proverTask.Assign(ctx, &getTaskParameter) if err != nil { nerr := fmt.Errorf("return prover task err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorGetTaskFailure, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorGetTaskFailure, nerr) return } if result == nil { nerr := fmt.Errorf("get empty prover task") - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorEmptyProofData, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorEmptyProofData, nerr) return } - coordinatorType.RenderJSON(ctx, types.Success, nil, result) + types.RenderSuccess(ctx, result) } func (ptc *GetTaskController) proofType(para *coordinatorType.GetTaskParameter) message.ProofType { diff --git a/coordinator/internal/controller/api/health_check.go b/coordinator/internal/controller/api/health_check.go deleted file mode 100644 index a9cb0c83ca..0000000000 --- a/coordinator/internal/controller/api/health_check.go +++ /dev/null @@ -1,23 +0,0 @@ -package api - -import ( - "github.com/gin-gonic/gin" - - ctypes "scroll-tech/common/types" - - "scroll-tech/coordinator/internal/types" -) - -// HealthCheckController is health check API -type HealthCheckController struct { -} - -// NewHealthCheckController returns an HealthCheckController instance -func NewHealthCheckController() *HealthCheckController { - return &HealthCheckController{} -} - -// HealthCheck the api controller for coordinator health check -func (a *HealthCheckController) HealthCheck(c *gin.Context) { - types.RenderJSON(c, ctypes.Success, nil, nil) -} diff --git a/coordinator/internal/controller/api/submit_proof.go b/coordinator/internal/controller/api/submit_proof.go index 815b07f1b0..d4ac0c7091 100644 --- a/coordinator/internal/controller/api/submit_proof.go +++ b/coordinator/internal/controller/api/submit_proof.go @@ -34,7 +34,7 @@ func (spc *SubmitProofController) SubmitProof(ctx *gin.Context) { var spp coordinatorType.SubmitProofParameter if err := ctx.ShouldBind(&spp); err != nil { nerr := fmt.Errorf("parameter invalid, err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorParameterInvalidNo, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorParameterInvalidNo, nerr) return } @@ -52,7 +52,7 @@ func (spc *SubmitProofController) SubmitProof(ctx *gin.Context) { var tmpChunkProof message.ChunkProof if err := json.Unmarshal([]byte(spp.Proof), &tmpChunkProof); err != nil { nerr := fmt.Errorf("unmarshal parameter chunk proof invalid, err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorParameterInvalidNo, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorParameterInvalidNo, nerr) return } proofMsg.ChunkProof = &tmpChunkProof @@ -60,7 +60,7 @@ func (spc *SubmitProofController) SubmitProof(ctx *gin.Context) { var tmpBatchProof message.BatchProof if err := json.Unmarshal([]byte(spp.Proof), &tmpBatchProof); err != nil { nerr := fmt.Errorf("unmarshal parameter batch proof invalid, err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorParameterInvalidNo, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorParameterInvalidNo, nerr) return } proofMsg.BatchProof = &tmpBatchProof @@ -69,8 +69,8 @@ func (spc *SubmitProofController) SubmitProof(ctx *gin.Context) { if err := spc.submitProofReceiverLogic.HandleZkProof(ctx, &proofMsg, spp); err != nil { nerr := fmt.Errorf("handle zk proof failure, err:%w", err) - coordinatorType.RenderJSON(ctx, types.ErrCoordinatorHandleZkProofFailure, nerr, nil) + types.RenderFailure(ctx, types.ErrCoordinatorHandleZkProofFailure, nerr) return } - coordinatorType.RenderJSON(ctx, types.Success, nil, nil) + types.RenderSuccess(ctx, nil) } diff --git a/coordinator/internal/middleware/jwt.go b/coordinator/internal/middleware/jwt.go index 8ceca2e3bd..eb2187d27f 100644 --- a/coordinator/internal/middleware/jwt.go +++ b/coordinator/internal/middleware/jwt.go @@ -22,7 +22,7 @@ func unauthorized(c *gin.Context, _ int, message string) { } else { errCode = types.ErrJWTCommonErr } - coordinatorType.RenderJSON(c, errCode, err, nil) + types.RenderFailure(c, errCode, err) } func loginResponse(c *gin.Context, code int, message string, time time.Time) { @@ -30,5 +30,5 @@ func loginResponse(c *gin.Context, code int, message string, time time.Time) { Time: time, Token: message, } - coordinatorType.RenderJSON(c, types.Success, nil, resp) + types.RenderSuccess(c, resp) } diff --git a/coordinator/internal/route/route.go b/coordinator/internal/route/route.go index ea8841f03b..9e9eef076e 100644 --- a/coordinator/internal/route/route.go +++ b/coordinator/internal/route/route.go @@ -4,7 +4,7 @@ import ( "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" - "scroll-tech/common/metrics" + "scroll-tech/common/observability" "scroll-tech/coordinator/internal/config" "scroll-tech/coordinator/internal/controller/api" @@ -15,7 +15,7 @@ import ( func Route(router *gin.Engine, cfg *config.Config, reg prometheus.Registerer) { router.Use(gin.Recovery()) - metrics.Use(router, "coordinator", reg) + observability.Use(router, "coordinator", reg) r := router.Group("coordinator") @@ -25,8 +25,6 @@ func Route(router *gin.Engine, cfg *config.Config, reg prometheus.Registerer) { func v1(router *gin.RouterGroup, conf *config.Config) { r := router.Group("/v1") - r.GET("/health", api.HealthCheck.HealthCheck) - challengeMiddleware := middleware.ChallengeMiddleware(conf) r.GET("/challenge", challengeMiddleware.LoginHandler) diff --git a/coordinator/internal/types/response.go b/coordinator/internal/types/response.go deleted file mode 100644 index d1bb8cb7ae..0000000000 --- a/coordinator/internal/types/response.go +++ /dev/null @@ -1,29 +0,0 @@ -package types - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -// Response the response schema -type Response struct { - ErrCode int `json:"errcode"` - ErrMsg string `json:"errmsg"` - Data interface{} `json:"data"` -} - -// RenderJSON renders response with json -func RenderJSON(ctx *gin.Context, errCode int, err error, data interface{}) { - var errMsg string - if err != nil { - errMsg = err.Error() - } - renderData := Response{ - ErrCode: errCode, - ErrMsg: errMsg, - Data: data, - } - ctx.Set("errcode", errCode) - ctx.JSON(http.StatusOK, renderData) -} diff --git a/coordinator/test/mock_prover.go b/coordinator/test/mock_prover.go index 9cb1a1787a..804c163973 100644 --- a/coordinator/test/mock_prover.go +++ b/coordinator/test/mock_prover.go @@ -55,7 +55,7 @@ func (r *mockProver) connectToCoordinator(t *testing.T) string { } func (r *mockProver) challenge(t *testing.T) string { - var result types.Response + var result ctypes.Response client := resty.New() resp, err := client.R(). SetResult(&result). @@ -87,7 +87,7 @@ func (r *mockProver) login(t *testing.T, challengeString string) string { body := fmt.Sprintf("{\"message\":{\"challenge\":\"%s\",\"prover_name\":\"%s\", \"prover_version\":\"%s\"},\"signature\":\"%s\"}", authMsg.Identity.Challenge, authMsg.Identity.ProverName, authMsg.Identity.ProverVersion, authMsg.Signature) - var result types.Response + var result ctypes.Response client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/json"). @@ -110,11 +110,11 @@ func (r *mockProver) login(t *testing.T, challengeString string) string { } func (r *mockProver) healthCheckSuccess(t *testing.T) bool { - var result types.Response + var result ctypes.Response client := resty.New() resp, err := client.R(). SetResult(&result). - Get("http://" + r.coordinatorURL + "/coordinator/v1/health") + Get("http://" + r.coordinatorURL + "/coordinator/v1/challenge") assert.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode()) assert.Equal(t, ctypes.Success, result.ErrCode) @@ -122,11 +122,11 @@ func (r *mockProver) healthCheckSuccess(t *testing.T) bool { } func (r *mockProver) healthCheckFailure(t *testing.T) bool { - var result types.Response + var result ctypes.Response client := resty.New() resp, err := client.R(). SetResult(&result). - Get("http://" + r.coordinatorURL + "/coordinator/v1/health") + Get("http://" + r.coordinatorURL + "/coordinator/v1/challenge") assert.Error(t, err) assert.Equal(t, 0, resp.StatusCode()) assert.Equal(t, 0, result.ErrCode) @@ -212,7 +212,7 @@ func (r *mockProver) submitProof(t *testing.T, proverTaskSchema *types.GetTaskSc assert.NoError(t, err) assert.NotNil(t, submitProofData) - var result types.Response + var result ctypes.Response client := resty.New() resp, err := client.R(). SetHeader("Content-Type", "application/json"). diff --git a/go.work b/go.work index 020c6c5f39..f1875b8b3a 100644 --- a/go.work +++ b/go.work @@ -1,12 +1,12 @@ go 1.19 use ( - ./rollup ./bridge-history-api ./common ./coordinator ./database ./prover ./prover-stats-api + ./rollup ./tests/integration-test ) diff --git a/go.work.sum b/go.work.sum index 2b4fb7fb8f..147f8822a8 100644 --- a/go.work.sum +++ b/go.work.sum @@ -363,6 +363,7 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64= diff --git a/prover-stats-api/cmd/app/app.go b/prover-stats-api/cmd/app/app.go index 58a3bd68f4..beb82514ac 100644 --- a/prover-stats-api/cmd/app/app.go +++ b/prover-stats-api/cmd/app/app.go @@ -6,10 +6,12 @@ import ( "os/signal" "github.com/gin-gonic/gin" + "github.com/prometheus/client_golang/prometheus" "github.com/scroll-tech/go-ethereum/log" "github.com/urfave/cli/v2" "scroll-tech/common/database" + "scroll-tech/common/observability" "scroll-tech/common/utils" "scroll-tech/common/version" @@ -53,12 +55,15 @@ func action(ctx *cli.Context) error { } }() + registry := prometheus.DefaultRegisterer + observability.Server(ctx, db) + // init Prover Stats API port := ctx.String(httpPortFlag.Name) router := gin.Default() controller.InitController(db) - route.Route(router, cfg) + route.Route(router, cfg, registry) go func() { if runServerErr := router.Run(fmt.Sprintf(":%s", port)); runServerErr != nil { diff --git a/prover-stats-api/go.mod b/prover-stats-api/go.mod index 3142a0b53a..fa11229dec 100644 --- a/prover-stats-api/go.mod +++ b/prover-stats-api/go.mod @@ -6,17 +6,24 @@ require ( github.com/appleboy/gin-jwt/v2 v2.9.1 github.com/gin-gonic/gin v1.9.1 github.com/google/uuid v1.3.0 + github.com/prometheus/client_golang v1.14.0 github.com/stretchr/testify v1.8.3 gorm.io/gorm v1.25.2 ) require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect - github.com/google/go-cmp v0.5.9 // indirect + github.com/golang/protobuf v1.5.2 // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_model v0.3.0 // indirect + github.com/prometheus/common v0.39.0 // indirect + github.com/prometheus/procfs v0.9.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect ) diff --git a/prover-stats-api/go.sum b/prover-stats-api/go.sum index 966f06dbfc..5785bad49c 100644 --- a/prover-stats-api/go.sum +++ b/prover-stats-api/go.sum @@ -4,9 +4,13 @@ github.com/appleboy/gin-jwt/v2 v2.9.1 h1:l29et8iLW6omcHltsOP6LLk4s3v4g2FbFs0koxG github.com/appleboy/gin-jwt/v2 v2.9.1/go.mod h1:jwcPZJ92uoC9nOUTOKWoN/f6JZOgMSKlFSHw5/FrRUk= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM= github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= @@ -59,10 +63,13 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -98,6 +105,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= +github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -111,6 +120,14 @@ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNc github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= +github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= +github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= +github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= +github.com/prometheus/common v0.39.0 h1:oOyhkDq05hPZKItWVBkJ6g6AtGxi+fy7F4JvUV8uhsI= +github.com/prometheus/common v0.39.0/go.mod h1:6XBZ7lYdLCbkAVhwRsWTZn+IN5AB9F/NXd5w0BbEX0Y= +github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= +github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= @@ -178,6 +195,7 @@ golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -214,6 +232,7 @@ golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= diff --git a/prover-stats-api/internal/controller/auth.go b/prover-stats-api/internal/controller/auth.go index 65be021a82..2618217fac 100644 --- a/prover-stats-api/internal/controller/auth.go +++ b/prover-stats-api/internal/controller/auth.go @@ -8,6 +8,8 @@ import ( "github.com/gin-gonic/gin" + ctype "scroll-tech/common/types" + "scroll-tech/prover-stats-api/internal/types" ) @@ -52,5 +54,5 @@ func (a *AuthController) LoginResponse(c *gin.Context, code int, message string, Time: time, Token: message, } - types.RenderJSON(c, code, nil, resp) + ctype.RenderSuccess(c, resp) } diff --git a/prover-stats-api/internal/controller/prover_task.go b/prover-stats-api/internal/controller/prover_task.go index 6d79ab54e2..16709890ec 100644 --- a/prover-stats-api/internal/controller/prover_task.go +++ b/prover-stats-api/internal/controller/prover_task.go @@ -41,14 +41,14 @@ func (c *ProverTaskController) ProverTasks(ctx *gin.Context) { var pp types.ProverTasksPaginationParameter if err := ctx.ShouldBind(&pp); err != nil { nerr := fmt.Errorf("parameter invalid, err:%w", err) - types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIParameterInvalidNo, nerr) return } tasks, err := c.logic.GetTasksByProver(ctx, pp.PublicKey, pp.Page, pp.PageSize) if err != nil { nerr := fmt.Errorf("controller.ProverTasks err:%w", err) - types.RenderJSON(ctx, types.ErrProverTaskFailure, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIProverTaskFailure, nerr) return } @@ -65,7 +65,7 @@ func (c *ProverTaskController) ProverTasks(ctx *gin.Context) { proverTaskSchemas = append(proverTaskSchemas, proverTaskSchema) } - types.RenderJSON(ctx, types.Success, nil, proverTaskSchemas) + ctype.RenderSuccess(ctx, proverTaskSchemas) } // GetTotalRewards godoc @@ -82,14 +82,14 @@ func (c *ProverTaskController) GetTotalRewards(ctx *gin.Context) { var pp types.ProverTotalRewardsParameter if err := ctx.ShouldBind(&pp); err != nil { nerr := fmt.Errorf("parameter invalid, err:%w", err) - types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIParameterInvalidNo, nerr) return } rewards, err := c.logic.GetTotalRewards(ctx, pp.PublicKey) if err != nil { nerr := fmt.Errorf("controller.GetTotalRewards, err:%w", err) - types.RenderJSON(ctx, types.ErrProverTotalRewardFailure, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIProverTotalRewardFailure, nerr) return } @@ -97,7 +97,7 @@ func (c *ProverTaskController) GetTotalRewards(ctx *gin.Context) { Rewards: rewards.String(), } - types.RenderJSON(ctx, types.Success, nil, resp) + ctype.RenderSuccess(ctx, resp) } // GetTask godoc @@ -114,14 +114,14 @@ func (c *ProverTaskController) GetTask(ctx *gin.Context) { var pp types.ProverTaskParameter if err := ctx.ShouldBind(&pp); err != nil { nerr := fmt.Errorf("parameter invalid, err:%w", err) - types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIParameterInvalidNo, nerr) return } task, err := c.logic.GetTask(ctx, pp.TaskID) if err != nil { nerr := fmt.Errorf("controller.GetTask, err:%w", err) - types.RenderJSON(ctx, types.ErrProverTotalRewardFailure, nerr, nil) + ctype.RenderFailure(ctx, ctype.ErrProverStatsAPIProverTotalRewardFailure, nerr) return } @@ -134,5 +134,5 @@ func (c *ProverTaskController) GetTask(ctx *gin.Context) { CreatedAt: task.CreatedAt, } - types.RenderJSON(ctx, types.Success, nil, schema) + ctype.RenderSuccess(ctx, schema) } diff --git a/prover-stats-api/internal/middleware/jwt.go b/prover-stats-api/internal/middleware/jwt.go index 979876f0db..735e53bb54 100644 --- a/prover-stats-api/internal/middleware/jwt.go +++ b/prover-stats-api/internal/middleware/jwt.go @@ -9,6 +9,8 @@ import ( "github.com/scroll-tech/go-ethereum/log" + ctype "scroll-tech/common/types" + "scroll-tech/prover-stats-api/internal/config" "scroll-tech/prover-stats-api/internal/controller" "scroll-tech/prover-stats-api/internal/types" @@ -47,7 +49,7 @@ func AuthMiddleware(conf *config.Config) *jwt.GinJWTMiddleware { // Unauthorized response Unauthorized error message to client func Unauthorized(c *gin.Context, code int, message string) { err := errors.New(message) - types.RenderJSON(c, code, err, nil) + ctype.RenderFailure(c, code, err) } // PayloadFunc returns jwt.MapClaims with public key. diff --git a/prover-stats-api/internal/route/route.go b/prover-stats-api/internal/route/route.go index c10d442b32..a36cab0338 100644 --- a/prover-stats-api/internal/route/route.go +++ b/prover-stats-api/internal/route/route.go @@ -2,18 +2,23 @@ package route import ( "github.com/gin-gonic/gin" + "github.com/prometheus/client_golang/prometheus" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + "scroll-tech/common/observability" + "scroll-tech/prover-stats-api/internal/config" "scroll-tech/prover-stats-api/internal/controller" "scroll-tech/prover-stats-api/internal/middleware" ) // Route routes the APIs -func Route(router *gin.Engine, conf *config.Config) { +func Route(router *gin.Engine, conf *config.Config, reg prometheus.Registerer) { router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + observability.Use(router, "prover_stats_api", reg) + r := router.Group("api/prover_task") v1(r, conf) diff --git a/prover-stats-api/internal/types/errno.go b/prover-stats-api/internal/types/errno.go deleted file mode 100644 index 62a0de888d..0000000000 --- a/prover-stats-api/internal/types/errno.go +++ /dev/null @@ -1,12 +0,0 @@ -package types - -const ( - // Success shows OK. - Success = 200 - // ErrParameterInvalidNo is invalid params - ErrParameterInvalidNo = 10001 - // ErrProverTaskFailure is getting prover task error - ErrProverTaskFailure = 10002 - // ErrProverTotalRewardFailure is getting total rewards error - ErrProverTotalRewardFailure = 10003 -) diff --git a/prover-stats-api/internal/types/response.go b/prover-stats-api/internal/types/response.go deleted file mode 100644 index 61a69b1f8d..0000000000 --- a/prover-stats-api/internal/types/response.go +++ /dev/null @@ -1,28 +0,0 @@ -package types - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -// Response the response schema -type Response struct { - ErrCode int `json:"errcode"` - ErrMsg string `json:"errmsg"` - Data interface{} `json:"data"` -} - -// RenderJSON renders response with json -func RenderJSON(ctx *gin.Context, errCode int, err error, data interface{}) { - var errMsg string - if err != nil { - errMsg = err.Error() - } - renderData := Response{ - ErrCode: errCode, - ErrMsg: errMsg, - Data: data, - } - ctx.JSON(http.StatusOK, renderData) -} diff --git a/prover-stats-api/test/prover_task_test.go b/prover-stats-api/test/prover_task_test.go index f456129e77..efc6e3f7bc 100644 --- a/prover-stats-api/test/prover_task_test.go +++ b/prover-stats-api/test/prover_task_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/prometheus/client_golang/prometheus" "github.com/shopspring/decimal" "github.com/stretchr/testify/assert" "gorm.io/gorm" @@ -25,7 +26,6 @@ import ( "scroll-tech/prover-stats-api/internal/controller" "scroll-tech/prover-stats-api/internal/orm" "scroll-tech/prover-stats-api/internal/route" - apitypes "scroll-tech/prover-stats-api/internal/types" ) var ( @@ -42,7 +42,7 @@ func mockHTTPServer(cfg *config.Config, db *gorm.DB) (*http.Server, error) { // run Prover Stats APIs router := gin.Default() controller.InitController(db) - route.Route(router, cfg) + route.Route(router, cfg, prometheus.DefaultRegisterer) return utils.StartHTTPServer(addr, router) } @@ -116,10 +116,9 @@ func getResp(t *testing.T, url string) interface{} { byt, err := io.ReadAll(resp.Body) assert.NoError(t, err) - res := new(apitypes.Response) + res := new(types.Response) assert.NoError(t, json.Unmarshal(byt, res)) - t.Log("----byt is ", string(byt)) - assert.Equal(t, apitypes.Success, res.ErrCode) + assert.Equal(t, types.Success, res.ErrCode) return res.Data } diff --git a/rollup/cmd/event_watcher/app/app.go b/rollup/cmd/event_watcher/app/app.go index aa5633e3f0..05728a35d5 100644 --- a/rollup/cmd/event_watcher/app/app.go +++ b/rollup/cmd/event_watcher/app/app.go @@ -13,7 +13,7 @@ import ( "github.com/urfave/cli/v2" "scroll-tech/common/database" - "scroll-tech/common/metrics" + "scroll-tech/common/observability" "scroll-tech/common/utils" "scroll-tech/common/version" @@ -61,7 +61,7 @@ func action(ctx *cli.Context) error { }() registry := prometheus.DefaultRegisterer - metrics.Server(ctx, registry.(*prometheus.Registry)) + observability.Server(ctx, db) l1client, err := ethclient.Dial(cfg.L1Config.Endpoint) if err != nil { log.Error("failed to connect l1 geth", "config file", cfgFile, "error", err) diff --git a/rollup/cmd/gas_oracle/app/app.go b/rollup/cmd/gas_oracle/app/app.go index 7cf08a8952..230f18019c 100644 --- a/rollup/cmd/gas_oracle/app/app.go +++ b/rollup/cmd/gas_oracle/app/app.go @@ -14,7 +14,7 @@ import ( "github.com/urfave/cli/v2" "scroll-tech/common/database" - "scroll-tech/common/metrics" + "scroll-tech/common/observability" "scroll-tech/common/utils" "scroll-tech/common/version" @@ -64,7 +64,7 @@ func action(ctx *cli.Context) error { }() registry := prometheus.DefaultRegisterer - metrics.Server(ctx, registry.(*prometheus.Registry)) + observability.Server(ctx, db) l1client, err := ethclient.Dial(cfg.L1Config.Endpoint) if err != nil { diff --git a/rollup/cmd/rollup_relayer/app/app.go b/rollup/cmd/rollup_relayer/app/app.go index dca210a324..63213fd297 100644 --- a/rollup/cmd/rollup_relayer/app/app.go +++ b/rollup/cmd/rollup_relayer/app/app.go @@ -13,7 +13,7 @@ import ( "github.com/urfave/cli/v2" "scroll-tech/common/database" - "scroll-tech/common/metrics" + "scroll-tech/common/observability" "scroll-tech/common/utils" "scroll-tech/common/version" @@ -64,7 +64,7 @@ func action(ctx *cli.Context) error { }() registry := prometheus.DefaultRegisterer - metrics.Server(ctx, registry.(*prometheus.Registry)) + observability.Server(ctx, db) // Init l2geth connection l2client, err := ethclient.Dial(cfg.L2Config.Endpoint) diff --git a/rollup/conf/config.json b/rollup/conf/config.json index f649d821c8..5261f30726 100644 --- a/rollup/conf/config.json +++ b/rollup/conf/config.json @@ -8,7 +8,7 @@ "relayer_config": { "gas_price_oracle_address": "0x0000000000000000000000000000000000000000", "sender_config": { - "endpoint": "/var/lib/jenkins/workspace/SequencerPipeline/MyPrivateNetwork/geth.ipc", + "endpoint": "https://sepolia-rpc.scroll.io", "check_pending_time": 2, "check_balance_time": 100, "escalate_blocks": 100, @@ -30,14 +30,14 @@ }, "l2_config": { "confirmations": "0x1", - "endpoint": "/var/lib/jenkins/workspace/SequencerPipeline/MyPrivateNetwork/geth.ipc", + "endpoint": "https://sepolia-rpc.scroll.io", "l2_messenger_address": "0x0000000000000000000000000000000000000000", "l2_message_queue_address": "0x0000000000000000000000000000000000000000", "relayer_config": { "rollup_contract_address": "0x0000000000000000000000000000000000000000", "gas_price_oracle_address": "0x0000000000000000000000000000000000000000", "sender_config": { - "endpoint": "DUMMY_ENDPOINT", + "endpoint": "https://sepolia-rpc.scroll.io", "check_pending_time": 10, "check_balance_time": 100, "escalate_blocks": 100, @@ -83,7 +83,7 @@ }, "db_config": { "driver_name": "postgres", - "dsn": "postgres://admin:123456@localhost/test?sslmode=disable", + "dsn": "postgres://localhost/scroll?sslmode=disable", "maxOpenNum": 200, "maxIdleNum": 20 }