Merge PR #6399: SDK Telemetry

This commit is contained in:
Alexander Bezobchuk
2020-06-16 11:11:02 -04:00
committed by GitHub
parent e2f336345d
commit 8ee7d1f403
11 changed files with 492 additions and 89 deletions
+41 -9
View File
@@ -1,9 +1,11 @@
package api
import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/handlers"
@@ -14,6 +16,8 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/rest"
// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
@@ -25,6 +29,7 @@ type Server struct {
ClientCtx client.Context
logger log.Logger
metrics *telemetry.Metrics
listener net.Listener
}
@@ -40,18 +45,28 @@ func New(clientCtx client.Context) *Server {
// JSON RPC server. Configuration options are provided via config.APIConfig
// and are delegated to the Tendermint JSON RPC server. The process is
// non-blocking, so an external signal handler must be used.
func (s *Server) Start(cfg config.APIConfig) error {
if cfg.Swagger {
func (s *Server) Start(cfg config.Config) error {
if cfg.API.Swagger {
s.registerSwaggerUI()
}
tmCfg := tmrpcserver.DefaultConfig()
tmCfg.MaxOpenConnections = int(cfg.MaxOpenConnections)
tmCfg.ReadTimeout = time.Duration(cfg.RPCReadTimeout) * time.Second
tmCfg.WriteTimeout = time.Duration(cfg.RPCWriteTimeout) * time.Second
tmCfg.MaxBodyBytes = int64(cfg.RPCMaxBodyBytes)
if cfg.Telemetry.Enabled {
m, err := telemetry.New(cfg.Telemetry)
if err != nil {
return err
}
listener, err := tmrpcserver.Listen(cfg.Address, tmCfg)
s.metrics = m
s.registerMetrics()
}
tmCfg := tmrpcserver.DefaultConfig()
tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections)
tmCfg.ReadTimeout = time.Duration(cfg.API.RPCReadTimeout) * time.Second
tmCfg.WriteTimeout = time.Duration(cfg.API.RPCWriteTimeout) * time.Second
tmCfg.MaxBodyBytes = int64(cfg.API.RPCMaxBodyBytes)
listener, err := tmrpcserver.Listen(cfg.API.Address, tmCfg)
if err != nil {
return err
}
@@ -59,7 +74,7 @@ func (s *Server) Start(cfg config.APIConfig) error {
s.listener = listener
var h http.Handler = s.Router
if cfg.EnableUnsafeCORS {
if cfg.API.EnableUnsafeCORS {
return tmrpcserver.Serve(s.listener, handlers.CORS()(h), s.logger, tmCfg)
}
@@ -75,3 +90,20 @@ func (s *Server) registerSwaggerUI() {
staticServer := http.FileServer(statikFS)
s.Router.PathPrefix("/").Handler(staticServer)
}
func (s *Server) registerMetrics() {
metricsHandler := func(w http.ResponseWriter, r *http.Request) {
format := strings.TrimSpace(r.FormValue("format"))
gr, err := s.metrics.Gather(format)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to gather metrics: %s", err))
return
}
w.Header().Set("Content-Type", gr.ContentType)
_, _ = w.Write(gr.Metrics)
}
s.Router.HandleFunc("/metrics", metricsHandler).Methods("GET")
}
+38 -1
View File
@@ -4,7 +4,10 @@ import (
"fmt"
"strings"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -75,7 +78,9 @@ type APIConfig struct {
type Config struct {
BaseConfig `mapstructure:",squash"`
API APIConfig `mapstructure:"api"`
// Telemetry defines the application telemetry configuration
Telemetry telemetry.Config `mapstructure:"telemetry"`
API APIConfig `mapstructure:"api"`
}
// SetMinGasPrices sets the validator's minimum gas prices.
@@ -115,6 +120,7 @@ func DefaultConfig() *Config {
PruningKeepEvery: "0",
PruningSnapshotEvery: "0",
},
Telemetry: telemetry.Config{},
API: APIConfig{
Enable: false,
Swagger: false,
@@ -125,3 +131,34 @@ func DefaultConfig() *Config {
},
}
}
// GetConfig returns a fully parsed Config object.
func GetConfig() Config {
return Config{
BaseConfig: BaseConfig{
MinGasPrices: viper.GetString("minimum-gas-prices"),
InterBlockCache: viper.GetBool("inter-block-cache"),
Pruning: viper.GetString("pruning"),
PruningKeepEvery: viper.GetString("pruning-keep-every"),
PruningSnapshotEvery: viper.GetString("pruning-snapshot-every"),
HaltHeight: viper.GetUint64("halt-height"),
HaltTime: viper.GetUint64("halt-time"),
},
Telemetry: telemetry.Config{
ServiceName: viper.GetString("telemetry.service-name"),
Enabled: viper.GetBool("telemetry.enabled"),
EnableHostname: viper.GetBool("telemetry.enable-hostname"),
EnableHostnameLabel: viper.GetBool("telemetry.enable-hostname-label"),
EnableServiceLabel: viper.GetBool("telemetry.enable-service-label"),
PrometheusRetentionTime: viper.GetInt64("telemetry.prometheus-retention-time"),
},
API: APIConfig{
Address: viper.GetString("api.address"),
MaxOpenConnections: viper.GetUint("api.max-open-connections"),
RPCReadTimeout: viper.GetUint("api.rpc-read-timeout"),
RPCWriteTimeout: viper.GetUint("api.rpc-write-timeout"),
RPCMaxBodyBytes: viper.GetUint("api.rpc-max-body-bytes"),
EnableUnsafeCORS: viper.GetBool("api.enabled-unsafe-cors"),
},
}
}
+26
View File
@@ -47,6 +47,32 @@ halt-time = {{ .BaseConfig.HaltTime }}
# InterBlockCache enables inter-block caching.
inter-block-cache = {{ .BaseConfig.InterBlockCache }}
###############################################################################
### Telemetry Configuration ###
###############################################################################
[telemetry]
# Prefixed with keys to separate services
service-name = "{{ .Telemetry.ServiceName }}"
# Enabled enables the application telemetry functionality. When enabled,
# an in-memory sink is also enabled by default. Operators may also enabled
# other sinks such as Prometheus.
enabled = {{ .Telemetry.Enabled }}
# Enable prefixing gauge values with hostname
enable-hostname = {{ .Telemetry.EnableHostname }}
# Enable adding hostname to labels
enable-hostname-label = {{ .Telemetry.EnableHostnameLabel }}
# Enable adding service to labels
enable-service-label = {{ .Telemetry.EnableServiceLabel }}
# PrometheusRetentionTime, when positive, enables a Prometheus metrics sink.
prometheus-retention-time = {{ .Telemetry.PrometheusRetentionTime }}
###############################################################################
### API Configuration ###
###############################################################################
+1 -10
View File
@@ -209,18 +209,9 @@ func startInProcess(ctx *Context, cdc codec.JSONMarshaler, appCreator AppCreator
WithTrustNode(true)
apiSrv := api.New(ctx)
apiCfg := config.APIConfig{
Address: viper.GetString("api.address"),
MaxOpenConnections: viper.GetUint("api.max-open-connections"),
RPCReadTimeout: viper.GetUint("api.rpc-read-timeout"),
RPCWriteTimeout: viper.GetUint("api.rpc-write-timeout"),
RPCMaxBodyBytes: viper.GetUint("api.rpc-max-body-bytes"),
EnableUnsafeCORS: viper.GetBool("api.enabled-unsafe-cors"),
}
app.RegisterAPIRoutes(apiSrv)
if err := apiSrv.Start(apiCfg); err != nil {
if err := apiSrv.Start(config.GetConfig()); err != nil {
return err
}
}