parent
d1bdef6f83
commit
d891c13cb3
6 changed files with 60 additions and 76 deletions
22
app.go
22
app.go
|
@ -14,22 +14,19 @@ import (
|
|||
"github.com/nspcc-dev/neofs-http-gw/downloader"
|
||||
"github.com/nspcc-dev/neofs-http-gw/response"
|
||||
"github.com/nspcc-dev/neofs-http-gw/uploader"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/logger"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
)
|
||||
|
||||
type (
|
||||
app struct {
|
||||
log *zap.Logger
|
||||
pool *pool.Pool
|
||||
cfg *viper.Viper
|
||||
auxiliaryLog logger.Logger
|
||||
webServer *fasthttp.Server
|
||||
webDone chan struct{}
|
||||
log *zap.Logger
|
||||
pool *pool.Pool
|
||||
cfg *viper.Viper
|
||||
webServer *fasthttp.Server
|
||||
webDone chan struct{}
|
||||
}
|
||||
|
||||
// App is an interface for the main gateway function.
|
||||
|
@ -77,10 +74,7 @@ func newApp(ctx context.Context, opt ...Option) App {
|
|||
for i := range opt {
|
||||
opt[i](a)
|
||||
}
|
||||
a.auxiliaryLog = logger.GRPC(a.log)
|
||||
if a.cfg.GetBool(cmdVerbose) {
|
||||
grpclog.SetLoggerV2(a.auxiliaryLog)
|
||||
}
|
||||
|
||||
// -- setup FastHTTP server --
|
||||
a.webServer.Name = "neofs-http-gw"
|
||||
a.webServer.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize)
|
||||
|
@ -182,7 +176,7 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string, password *string) (*ecds
|
|||
}
|
||||
|
||||
func (a *app) Wait() {
|
||||
a.log.Info("starting application", zap.String("version", a.cfg.GetString(cfgApplicationVersion)))
|
||||
a.log.Info("starting application", zap.String("app_name", "neofs-http-gw"), zap.String("version", Version))
|
||||
<-a.webDone // wait for web-server to be stopped
|
||||
}
|
||||
|
||||
|
@ -220,7 +214,7 @@ func (a *app) Serve(ctx context.Context) {
|
|||
// enable metrics
|
||||
if a.cfg.GetBool(cmdMetrics) {
|
||||
a.log.Info("added path /metrics/")
|
||||
attachMetrics(r, a.auxiliaryLog)
|
||||
attachMetrics(r, a.log)
|
||||
}
|
||||
// enable pprof
|
||||
if a.cfg.GetBool(cmdPprof) {
|
||||
|
|
1
go.mod
1
go.mod
|
@ -24,6 +24,5 @@ require (
|
|||
go.uber.org/zap v1.18.1
|
||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
|
||||
golang.org/x/tools v0.1.5 // indirect
|
||||
google.golang.org/grpc v1.41.0
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
)
|
||||
|
|
57
main.go
57
main.go
|
@ -2,12 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/nspcc-dev/neofs-sdk-go/logger"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -21,25 +22,45 @@ func main() {
|
|||
app.Wait()
|
||||
}
|
||||
|
||||
// newLogger constructs a zap.Logger instance for current application.
|
||||
// Panics on failure.
|
||||
//
|
||||
// Logger is built from zap's production logging configuration with:
|
||||
// * parameterized level (debug by default)
|
||||
// * console encoding
|
||||
// * ISO8601 time encoding
|
||||
//
|
||||
// Logger records a stack trace for all messages at or above fatal level.
|
||||
//
|
||||
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
||||
func newLogger(v *viper.Viper) *zap.Logger {
|
||||
options := []logger.Option{
|
||||
logger.WithLevel(v.GetString(cfgLoggerLevel)),
|
||||
logger.WithTraceLevel(v.GetString(cfgLoggerTraceLevel)),
|
||||
logger.WithFormat(v.GetString(cfgLoggerFormat)),
|
||||
logger.WithSamplingInitial(v.GetInt(cfgLoggerSamplingInitial)),
|
||||
logger.WithSamplingThereafter(v.GetInt(cfgLoggerSamplingThereafter)),
|
||||
logger.WithAppName(v.GetString(cfgApplicationName)),
|
||||
logger.WithAppVersion(v.GetString(cfgApplicationVersion)),
|
||||
}
|
||||
if v.GetBool(cfgLoggerNoCaller) {
|
||||
options = append(options, logger.WithoutCaller())
|
||||
}
|
||||
if v.GetBool(cfgLoggerNoDisclaimer) {
|
||||
options = append(options, logger.WithoutDisclaimer())
|
||||
}
|
||||
l, err := logger.New(options...)
|
||||
var lvl zapcore.Level
|
||||
lvlStr := v.GetString(cfgLoggerLevel)
|
||||
err := lvl.UnmarshalText([]byte(lvlStr))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
panic(fmt.Sprintf("incorrect logger level configuration %s (%v), "+
|
||||
"value should be one of %v", lvlStr, err, [...]zapcore.Level{
|
||||
zapcore.DebugLevel,
|
||||
zapcore.InfoLevel,
|
||||
zapcore.WarnLevel,
|
||||
zapcore.ErrorLevel,
|
||||
zapcore.DPanicLevel,
|
||||
zapcore.PanicLevel,
|
||||
zapcore.FatalLevel,
|
||||
}))
|
||||
}
|
||||
|
||||
c := zap.NewProductionConfig()
|
||||
c.Level = zap.NewAtomicLevelAt(lvl)
|
||||
c.Encoding = "console"
|
||||
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
|
||||
l, err := c.Build(
|
||||
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
||||
)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
|
21
metrics.go
21
metrics.go
|
@ -9,21 +9,14 @@ import (
|
|||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func attachMetrics(r *router.Router, z promhttp.Logger) {
|
||||
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, promhttp.HandlerOpts{
|
||||
ErrorLog: z,
|
||||
// ErrorHandling: 0,
|
||||
// Registry: nil,
|
||||
// DisableCompression: false,
|
||||
// MaxRequestsInFlight: 0,
|
||||
// Timeout: 0,
|
||||
// EnableOpenMetrics: false,
|
||||
}))
|
||||
func attachMetrics(r *router.Router, l *zap.Logger) {
|
||||
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, l, promhttp.HandlerOpts{}))
|
||||
}
|
||||
|
||||
func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp.RequestHandler {
|
||||
func metricsHandler(reg prometheus.Gatherer, logger *zap.Logger, opts promhttp.HandlerOpts) fasthttp.RequestHandler {
|
||||
var (
|
||||
inFlightSem chan struct{}
|
||||
errCnt = prometheus.NewCounterVec(
|
||||
|
@ -66,7 +59,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
|
|||
}
|
||||
mfs, err := reg.Gather()
|
||||
if err != nil {
|
||||
if opts.ErrorLog != nil {
|
||||
if logger != nil {
|
||||
panic("error gathering metrics:" + err.Error())
|
||||
}
|
||||
|
||||
|
@ -99,8 +92,8 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
|
|||
return false
|
||||
}
|
||||
lastErr = err
|
||||
if opts.ErrorLog != nil {
|
||||
opts.ErrorLog.Println("error encoding and sending metric family:", err)
|
||||
if logger != nil {
|
||||
logger.Error("encoding and sending metric family", zap.Error(err))
|
||||
}
|
||||
errCnt.WithLabelValues("encoding").Inc()
|
||||
switch opts.ErrorHandling {
|
||||
|
|
30
settings.go
30
settings.go
|
@ -36,13 +36,7 @@ const (
|
|||
cfgRebalance = "rebalance_timer"
|
||||
|
||||
// Logger.
|
||||
cfgLoggerLevel = "logger.level"
|
||||
cfgLoggerFormat = "logger.format"
|
||||
cfgLoggerTraceLevel = "logger.trace_level"
|
||||
cfgLoggerNoCaller = "logger.no_caller"
|
||||
cfgLoggerNoDisclaimer = "logger.no_disclaimer"
|
||||
cfgLoggerSamplingInitial = "logger.sampling.initial"
|
||||
cfgLoggerSamplingThereafter = "logger.sampling.thereafter"
|
||||
cfgLoggerLevel = "logger.level"
|
||||
|
||||
// Wallet.
|
||||
cfgWalletPassphrase = "wallet.passphrase"
|
||||
|
@ -56,10 +50,6 @@ const (
|
|||
// Zip compression.
|
||||
cfgZipCompression = "zip.compression"
|
||||
|
||||
// Application.
|
||||
cfgApplicationName = "app.name"
|
||||
cfgApplicationVersion = "app.version"
|
||||
|
||||
// Command line args.
|
||||
cmdHelp = "help"
|
||||
cmdVersion = "version"
|
||||
|
@ -72,11 +62,9 @@ const (
|
|||
)
|
||||
|
||||
var ignore = map[string]struct{}{
|
||||
cfgApplicationName: {},
|
||||
cfgApplicationVersion: {},
|
||||
cfgPeers: {},
|
||||
cmdHelp: {},
|
||||
cmdVersion: {},
|
||||
cfgPeers: {},
|
||||
cmdHelp: {},
|
||||
cmdVersion: {},
|
||||
}
|
||||
|
||||
func settings() *viper.Viper {
|
||||
|
@ -111,20 +99,10 @@ func settings() *viper.Viper {
|
|||
flags.String(cfgTLSKey, "", "TLS key path")
|
||||
peers := flags.StringArrayP(cfgPeers, "p", nil, "NeoFS nodes")
|
||||
|
||||
// set prefers:
|
||||
v.Set(cfgApplicationName, "neofs-http-gw")
|
||||
v.Set(cfgApplicationVersion, Version)
|
||||
|
||||
// set defaults:
|
||||
|
||||
// logger:
|
||||
v.SetDefault(cfgLoggerLevel, "debug")
|
||||
v.SetDefault(cfgLoggerFormat, "console")
|
||||
v.SetDefault(cfgLoggerTraceLevel, "panic")
|
||||
v.SetDefault(cfgLoggerNoCaller, false)
|
||||
v.SetDefault(cfgLoggerNoDisclaimer, true)
|
||||
v.SetDefault(cfgLoggerSamplingInitial, 1000)
|
||||
v.SetDefault(cfgLoggerSamplingThereafter, 1000)
|
||||
|
||||
// web-server:
|
||||
v.SetDefault(cfgWebReadBufferSize, 4096)
|
||||
|
|
|
@ -7,14 +7,13 @@ import (
|
|||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
||||
"github.com/nspcc-dev/neofs-http-gw/utils"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/logger"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
log, err := logger.New()
|
||||
require.NoError(t, err)
|
||||
log := zap.NewNop()
|
||||
|
||||
req := &fasthttp.RequestHeader{}
|
||||
req.DisableNormalizing()
|
||||
|
|
Loading…
Reference in a new issue