Skip to content

Commit 6668f55

Browse files
authored
[#181]: feature: standardize logger used in endure and RR
2 parents 6ec599c + a6e0602 commit 6668f55

File tree

13 files changed

+246
-69
lines changed

13 files changed

+246
-69
lines changed

.golangci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
4141
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
4242
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
4343
- exhaustive # check exhaustiveness of enum switch statements
44-
- exportloopref # checks for pointers to enclosing loop variables
44+
- copyloopvar # checks for pointers to enclosing loop variables
4545
- gochecknoinits # Checks that no init functions are present in Go code
46-
- goconst # Finds repeated strings that could be replaced by a constant
4746
- gocritic # The most opinionated Go source code linter
4847
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
4948
- revive # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes

edges.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package endure
22

33
import (
4-
"log/slog"
54
"reflect"
65

76
"github.com/roadrunner-server/endure/v2/graph"
87
"github.com/roadrunner-server/errors"
8+
"go.uber.org/zap"
99
)
1010

1111
func (e *Endure) resolveCollectorEdges(plugin any) error {
@@ -20,13 +20,10 @@ func (e *Endure) resolveCollectorEdges(plugin any) error {
2020
if len(res) > 0 {
2121
for j := 0; j < len(res); j++ {
2222
e.graph.AddEdge(graph.CollectsConnection, res[j].Plugin(), plugin)
23-
/*
24-
Here we need to init the
25-
*/
2623
e.log.Debug("collects edge found",
27-
slog.Any("method", res[j].Method()),
28-
slog.Any("src", e.graph.VertexById(res[j].Plugin()).ID().String()),
29-
slog.Any("dest", e.graph.VertexById(plugin).ID().String()))
24+
zap.String("method", res[j].Method()),
25+
zap.String("src", e.graph.VertexById(res[j].Plugin()).ID().String()),
26+
zap.String("dest", e.graph.VertexById(plugin).ID().String()))
3027
}
3128
}
3229
}
@@ -35,7 +32,7 @@ func (e *Endure) resolveCollectorEdges(plugin any) error {
3532
}
3633

3734
// resolveEdges adds edges between the vertices
38-
// At this point, we know all plugins, and all provides values
35+
// At this point, we know all plugins and all 'provides' values
3936
func (e *Endure) resolveEdges() error {
4037
vertices := e.graph.Vertices()
4138

@@ -51,8 +48,8 @@ func (e *Endure) resolveEdges() error {
5148
if isPrimitive(initMethod.Type.In(j).String()) {
5249
e.log.Error(
5350
"primitive type in the function parameters",
54-
slog.String("plugin", vertices[i].ID().String()),
55-
slog.String("type", initMethod.Type.In(j).String()),
51+
zap.String("plugin", vertices[i].ID().String()),
52+
zap.String("type", initMethod.Type.In(j).String()),
5653
)
5754

5855
return errors.E("Init method should not receive primitive types (like string, int, etc). It should receive only interfaces")
@@ -81,22 +78,22 @@ func (e *Endure) resolveEdges() error {
8178
// log
8279
e.log.Debug(
8380
"init edge found",
84-
slog.Any("src", e.graph.VertexById(res[k].Plugin()).ID().String()),
85-
slog.Any("dest", e.graph.VertexById(vertex.Plugin()).ID().String()),
81+
zap.Any("src", e.graph.VertexById(res[k].Plugin()).ID().String()),
82+
zap.Any("dest", e.graph.VertexById(vertex.Plugin()).ID().String()),
8683
)
8784
}
8885
}
8986
}
9087

91-
// we should have here exact the same number of the deps implementing every particular arg
88+
// we should have here exactly the same number of the deps implementing every particular arg
9289
if count != len(args[1:]) {
9390
// if there are no plugins that implement Init deps, remove this vertex from the tree
9491
del := e.graph.Remove(vertices[i].Plugin())
9592
for k := 0; k < len(del); k++ {
9693
e.registar.Remove(del[k].Plugin())
9794
e.log.Debug(
9895
"plugin disabled, not enough Init dependencies",
99-
slog.String("name", del[k].ID().String()),
96+
zap.String("name", del[k].ID().String()),
10097
)
10198
}
10299

@@ -117,8 +114,8 @@ func (e *Endure) resolveEdges() error {
117114

118115
e.graph.TopologicalSort()
119116

120-
// notify user about the disabled plugins
121-
// after topological sorting we remove all plugins with indegree > 0, because there are no edges to them
117+
// to notify user about the disabled plugins
118+
// after topological sorting, we remove all plugins with indegree > 0, because there are no edges to them
122119
if len(e.graph.TopologicalOrder()) != len(e.graph.Vertices()) {
123120
tpl := e.graph.TopologicalOrder()
124121
vrt := e.graph.Vertices()
@@ -130,7 +127,7 @@ func (e *Endure) resolveEdges() error {
130127

131128
for _, v := range vrt {
132129
if _, ok := tmpM[v.ID().String()]; !ok {
133-
e.log.Warn("topological sort, plugin disabled", slog.String("plugin", v.ID().String()))
130+
e.log.Warn("topological sort, plugin disabled", zap.String("plugin", v.ID().String()))
134131
}
135132
}
136133
}

endure.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"net/http"
66
// pprof will be enabled in debug mode
77
"net/http/pprof"
8-
"os"
98
"reflect"
109
"sync"
1110
"time"
1211

1312
"github.com/roadrunner-server/endure/v2/graph"
13+
"github.com/roadrunner-server/endure/v2/logger"
1414
"github.com/roadrunner-server/endure/v2/registar"
1515
"github.com/roadrunner-server/errors"
16+
"go.uber.org/zap"
1617
)
1718

1819
// Endure struct represent main endure repr
@@ -24,7 +25,7 @@ type Endure struct {
2425
// Dependency graph
2526
graph *graph.Graph
2627
// log
27-
log *slog.Logger
28+
log *zap.Logger
2829
stopTimeout time.Duration
2930
profiler bool
3031
visualize bool
@@ -34,7 +35,7 @@ type Endure struct {
3435
userResultsCh chan *Result
3536
}
3637

37-
// Options is the endure options
38+
// Options is the 'endure' options
3839
type Options func(endure *Endure)
3940

4041
// New returns empty endure container
@@ -43,11 +44,15 @@ func New(level slog.Leveler, options ...Options) *Endure {
4344
level = slog.LevelDebug
4445
}
4546

47+
// error handling is omitted because we are sure that the logger will be created
48+
zlog, _ := logger.BuildLogger(level)
49+
4650
c := &Endure{
4751
registar: registar.New(),
4852
graph: graph.New(),
4953
mu: sync.RWMutex{},
5054
stopTimeout: time.Second * 30,
55+
log: zlog,
5156
}
5257

5358
// Main thread channels
@@ -59,14 +64,6 @@ func New(level slog.Leveler, options ...Options) *Endure {
5964
option(c)
6065
}
6166

62-
// create default logger if not already defined in the provided options
63-
if c.log == nil {
64-
opts := &slog.HandlerOptions{
65-
Level: level,
66-
}
67-
c.log = slog.New(slog.NewJSONHandler(os.Stderr, opts))
68-
}
69-
7067
// start profiler server
7168
if c.profiler {
7269
profile()
@@ -97,7 +94,7 @@ func (e *Endure) Register(vertex any) error {
9794
*/
9895

9996
if e.graph.HasVertex(vertex) {
100-
e.log.Warn("already registered", slog.Any("error", errors.Errorf("plugin `%s` is already registered", t.String())))
97+
e.log.Warn("already registered", zap.Error(errors.Errorf("plugin `%s` is already registered", t.String())))
10198
return nil
10299
}
103100

@@ -106,9 +103,9 @@ func (e *Endure) Register(vertex any) error {
106103
weight = val.Weight()
107104
e.log.Debug(
108105
"weight added",
109-
slog.String("type", reflect.TypeOf(vertex).Elem().String()),
110-
slog.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
111-
slog.Uint64("value", uint64(weight)),
106+
zap.String("type", reflect.TypeOf(vertex).Elem().String()),
107+
zap.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
108+
zap.Uint64("value", uint64(weight)),
112109
)
113110
}
114111

@@ -119,9 +116,9 @@ func (e *Endure) Register(vertex any) error {
119116

120117
e.log.Debug(
121118
"type registered",
122-
slog.String("type", reflect.TypeOf(vertex).Elem().String()),
123-
slog.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
124-
slog.String("method", "plugin"),
119+
zap.String("type", reflect.TypeOf(vertex).Elem().String()),
120+
zap.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
121+
zap.String("method", "plugin"),
125122
)
126123

127124
/*
@@ -142,9 +139,9 @@ func (e *Endure) Register(vertex any) error {
142139
e.registar.Insert(vertex, outDeps[i].Type, outDeps[i].Method, weight)
143140
e.log.Debug(
144141
"provided type registered",
145-
slog.String("type", outDeps[i].Type.String()),
146-
slog.String("kind", outDeps[i].Type.Kind().String()),
147-
slog.String("method", outDeps[i].Method),
142+
zap.String("type", outDeps[i].Type.String()),
143+
zap.String("kind", outDeps[i].Type.Kind().String()),
144+
zap.String("method", outDeps[i].Method),
148145
)
149146
}
150147
}

go.mod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,15 @@ go 1.23
44

55
toolchain go1.23.0
66

7-
require github.com/roadrunner-server/errors v1.4.1
7+
require (
8+
github.com/fatih/color v1.17.0
9+
github.com/roadrunner-server/errors v1.4.1
10+
go.uber.org/zap v1.27.0
11+
)
12+
13+
require (
14+
github.com/mattn/go-colorable v0.1.13 // indirect
15+
github.com/mattn/go-isatty v0.0.20 // indirect
16+
go.uber.org/multierr v1.10.0 // indirect
17+
golang.org/x/sys v0.18.0 // indirect
18+
)

go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
4+
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
5+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
9+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
112
github.com/roadrunner-server/errors v1.4.1 h1:LKNeaCGiwd3t8IaL840ZNF3UA9yDQlpvHnKddnh0YRQ=
213
github.com/roadrunner-server/errors v1.4.1/go.mod h1:qeffnIKG0e4j1dzGpa+OGY5VKSfMphizvqWIw8s2lAo=
14+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
15+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
16+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
17+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
18+
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
19+
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
20+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
21+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
22+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
25+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
26+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

go.work.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
22
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
44
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
5+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
6+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
57
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
68
github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
79
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=

graph/vertex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55
)
66

7-
// Vertex is main vertex representation for the graph
7+
// Vertex is the main vertex representation for the graph
88
// since we can have cyclic dependencies
99
// when we traverse the VerticesMap, we should mark nodes as visited or not to detect cycle
1010
type Vertex struct {

init.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package endure
22

33
import (
4-
"log/slog"
54
"reflect"
65

76
"github.com/roadrunner-server/errors"
7+
"go.uber.org/zap"
88
)
99

1010
func (e *Endure) init() error {
@@ -33,7 +33,7 @@ func (e *Endure) init() error {
3333
inVals = append(inVals, reflect.ValueOf(vertices[i].Plugin()))
3434
// has deps if > 1
3535
if len(args) > 1 {
36-
// exclude first arg (it's receiver)
36+
// exclude first arg (its receiver)
3737
arg := args[1:]
3838
for j := 0; j < len(arg); j++ {
3939
plugin := e.registar.ImplementsExcept(arg[j], vertices[i].Plugin())
@@ -43,15 +43,15 @@ func (e *Endure) init() error {
4343
e.registar.Remove(del[k].Plugin())
4444
e.log.Debug(
4545
"plugin disabled, not enough Init dependencies",
46-
slog.String("name", del[k].ID().String()),
46+
zap.String("name", del[k].ID().String()),
4747
)
4848
}
4949

5050
continue
5151
}
5252

5353
// check if the provided plugin dep has a method
54-
// existence of the method indicates, that the dep provided by this plugin should be obtained via the method call
54+
// existence of the method indicates that the dep provided by this plugin should be obtained via the method call
5555
switch plugin[0].Method() == "" {
5656
// we don't have a method, that means, plugin itself implements the dep
5757
case true:
@@ -92,15 +92,15 @@ func (e *Endure) init() error {
9292
if errors.Is(errors.Disabled, ret[0].Interface().(error)) {
9393
e.log.Debug(
9494
"plugin disabled",
95-
slog.String("name", vertices[i].ID().String()),
95+
zap.String("name", vertices[i].ID().String()),
9696
)
9797
// delete vertex and continue
9898
plugins := e.graph.Remove(vertices[i].Plugin())
9999

100100
for j := 0; j < len(plugins); j++ {
101101
e.log.Debug(
102102
"destination plugin disabled because root was disabled",
103-
slog.String("name", plugins[j].ID().String()),
103+
zap.String("name", plugins[j].ID().String()),
104104
)
105105
e.registar.Remove(plugins[j].Plugin())
106106
}

0 commit comments

Comments
 (0)