Skip to content

Commit

Permalink
fix(telemetry): start telemetry independently from the API server (#1…
Browse files Browse the repository at this point in the history
…2448)

* fix(telemetry): start telemetry independently from the API server

* fix unlock

* add changelog

* Update CHANGELOG.md

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
(cherry picked from commit 9057db6)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
robert-zaremba authored and mergify[bot] committed Jul 7, 2022
1 parent 846b32b commit 6d41d57
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (types) [#12154](https://github.com/cosmos/cosmos-sdk/pull/12154) Add `baseAccountGetter` to avoid invalid account error when create vesting account.
* (x/crisis) [#12208](https://github.com/cosmos/cosmos-sdk/pull/12208) Fix progress index of crisis invariant assertion logs.
* (types) [#12229](https://github.com/cosmos/cosmos-sdk/pull/12229) Increase sdk.Dec maxApproxRootIterations to 300
<<<<<<< HEAD
=======
* (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`.
* (x/auth/tx) [#12474](https://github.com/cosmos/cosmos-sdk/pull/12474) Remove condition in GetTxsEvent that disallowed multiple equal signs, which would break event queries with base64 strings (i.e. query by signature).
* [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Start telemetry independently from the API server.
>>>>>>> 9057db6ec (fix(telemetry): start telemetry independently from the API server (#12448))
## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23

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

tmCfg := tmrpcserver.DefaultConfig()
tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections)
Expand All @@ -114,18 +104,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 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 @@ -140,6 +129,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
21 changes: 21 additions & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -319,6 +325,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 Down Expand Up @@ -366,6 +377,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 @@ -488,3 +502,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 6d41d57

Please sign in to comment.