Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(observability):add ready&health check api for k8s #938

Merged
merged 23 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9771100
feat: add ready&health to coordinator&bridge-history&prover_stats-api
georgehao Sep 8, 2023
1be602d
feat: fmt
georgehao Sep 8, 2023
4eab8f9
feat: rename metrics to observability
georgehao Sep 8, 2023
98de7a5
feat: format
georgehao Sep 8, 2023
7868e0b
chore: auto version bump [bot]
georgehao Sep 8, 2023
e64a425
feat: refactor config.json
georgehao Sep 8, 2023
224832d
Merge branch 'feat/add-api-ready' of github.com:scroll-tech/scroll in…
georgehao Sep 8, 2023
e9d1cf9
feat: fix prover-stats-api unit test
georgehao Sep 8, 2023
fa9517d
feat: fix lint failure
georgehao Sep 8, 2023
7f5338e
Update bridge-history-api/internal/types/history_types.go
georgehao Sep 8, 2023
bf99767
feat: address comments
georgehao Sep 8, 2023
29ce7c4
Merge branch 'feat/add-api-ready' of github.com:scroll-tech/scroll in…
georgehao Sep 8, 2023
d252709
feat: fix comments
georgehao Sep 8, 2023
492adce
feat: fix prover-stats-api unit test
georgehao Sep 8, 2023
681f191
feat: fix comments
georgehao Sep 8, 2023
d156111
Merge branch 'develop' into feat/add-api-ready
georgehao Sep 11, 2023
1d0385d
chore: auto version bump [bot]
georgehao Sep 11, 2023
35a8a5f
feat: add test for db open
georgehao Sep 11, 2023
4cdd624
Merge branch 'feat/add-api-ready' of github.com:scroll-tech/scroll in…
georgehao Sep 11, 2023
9943eac
Merge branch 'develop' into feat/add-api-ready
georgehao Sep 11, 2023
5fbc5ce
feat: fix lint failure
georgehao Sep 11, 2023
d06a6d9
Merge branch 'feat/add-api-ready' of github.com:scroll-tech/scroll in…
georgehao Sep 11, 2023
10977c8
feat: address comments
georgehao Sep 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bridge-history-api/internal/controller/batch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 8 additions & 1 deletion bridge-history-api/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -19,5 +24,7 @@ func InitController(db *gorm.DB) {
initControllerOnce.Do(func() {
HistoryCtrler = NewHistoryController(db)
BatchCtrler = NewBatchController(db)
HealthCheck = NewHealthCheckController(db)
Ready = NewReadyController()
})
}
30 changes: 30 additions & 0 deletions bridge-history-api/internal/controller/health_check.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 8 additions & 8 deletions bridge-history-api/internal/controller/history_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
}
21 changes: 21 additions & 0 deletions bridge-history-api/internal/controller/ready.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions bridge-history-api/internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 27 additions & 0 deletions bridge-history-api/internal/types/history_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
20 changes: 16 additions & 4 deletions bridge-history-api/utils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"database/sql"
"fmt"
"time"

Expand Down Expand Up @@ -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.
Expand Down
24 changes: 18 additions & 6 deletions common/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"database/sql"
"fmt"
"time"

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
27 changes: 27 additions & 0 deletions common/database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 35 additions & 0 deletions common/observability/probes.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading
Loading