[#137] Drop sdk logger

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-03-25 16:06:33 +03:00 committed by Alex Vanin
parent d1bdef6f83
commit d891c13cb3
6 changed files with 60 additions and 76 deletions

12
app.go
View file

@ -14,12 +14,10 @@ import (
"github.com/nspcc-dev/neofs-http-gw/downloader" "github.com/nspcc-dev/neofs-http-gw/downloader"
"github.com/nspcc-dev/neofs-http-gw/response" "github.com/nspcc-dev/neofs-http-gw/response"
"github.com/nspcc-dev/neofs-http-gw/uploader" "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/nspcc-dev/neofs-sdk-go/pool"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc/grpclog"
) )
type ( type (
@ -27,7 +25,6 @@ type (
log *zap.Logger log *zap.Logger
pool *pool.Pool pool *pool.Pool
cfg *viper.Viper cfg *viper.Viper
auxiliaryLog logger.Logger
webServer *fasthttp.Server webServer *fasthttp.Server
webDone chan struct{} webDone chan struct{}
} }
@ -77,10 +74,7 @@ func newApp(ctx context.Context, opt ...Option) App {
for i := range opt { for i := range opt {
opt[i](a) opt[i](a)
} }
a.auxiliaryLog = logger.GRPC(a.log)
if a.cfg.GetBool(cmdVerbose) {
grpclog.SetLoggerV2(a.auxiliaryLog)
}
// -- setup FastHTTP server -- // -- setup FastHTTP server --
a.webServer.Name = "neofs-http-gw" a.webServer.Name = "neofs-http-gw"
a.webServer.ReadBufferSize = a.cfg.GetInt(cfgWebReadBufferSize) 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() { 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 <-a.webDone // wait for web-server to be stopped
} }
@ -220,7 +214,7 @@ func (a *app) Serve(ctx context.Context) {
// enable metrics // enable metrics
if a.cfg.GetBool(cmdMetrics) { if a.cfg.GetBool(cmdMetrics) {
a.log.Info("added path /metrics/") a.log.Info("added path /metrics/")
attachMetrics(r, a.auxiliaryLog) attachMetrics(r, a.log)
} }
// enable pprof // enable pprof
if a.cfg.GetBool(cmdPprof) { if a.cfg.GetBool(cmdPprof) {

1
go.mod
View file

@ -24,6 +24,5 @@ require (
go.uber.org/zap v1.18.1 go.uber.org/zap v1.18.1
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.5 // 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 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
) )

57
main.go
View file

@ -2,12 +2,13 @@ package main
import ( import (
"context" "context"
"fmt"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/nspcc-dev/neofs-sdk-go/logger"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
) )
func main() { func main() {
@ -21,25 +22,45 @@ func main() {
app.Wait() 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 { func newLogger(v *viper.Viper) *zap.Logger {
options := []logger.Option{ var lvl zapcore.Level
logger.WithLevel(v.GetString(cfgLoggerLevel)), lvlStr := v.GetString(cfgLoggerLevel)
logger.WithTraceLevel(v.GetString(cfgLoggerTraceLevel)), err := lvl.UnmarshalText([]byte(lvlStr))
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...)
if err != nil { 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 return l
} }

View file

@ -9,21 +9,14 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap"
) )
func attachMetrics(r *router.Router, z promhttp.Logger) { func attachMetrics(r *router.Router, l *zap.Logger) {
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, promhttp.HandlerOpts{ r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, l, promhttp.HandlerOpts{}))
ErrorLog: z,
// ErrorHandling: 0,
// Registry: nil,
// DisableCompression: false,
// MaxRequestsInFlight: 0,
// Timeout: 0,
// EnableOpenMetrics: false,
}))
} }
func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp.RequestHandler { func metricsHandler(reg prometheus.Gatherer, logger *zap.Logger, opts promhttp.HandlerOpts) fasthttp.RequestHandler {
var ( var (
inFlightSem chan struct{} inFlightSem chan struct{}
errCnt = prometheus.NewCounterVec( errCnt = prometheus.NewCounterVec(
@ -66,7 +59,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
} }
mfs, err := reg.Gather() mfs, err := reg.Gather()
if err != nil { if err != nil {
if opts.ErrorLog != nil { if logger != nil {
panic("error gathering metrics:" + err.Error()) panic("error gathering metrics:" + err.Error())
} }
@ -99,8 +92,8 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
return false return false
} }
lastErr = err lastErr = err
if opts.ErrorLog != nil { if logger != nil {
opts.ErrorLog.Println("error encoding and sending metric family:", err) logger.Error("encoding and sending metric family", zap.Error(err))
} }
errCnt.WithLabelValues("encoding").Inc() errCnt.WithLabelValues("encoding").Inc()
switch opts.ErrorHandling { switch opts.ErrorHandling {

View file

@ -37,12 +37,6 @@ const (
// Logger. // Logger.
cfgLoggerLevel = "logger.level" 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"
// Wallet. // Wallet.
cfgWalletPassphrase = "wallet.passphrase" cfgWalletPassphrase = "wallet.passphrase"
@ -56,10 +50,6 @@ const (
// Zip compression. // Zip compression.
cfgZipCompression = "zip.compression" cfgZipCompression = "zip.compression"
// Application.
cfgApplicationName = "app.name"
cfgApplicationVersion = "app.version"
// Command line args. // Command line args.
cmdHelp = "help" cmdHelp = "help"
cmdVersion = "version" cmdVersion = "version"
@ -72,8 +62,6 @@ const (
) )
var ignore = map[string]struct{}{ var ignore = map[string]struct{}{
cfgApplicationName: {},
cfgApplicationVersion: {},
cfgPeers: {}, cfgPeers: {},
cmdHelp: {}, cmdHelp: {},
cmdVersion: {}, cmdVersion: {},
@ -111,20 +99,10 @@ func settings() *viper.Viper {
flags.String(cfgTLSKey, "", "TLS key path") flags.String(cfgTLSKey, "", "TLS key path")
peers := flags.StringArrayP(cfgPeers, "p", nil, "NeoFS nodes") peers := flags.StringArrayP(cfgPeers, "p", nil, "NeoFS nodes")
// set prefers:
v.Set(cfgApplicationName, "neofs-http-gw")
v.Set(cfgApplicationVersion, Version)
// set defaults: // set defaults:
// logger: // logger:
v.SetDefault(cfgLoggerLevel, "debug") 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: // web-server:
v.SetDefault(cfgWebReadBufferSize, 4096) v.SetDefault(cfgWebReadBufferSize, 4096)

View file

@ -7,14 +7,13 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/object" "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-http-gw/utils" "github.com/nspcc-dev/neofs-http-gw/utils"
"github.com/nspcc-dev/neofs-sdk-go/logger"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap"
) )
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {
log, err := logger.New() log := zap.NewNop()
require.NoError(t, err)
req := &fasthttp.RequestHeader{} req := &fasthttp.RequestHeader{}
req.DisableNormalizing() req.DisableNormalizing()