Skip to content

Commit

Permalink
fix: start telemetry independently from the API server from tkxkd0159…
Browse files Browse the repository at this point in the history
…/se901

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 27, 2022
2 parents 532154f + 6292977 commit 1ebc973
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (client) [\#737](https://github.com/line/lbm-sdk/pull/737) check multisig key list to prevent unexpected key deletion
* (simapp) [\#752](https://github.com/line/lbm-sdk/pull/752) add x/distribution's module account into blockedAddr
* (x/auth) [\#754](https://github.com/line/lbm-sdk/pull/754) Fix wrong sequences in `sign-batch`
* (server) [\#763](https://github.com/line/lbm-sdk/pull/763) start telemetry independently from the API server

### Breaking Changes
* (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path
Expand Down
22 changes: 9 additions & 13 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
// non-blocking, so an external signal handler must be used.
func (s *Server) Start(cfg config.Config) error {
s.mtx.Lock()
if cfg.Telemetry.Enabled {
m, err := telemetry.New(cfg.Telemetry)
if err != nil {
s.mtx.Unlock()
return err
}

s.metrics = m
s.registerMetrics()
}

ostCfg := ostrpcserver.DefaultConfig()
ostCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections)
Expand All @@ -115,18 +105,17 @@ func (s *Server) Start(cfg config.Config) error {
}

s.registerGRPCGatewayRoutes()

s.listener = listener
var h http.Handler = s.Router

s.mtx.Unlock()

if cfg.API.EnableUnsafeCORS {
allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))
s.mtx.Unlock()
return ostrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, ostCfg)
}

s.logger.Info("starting API server...")
s.mtx.Unlock()
return ostrpcserver.Serve(s.listener, s.Router, s.logger, ostCfg)
}

Expand All @@ -141,6 +130,13 @@ func (s *Server) registerGRPCGatewayRoutes() {
s.Router.PathPrefix("/").Handler(s.GRPCGatewayRouter)
}

func (s *Server) SetTelemetry(m *telemetry.Metrics) {
s.mtx.Lock()
s.metrics = m
s.registerMetrics()
s.mtx.Unlock()
}

func (s *Server) registerMetrics() {
metricsHandler := func(w http.ResponseWriter, r *http.Request) {
format := strings.TrimSpace(r.FormValue("format"))
Expand Down
20 changes: 20 additions & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/line/lbm-sdk/store/cache"
"github.com/line/lbm-sdk/store/iavl"
storetypes "github.com/line/lbm-sdk/store/types"
"github.com/line/lbm-sdk/telemetry"
)

// Ostracon full-node start flags
Expand Down Expand Up @@ -200,6 +201,10 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error {
}

app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper)
_, err = startTelemetry(config.GetConfig(ctx.Viper))
if err != nil {
return err
}

svr, err := server.NewServer(addr, transport, app)
if err != nil {
Expand Down Expand Up @@ -319,6 +324,11 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
app.RegisterTendermintService(clientCtx)
}

metrics, err := startTelemetry(config)
if err != nil {
return err
}

var apiSrv *api.Server
if config.API.Enable {
genDoc, err := genDocProvider()
Expand All @@ -330,6 +340,9 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App

apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"))
app.RegisterAPIRoutes(apiSrv, config.API)
if config.Telemetry.Enabled {
apiSrv.SetTelemetry(metrics)
}
errCh := make(chan error)

go func() {
Expand Down Expand Up @@ -441,3 +454,10 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
// wait for signal capture and gracefully return
return WaitForQuitSignals()
}

func startTelemetry(cfg config.Config) (*telemetry.Metrics, error) {
if !cfg.Telemetry.Enabled {
return nil, nil
}
return telemetry.New(cfg.Telemetry)
}

0 comments on commit 1ebc973

Please sign in to comment.