Skip to content

Commit

Permalink
Develop (#15)
Browse files Browse the repository at this point in the history
* add client grpc interceptor

* add draft for slog logger

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix error

* fix register

* change call string

* Update metrics/grpc_mw_client.go

Co-authored-by: Mikhail Barshev <[email protected]>

* fix mrs

* go to zero log, delete logrus (#14)

* go to zero log, delete logrus

* fix mr error

---------

Co-authored-by: Mikhail Barshev <[email protected]>
  • Loading branch information
glebnaz and mike-barshev authored Nov 28, 2023
1 parent 46b9e11 commit dd1fda7
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 969 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"time"

"github.com/glebnaz/witcher/engine"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions db/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/glebnaz/witcher/engine"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"

driver "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -40,7 +40,7 @@ func (m *Mongo) Closer() engine.Closer {
defer group.Done()
err := m.Disconnect(ctx)
if err != nil {
log.Debugf("Error disconnect mongo: %s", err)
log.Debug().Msgf("Error disconnect mongo: %s", err)
return err
}
return nil
Expand Down
23 changes: 11 additions & 12 deletions engine/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"time"

"github.com/glebnaz/witcher/metrics"

"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)

type DebugServer struct {
Expand Down Expand Up @@ -51,14 +50,14 @@ func (d *DebugServer) SetReady(ready bool) {
defer d.m.Unlock()
d.ready = ready
if ready {
log.Infof("Server set ready")
log.Info().Msg("Server set ready")
} else {
log.Infof("Server is not ready")
log.Info().Msg("Server is not ready")
}
}

func (d *DebugServer) AddChecker(checker Checker) {
log.Debugf("Adding checker %s", checker.Name())
log.Debug().Msgf("Adding checker %s", checker.Name())
d.m.Lock()
defer d.m.Unlock()
d.checkers = append(d.checkers, checker)
Expand All @@ -73,7 +72,7 @@ func (d *DebugServer) AddCheckers(checkers []Checker) {

// Live is probe checker
func (d *DebugServer) Live(c echo.Context) error {
log.Infof("Live check at %s", time.Now())
log.Info().Msgf("Live check at %s", time.Now())
d.m.Lock()
defer d.m.Unlock()

Expand All @@ -83,7 +82,7 @@ func (d *DebugServer) Live(c echo.Context) error {

for i := range d.checkers {
if err := d.checkers[i].Check(); err != nil {
log.Errorf("Server is not live: %s", err)
log.Error().Msgf("Server is not live: %s", err)
live = false
info[d.checkers[i].Name()] = false
} else {
Expand All @@ -92,32 +91,32 @@ func (d *DebugServer) Live(c echo.Context) error {
}

if !live {
log.Errorf("Server is not live")
log.Error().Msgf("Server is not live")
return c.JSON(http.StatusInternalServerError, info)
}
return c.JSON(http.StatusOK, info)
}

// Ready is probe checker
func (d *DebugServer) Ready(c echo.Context) error {
log.Infof("Ready check at %s", time.Now())
log.Info().Msgf("Ready check at %s", time.Now())
if d.ready {
return c.String(http.StatusOK, "OK")
}
return c.String(http.StatusInternalServerError, "Not ready")
}

func (d *DebugServer) RunDebug() error {
log.Infof("Run debug server at %s", time.Now())
log.Info().Msgf("Run debug server at %s", time.Now())
return d.engine.Start(d.PORT)
}

func (d *DebugServer) ShutdownDebug(ctx context.Context) error {
log.Infof("Start shutdown debug server at %s", time.Now())
log.Info().Msgf("Start shutdown debug server at %s", time.Now())
errShutDown := d.engine.Shutdown(ctx)
if errShutDown != nil {
return errShutDown
}
log.Debugf("Shutdown debug server success")
log.Debug().Msg("Shutdown debug server success")
return nil
}
34 changes: 17 additions & 17 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/kelseyhightower/envconfig"
"github.com/oklog/run"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)

type engineCfg struct {
Expand Down Expand Up @@ -84,7 +84,7 @@ type Server struct {

// Run start your server
func (s *Server) Run() error {
log.Infof("Starting ...")
log.Info().Msgf("Starting ...")

//add debug server
s.AddActor(func() error {
Expand All @@ -95,7 +95,7 @@ func (s *Server) Run() error {
return nil
}, func(err error) {
if err != nil {
log.Fatalf("Error Run Debug Server: %s", err)
log.Fatal().Msgf("Error Run Debug Server: %s", err)
}
})

Expand All @@ -117,13 +117,13 @@ func (s *Server) Run() error {
<-quit

//start shutdown
log.Infof("Start shutdown server")
log.Info().Msgf("Start shutdown server")
err := s.Shutdown()
if err != nil {
log.Errorf("Error Shutdown Server: %s", err)
log.Error().Msgf("Error Shutdown Server: %s", err)
return err
}
log.Infof("Shutdown server gracefully")
log.Info().Msgf("Shutdown server gracefully")
return nil
}

Expand All @@ -143,7 +143,7 @@ func (s *Server) runRunGroup() {
go func() {
err := s.runGroup.Run()
if err != nil {
log.Errorf("Error runGroup Server: %s", err)
log.Error().Msgf("Error runGroup Server: %s", err)
s.setErrRunGroup(err)
}
}()
Expand All @@ -160,7 +160,7 @@ func (s *Server) runRunGroup() {
func (s *Server) AddActor(execute func() error, interrupt func(err error)) {
s.m.Lock()
defer s.m.Unlock()
log.Debugf("Add New Actor")
log.Debug().Msgf("Add New Actor")
s.runGroup.Add(execute, interrupt)
}

Expand All @@ -169,7 +169,7 @@ func (s *Server) AddActor(execute func() error, interrupt func(err error)) {
func (s *Server) AddCloser(closer Closer) {
s.m.Lock()
defer s.m.Unlock()
log.Debugf("AddCloser %s", closer.GetName())
log.Debug().Msgf("AddCloser %s", closer.GetName())
s.closerGroup = append(s.closerGroup, closer)
}

Expand All @@ -187,7 +187,7 @@ func (s *Server) AddClosers(closers []Closer) {
func (s *Server) Shutdown() error {
s.m.Lock()
defer s.m.Unlock()
log.Debugf("Shutdown with timeout - %s", s.shutdownTimeout.String())
log.Debug().Msgf("Shutdown with timeout - %s", s.shutdownTimeout.String())
s.SetReady(false)
ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer cancel()
Expand All @@ -196,15 +196,15 @@ func (s *Server) Shutdown() error {

err := s.ShutdownDebug(ctx)
if err != nil {
log.Errorf("Error Shutdown Debug Server: %s", err)
log.Error().Msgf("Error Shutdown Debug Server: %s", err)
}

quit := make(chan struct{})
go func() {
err = s.closeClosers(ctx)
if err != nil {
quit <- struct{}{}
log.Errorf("Error Close Closers: %s", err)
log.Error().Msgf("Error Close Closers: %s", err)
return
}
quit <- struct{}{}
Expand All @@ -215,7 +215,7 @@ func (s *Server) Shutdown() error {
case <-quit:
return err
case <-ctx.Done():
log.Debugf("force shutdown")
log.Debug().Msgf("force shutdown")
return errors.New("force quit")
}
}
Expand All @@ -225,21 +225,21 @@ func (s *Server) closeClosers(ctx context.Context) error {
var wg sync.WaitGroup
var errMsg []string
for _, closer := range s.closerGroup {
log.Debugf("Close closer: %s", closer.GetName())
log.Debug().Msgf("Close closer: %s", closer.GetName())
wg.Add(1)
go func(c Closer) {
err := c.Close(ctx, &wg)
if err != nil {
errMsg = append(errMsg, fmt.Sprintf("Error Close Closer %s: %s", c.GetName(), err))
log.Errorf("Error Close Closer %s: %s", c.GetName(), err)
log.Error().Msgf("Error Close Closer %s: %s", c.GetName(), err)
}
}(closer)
}
wg.Wait()
if len(errMsg) > 0 {
return errors.New(strings.Join(errMsg, "\n"))
}
log.Debug("Close all closers gracefully")
log.Debug().Msg("Close all closers gracefully")
return nil
}

Expand All @@ -261,7 +261,7 @@ func NewServer(opt ...ServerOpt) *Server {

err := envconfig.Process("", &cfg)
if err != nil {
log.Errorf("err read engine config: %s", err)
log.Error().Msgf("err read engine config: %s", err)
panic(err)
}

Expand Down
12 changes: 6 additions & 6 deletions engine/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)

Expand All @@ -30,11 +30,11 @@ func (b *grpcServerRunner) actor() (func() error, func(error)) {
if err != nil {
return errors.Wrap(err, "grpc listener init failure")
}
log.Infof("grpc server: started on %s port", b.port)
log.Info().Msgf("grpc server: started on %s port", b.port)

err = errors.Wrap(b.server.Serve(listener), "grpc server")
if err != nil {
log.Errorf("grpc serve error: %s", err)
log.Error().Msgf("grpc serve error: %s", err)
}
return err
}, func(err error) {
Expand All @@ -46,11 +46,11 @@ func (b *grpcServerRunner) actor() (func() error, func(error)) {

select {
case <-time.After(b.gracefulShutdownTimeout):
log.Error(errors.Wrap(context.DeadlineExceeded, "grpc server graceful stop timed out"))
log.Error().Err(errors.Wrap(context.DeadlineExceeded, "grpc server graceful stop timed out")).Msg("stop server by timeout")
b.server.Stop()
log.Info("grpc server stopped (force)")
log.Info().Msg("grpc server stopped (force)")
case <-doneCh:
log.Info("grpc server: gracefully stopped")
log.Info().Msg("grpc server: gracefully stopped")
}
}
}
9 changes: 4 additions & 5 deletions engine/text.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package engine

import log "github.com/sirupsen/logrus"
import "github.com/rs/zerolog/log"

const (
banner = `
Expand Down Expand Up @@ -38,13 +38,12 @@ const (
===================================================================⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
`

introduce = `Server is running! Your Debug Port is: %s`
introduce = `Никак вы, бл@ть, не научитесь! Your Debug Port is: %s`
)

func (s *Server) introduce() {
log.Infof("Server Is Ready")
log.Infof(introduce, s.PORT)
log.Info().Msgf(introduce, s.PORT)
if !s.disableBanner {
log.Infof(banner)
log.Info().Msg(banner)
}
}
21 changes: 14 additions & 7 deletions example/logger/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package main

import (
"context"
"os"

"github.com/glebnaz/witcher/log"
"github.com/sirupsen/logrus"
zlog "github.com/rs/zerolog/log"
)

func main() {
logrus.SetReportCaller(true)
entry := logrus.WithFields(logrus.Fields{"id": 1})
ctx := log.AddEntryToCTX(context.Background(), entry)
log.InitLog(os.Stdout, true)

log.Infof(ctx, "test with id")
log.Infof(context.Background(), "test without id")
zlog.Debug().Msg("MSG")
}

//func main() {
// log.InitLog(os.Stdout, true)
//
// zlog.Debug().Msg("MSG")
//}

// OUTPUT
// {"level":"info","t":"2023-11-28T21:32:59+01:00","msg":"Inited prod version logger"}
// {"level":"debug","t":"2023-11-28T21:32:59+01:00","msg":"MSG"}
Loading

0 comments on commit dd1fda7

Please sign in to comment.