Skip to content

Commit

Permalink
fix(telemetry): start telemetry independently from the API server (ba…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and JeancarloBarrios committed Sep 28, 2024
1 parent b1aabae commit f6a92bb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ It is strongly recommended to upgrade to these releases as well.

* [#12453](https://github.com/cosmos/cosmos-sdk/pull/12453) Add `NewInMemoryWithKeyring` function which allows the creation of in memory `keystore` instances with a specified set of existing items.

### Bug Fixes

* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Start telemetry independently from the API server.

## [v0.46.0-rc2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc2) - 2022-07-05

### Features
Expand Down
22 changes: 10 additions & 12 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ func New(clientCtx client.Context, logger log.Logger, grpcSrv *grpc.Server) *Ser
// 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()
}

tmCfg := tmrpcserver.DefaultConfig()
tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections)
Expand All @@ -116,17 +106,18 @@ func (s *Server) Start(cfg config.Config) error {
return err
}

s.registerGRPCGatewayRoutes()
s.listener = listener
s.mtx.Unlock()

s.mtx.Unlock()

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)
}

Expand All @@ -144,6 +135,13 @@ func (s *Server) SetTelemetry(m *telemetry.Metrics) {
s.mtx.Unlock()
}

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
38 changes: 30 additions & 8 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"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"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/cosmos/cosmos-sdk/version"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
)

const (
Expand Down Expand Up @@ -217,7 +216,11 @@ func start[T types.Application](svrCtx *Context, clientCtx client.Context, appCr
return err
}

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

if !withCmt {
return startStandAlone[T](svrCtx, svrCfg, clientCtx, app, metrics, opts)
Expand Down Expand Up @@ -398,6 +401,11 @@ func startInProcess[T types.Application](svrCtx *Context, svrCfg serverconfig.Co
return config, nil
}

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

var apiSrv *api.Server
if config.API.Enable {
genDoc, err := genDocProvider()
Expand All @@ -412,11 +420,18 @@ func startInProcess[T types.Application](svrCtx *Context, svrCfg serverconfig.Co
return defaultGenesisDoc, err
}

bz, err := json.Marshal(genbz)
if err != nil {
return defaultGenesisDoc, err
apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"))
app.RegisterAPIRoutes(apiSrv, config.API)
if config.Telemetry.Enabled {
apiSrv.SetTelemetry(metrics)
}
sum := sha256.Sum256(bz)
errCh := make(chan error)

go func() {
if err := apiSrv.Start(config); err != nil {
errCh <- err
}
}()

select {
case err := <-errCh:
Expand Down Expand Up @@ -742,3 +757,10 @@ func testnetify[T types.Application](ctx *Context, testnetAppCreator types.AppCr
// 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 f6a92bb

Please sign in to comment.