Skip to content

Commit d9dd081

Browse files
authored
Fix closerCore With behaviour (#210)
This commit fixes the behaviour of `closerCore.With` and add tests to ensure it works as expected. The tests are a modification of existing tests that calls the configuration functions like Beats does and ensures both closerCore and typedCore are used. The method calls get proxyed from one core to another, so both are tested together. The race detector is enabled when testing on Linux and MacOS. Windows requires CGO and installation of gcc on CI, so it is not covered by this commit.
1 parent 440bb15 commit d9dd081

File tree

8 files changed

+405
-38
lines changed

8 files changed

+405
-38
lines changed

.buildkite/scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ with_go_junit_report
1111

1212
echo "--- Go Test"
1313
set +e
14-
go test -v ./... > tests-report.txt
14+
go test -race -v ./... > tests-report.txt
1515
exit_code=$?
1616
set -e
1717

NOTICE.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,11 +1252,11 @@ Contents of probable licence file $GOMODCACHE/github.com/spf13/[email protected]/LICE
12521252

12531253
--------------------------------------------------------------------------------
12541254
Dependency : github.com/stretchr/testify
1255-
Version: v1.8.2
1255+
Version: v1.9.0
12561256
Licence type (autodetected): MIT
12571257
--------------------------------------------------------------------------------
12581258

1259-
Contents of probable licence file $GOMODCACHE/github.com/stretchr/testify@v1.8.2/LICENSE:
1259+
Contents of probable licence file $GOMODCACHE/github.com/stretchr/testify@v1.9.0/LICENSE:
12601260

12611261
MIT License
12621262

@@ -4562,11 +4562,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45624562

45634563
--------------------------------------------------------------------------------
45644564
Dependency : github.com/stretchr/objx
4565-
Version: v0.5.0
4565+
Version: v0.5.2
45664566
Licence type (autodetected): MIT
45674567
--------------------------------------------------------------------------------
45684568

4569-
Contents of probable licence file $GOMODCACHE/github.com/stretchr/[email protected].0/LICENSE:
4569+
Contents of probable licence file $GOMODCACHE/github.com/stretchr/[email protected].2/LICENSE:
45704570

45714571
The MIT License
45724572

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/mitchellh/hashstructure v1.1.0
1616
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
1717
github.com/spf13/cobra v1.7.0
18-
github.com/stretchr/testify v1.8.2
18+
github.com/stretchr/testify v1.9.0
1919
go.elastic.co/apm/module/apmhttp/v2 v2.0.0
2020
go.elastic.co/ecszap v1.0.1
2121
go.elastic.co/go-licence-detector v0.5.0

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,13 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
9090
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
9191
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9292
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
93-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
94-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9593
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
9694
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
9795
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
9896
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9997
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
100-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
101-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
102-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
103-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
98+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
99+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
104100
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
105101
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
106102
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=

logp/core.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"os"
2727
"path/filepath"
2828
"strings"
29-
"sync"
3029
"sync/atomic"
3130
"unsafe"
3231

@@ -67,14 +66,13 @@ type coreLogger struct {
6766
type closerCore struct {
6867
zapcore.Core
6968
io.Closer
70-
mutex sync.Mutex
7169
}
7270

73-
func (c *closerCore) With(fields []zapcore.Field) zapcore.Core {
74-
c.mutex.Lock()
75-
c.Core = c.Core.With(fields)
76-
c.mutex.Unlock()
77-
return c
71+
func (c closerCore) With(fields []zapcore.Field) zapcore.Core {
72+
return closerCore{
73+
Core: c.Core.With(fields),
74+
Closer: c.Closer,
75+
}
7876
}
7977

8078
// Configure configures the logp package.

logp/core_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,70 @@ func TestCoresCanBeClosed(t *testing.T) {
635635
}
636636
}
637637

638+
func TestCloserLoggerCoreWith(t *testing.T) {
639+
defaultWriter := writeSyncer{}
640+
641+
cfg := zap.NewProductionEncoderConfig()
642+
cfg.TimeKey = "" // remove the time to make the log entry consistent
643+
644+
core := closerCore{
645+
Core: zapcore.NewCore(
646+
zapcore.NewJSONEncoder(cfg),
647+
&defaultWriter,
648+
zapcore.InfoLevel,
649+
),
650+
}
651+
652+
expectedLines := []string{
653+
// First/Default logger
654+
`{"level":"info","msg":"Very first message"}`,
655+
656+
// Two messages after calling With
657+
`{"level":"info","msg":"a message with extra fields","foo":"bar"}`,
658+
`{"level":"info","msg":"another message with extra fields","foo":"bar"}`,
659+
660+
// A message with the default logger
661+
`{"level":"info","msg":"a message without extra fields"}`,
662+
663+
// Two more messages with a different field
664+
`{"level":"info","msg":"a message with an answer","answer":"42"}`,
665+
`{"level":"info","msg":"another message with an answer","answer":"42"}`,
666+
667+
// One last message with the default logger
668+
`{"level":"info","msg":"another message without any extra fields"}`,
669+
}
670+
671+
// The default logger, it should not be modified by any call to With.
672+
logger := zap.New(&core)
673+
logger.Info("Very first message")
674+
675+
// Add a field and write messages
676+
loggerWithFields := logger.With(strField("foo", "bar"))
677+
loggerWithFields.Info("a message with extra fields")
678+
loggerWithFields.Info("another message with extra fields")
679+
680+
// Use the default logger again
681+
logger.Info("a message without extra fields")
682+
683+
// New logger with other fields
684+
loggerWithFields = logger.With(strField("answer", "42"))
685+
loggerWithFields.Info("a message with an answer")
686+
loggerWithFields.Info("another message with an answer")
687+
688+
// One last message with the default logger
689+
logger.Info("another message without any extra fields")
690+
691+
scanner := bufio.NewScanner(strings.NewReader(defaultWriter.String()))
692+
count := 0
693+
for scanner.Scan() {
694+
l := scanner.Text()
695+
if l != expectedLines[count] {
696+
t.Error("Expecting:\n", l, "\nGot:\n", expectedLines[count])
697+
}
698+
count++
699+
}
700+
}
701+
638702
func strField(key, val string) zapcore.Field {
639703
return zapcore.Field{Type: zapcore.StringType, Key: key, String: val}
640704
}

0 commit comments

Comments
 (0)