From 8bb35fe74b23933e89ce01ad8a751eb8e3aeb10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?okhowang=28=E7=8E=8B=E6=B2=9B=E6=96=87=29?= Date: Tue, 27 May 2025 13:01:16 +0800 Subject: [PATCH 1/2] fix(otelzap): write core.ctx only when init --- CHANGELOG.md | 4 ++++ bridges/otelzap/core.go | 5 +++-- bridges/otelzap/core_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31603887631..f796a8d44ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Fixed + +- - Fix data race when writing log entries with `context.Context` fields in `go.opentelemetry.io/contrib/bridges/otelzap`. (#7368) + diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index f3624042790..2aebe6427cb 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -223,10 +223,11 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { if ent.Stack != "" { r.AddAttributes(log.String(string(semconv.CodeStacktraceKey), ent.Stack)) } + emitCtx := o.ctx if len(fields) > 0 { ctx, attrbuf := convertField(fields) if ctx != nil { - o.ctx = ctx + emitCtx = ctx } r.AddAttributes(attrbuf...) } @@ -235,7 +236,7 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { if ent.LoggerName != "" { logger = o.provider.Logger(ent.LoggerName, o.opts...) } - logger.Emit(o.ctx, r) + logger.Emit(emitCtx, r) return nil } diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 8097780a652..3e28b89846e 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -5,6 +5,7 @@ package otelzap import ( "context" + "sync" "testing" "github.com/stretchr/testify/assert" @@ -144,6 +145,38 @@ func TestCore(t *testing.T) { }) } +func TestCoreConcurrentSafe(t *testing.T) { + rec := logtest.NewRecorder() + zc := NewCore(loggerName, WithLoggerProvider(rec)) + logger := zap.New(zc) + + t.Run("Write", func(t *testing.T) { + var wg sync.WaitGroup + const n = 2 + wg.Add(n) + ctx := context.Background() + for i := 0; i < n; i++ { + go func() { + defer wg.Done() + logger.Info(testMessage, zap.String(testKey, testValue), zap.Any("ctx", ctx)) + }() + } + wg.Wait() + + result := rec.Result() + require.Len(t, result, 1) + require.Len(t, result[logtest.Scope{Name: "name"}], 2) + got := result[logtest.Scope{Name: "name"}][0] + + assert.Equal(t, testMessage, got.Body.AsString()) + assert.Equal(t, log.SeverityInfo, got.Severity) + assert.Equal(t, zap.InfoLevel.String(), got.SeverityText) + assert.Equal(t, []log.KeyValue{ + log.String(testKey, testValue), + }, got.Attributes) + }) +} + func TestCoreEnabled(t *testing.T) { enabledFunc := func(c context.Context, param log.EnabledParameters) bool { return param.Severity >= log.SeverityInfo From f4a5cffeca1b14211ef1525da1cb5cd3ccbbaefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Tue, 27 May 2025 16:19:03 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f796a8d44ec..6e4f1dc449d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed -- - Fix data race when writing log entries with `context.Context` fields in `go.opentelemetry.io/contrib/bridges/otelzap`. (#7368) +- Fix data race when writing log entries with `context.Context` fields in `go.opentelemetry.io/contrib/bridges/otelzap`. (#7368)