From d99fa2bab5d411452c15f429dc79aa9f1bf456b2 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 5 Jul 2022 15:10:22 +0200 Subject: [PATCH 1/4] fix(telemetry): start telemetry independently from the API server --- server/api/server.go | 21 ++++++++------------- server/start.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/server/api/server.go b/server/api/server.go index c052c03f52ca..4b33a5728bec 100644 --- a/server/api/server.go +++ b/server/api/server.go @@ -90,16 +90,7 @@ 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() - } + defer s.mtx.Unlock() tmCfg := tmrpcserver.DefaultConfig() tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections) @@ -109,7 +100,6 @@ func (s *Server) Start(cfg config.Config) error { listener, err := tmrpcserver.Listen(cfg.API.Address, tmCfg.MaxOpenConnections) if err != nil { - s.mtx.Unlock() return err } @@ -120,12 +110,10 @@ func (s *Server) Start(cfg config.Config) error { if cfg.API.EnableUnsafeCORS { allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"})) - s.mtx.Unlock() return tmrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, tmCfg) } s.logger.Info("starting API server...") - s.mtx.Unlock() return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg) } @@ -140,6 +128,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")) diff --git a/server/start.go b/server/start.go index 9c76dbdffa06..d745ddc44a11 100644 --- a/server/start.go +++ b/server/start.go @@ -27,11 +27,13 @@ import ( "github.com/cosmos/cosmos-sdk/codec" pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types" "github.com/cosmos/cosmos-sdk/server/api" + "github.com/cosmos/cosmos-sdk/server/config" serverconfig "github.com/cosmos/cosmos-sdk/server/config" servergrpc "github.com/cosmos/cosmos-sdk/server/grpc" "github.com/cosmos/cosmos-sdk/server/rosetta" crgserver "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server" "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/telemetry" sdktypes "github.com/cosmos/cosmos-sdk/types" ) @@ -207,6 +209,10 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error { } app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper) + _, err = startTelemetry(serverconfig.GetConfig(ctx.Viper)) + if err != nil { + return err + } svr, err := server.NewServer(addr, transport, app) if err != nil { @@ -318,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 := tmtypes.GenesisDocFromFile(cfg.GenesisFile()) @@ -365,6 +376,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() { @@ -487,3 +501,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) +} From af5fc9138641be6f4fbe864f8c768b88498daf05 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 7 Jul 2022 15:59:56 +0200 Subject: [PATCH 2/4] fix unlock --- server/api/server.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/api/server.go b/server/api/server.go index 4b33a5728bec..2abd0130ce05 100644 --- a/server/api/server.go +++ b/server/api/server.go @@ -90,7 +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() - defer s.mtx.Unlock() tmCfg := tmrpcserver.DefaultConfig() tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections) @@ -100,14 +99,16 @@ func (s *Server) Start(cfg config.Config) error { listener, err := tmrpcserver.Listen(cfg.API.Address, tmCfg.MaxOpenConnections) if err != nil { + s.mtx.Unlock() return err } 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"})) return tmrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, tmCfg) From 13a723d6350312ea072e852322f8cac068274fb7 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 7 Jul 2022 16:02:36 +0200 Subject: [PATCH 3/4] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c46e3cb1eeb..134e55eb7db3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/staking) [#12303](https://github.com/cosmos/cosmos-sdk/pull/12303) Use bytes instead of string comparison in delete validator queue * (testutil/sims) [#12374](https://github.com/cosmos/cosmos-sdk/pull/12374) fix the non-determinstic behavior in simulations caused by `GenSignedMockTx` and check empty coins slice before it is used to create `banktype.MsgSend`. +* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Fix: start telemetry independently from the API server. ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 From 58d63e9ad5c0b07a647dddca67b2e6b7f746d2b4 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 7 Jul 2022 16:21:51 +0200 Subject: [PATCH 4/4] Update CHANGELOG.md Co-authored-by: Aleksandr Bezobchuk --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 134e55eb7db3..761597578a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,7 +88,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/staking) [#12303](https://github.com/cosmos/cosmos-sdk/pull/12303) Use bytes instead of string comparison in delete validator queue * (testutil/sims) [#12374](https://github.com/cosmos/cosmos-sdk/pull/12374) fix the non-determinstic behavior in simulations caused by `GenSignedMockTx` and check empty coins slice before it is used to create `banktype.MsgSend`. -* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Fix: start telemetry independently from the API server. +* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Start telemetry independently from the API server. ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23