Skip to content

Commit cd522c5

Browse files
authored
feat(beholder): add logger field to otel (#1987)
1 parent 8bdbd9f commit cd522c5

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

pkg/logger/otelzap/otelzap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func (o OtelZapCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
110110
attributes = append(attributes, attribute.String("caller", entry.Caller.String()))
111111
}
112112

113+
// Add zap logger name when present
114+
if entry.LoggerName != "" {
115+
attributes = append(attributes, attribute.String("logger", entry.LoggerName))
116+
}
117+
113118
// Add exception metadata for error levels
114119
if entry.Level > zapcore.InfoLevel {
115120
if entry.Stack != "" {

pkg/logger/otelzap/otelzap_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ func TestOtelZapCore_Write(t *testing.T) {
189189
core := NewCore(logger).(*OtelZapCore)
190190

191191
tests := []struct {
192-
name string
193-
entry zapcore.Entry
194-
fields []zapcore.Field
195-
coreFields []zapcore.Field
196-
wantMessage string
197-
wantAttrs map[string]string
192+
name string
193+
entry zapcore.Entry
194+
fields []zapcore.Field
195+
coreFields []zapcore.Field
196+
wantMessage string
197+
wantAttrs map[string]string
198+
mustLackAttrs []string
198199
}{
199200
{
200201
name: "basic info log",
@@ -206,8 +207,9 @@ func TestOtelZapCore_Write(t *testing.T) {
206207
fields: []zapcore.Field{
207208
{Key: "foo", Type: zapcore.StringType, String: "bar"},
208209
},
209-
wantMessage: "hello world",
210-
wantAttrs: map[string]string{"foo": "bar"},
210+
wantMessage: "hello world",
211+
wantAttrs: map[string]string{"foo": "bar"},
212+
mustLackAttrs: []string{"logger"},
211213
},
212214
{
213215
name: "error log with stack and caller",
@@ -227,6 +229,7 @@ func TestOtelZapCore_Write(t *testing.T) {
227229
"caller": "file.go:42",
228230
"exception.stacktrace": "stacktrace",
229231
},
232+
mustLackAttrs: []string{"logger"},
230233
},
231234
{
232235
name: "core fields and span context",
@@ -252,6 +255,18 @@ func TestOtelZapCore_Write(t *testing.T) {
252255
"span_id": "0405060000000000",
253256
"trace_flags": "01",
254257
},
258+
mustLackAttrs: []string{"logger"},
259+
},
260+
{
261+
name: "logger name is emitted",
262+
entry: zapcore.Entry{
263+
Message: "named",
264+
Level: zapcore.InfoLevel,
265+
Time: time.Now(),
266+
LoggerName: "svc.sub",
267+
},
268+
wantMessage: "named",
269+
wantAttrs: map[string]string{"logger": "svc.sub"},
255270
},
256271
}
257272

@@ -292,6 +307,9 @@ func TestOtelZapCore_Write(t *testing.T) {
292307
assert.Contains(t, got, k, "expected key %q", k)
293308
assert.Equal(t, v, got[k], "mismatch for key %q", k)
294309
}
310+
for _, k := range tt.mustLackAttrs {
311+
assert.NotContains(t, got, k, "unexpected attribute %q", k)
312+
}
295313
})
296314
}
297315
}

0 commit comments

Comments
 (0)