Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
c.tempLabels.mutex.Unlock()
lbls.mutex.RUnlock()

fields = append(fields, labelsField(c.allLabels()))
fields = mergeLabelFields(fields, c.allLabels())
fields = c.withSourceLocation(ent, fields)
if c.config.ServiceName != "" {
fields = c.withServiceContext(c.config.ServiceName, fields)
Expand Down
22 changes: 22 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ func TestWrite(t *testing.T) {
assert.NotNil(t, logs.All()[0].ContextMap()[labelsKey])
}

// ref: https://github.com/blendle/zapdriver/issues/29
func TestWriteDuplicateLabels(t *testing.T) {
debugcore, logs := observer.New(zapcore.DebugLevel)
core := &core{
Core: debugcore,
permLabels: newLabels(),
tempLabels: newLabels(),
}

fields := []zap.Field{
Labels(
Label("hello", "world"),
Label("hi", "universe"),
),
}

err := core.Write(zapcore.Entry{}, fields)
require.NoError(t, err)

assert.Len(t, logs.All()[0].Context, 1)
}

func TestWriteConcurrent(t *testing.T) {
temp := newLabels()
temp.store = map[string]string{"one": "1", "two": "2"}
Expand Down
23 changes: 23 additions & 0 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ func labelsField(l *labels) zap.Field {
return zap.Object(labelsKey, l)
}

func mergeLabelFields(fields []zap.Field, newLabels *labels) []zap.Field {
for i := range fields {
lbls, ok := fields[i].Interface.(*labels)
if !ok {
continue
}

newLabels.mutex.RLock()
defer newLabels.mutex.RUnlock()

lbls.mutex.Lock()
defer lbls.mutex.Unlock()

for k, v := range newLabels.store {
lbls.store[k] = v
}

return fields
}

return append(fields, labelsField(newLabels))
}

type labels struct {
store map[string]string
mutex *sync.RWMutex
Expand Down