Skip to content

Commit 035ed87

Browse files
authored
{agent, event, log}: Output EmitEvent logs at the trace level (#739)
1 parent 173321d commit 035ed87

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

agent/invocation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func EmitEvent(ctx context.Context, inv *Invocation, ch chan<- *event.Event,
488488
agentName = inv.AgentName
489489
requestID = inv.RunOptions.RequestID
490490
}
491-
log.Debugf("[agent.EmitEvent]queue monitoring:RequestID: %s channel capacity: %d, current length: %d, branch: %s, agent name:%s",
491+
log.Tracef("[agent.EmitEvent]queue monitoring:RequestID: %s channel capacity: %d, current length: %d, branch: %s, agent name:%s",
492492
requestID, cap(ch), len(ch), e.Branch, agentName)
493493
return event.EmitEvent(ctx, ch, e)
494494
}

event/event.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ func EmitEventWithTimeout(ctx context.Context, ch chan<- *Event,
255255
return err
256256
}
257257

258-
log.Debugf("[EmitEventWithTimeout]queue monitoring: RequestID: %s, channel capacity: %d, current length: %d, branch: %s",
258+
log.Tracef("[EmitEventWithTimeout]queue monitoring: RequestID: %s, channel capacity: %d, current length: %d, branch: %s",
259259
e.RequestID, cap(ch), len(ch), e.Branch)
260260

261261
if timeout == EmitWithoutTimeout {
262262
select {
263263
case ch <- e:
264-
log.Debugf("EmitEventWithTimeout: event sent, event: %+v", *e)
264+
log.Tracef("EmitEventWithTimeout: event sent, event: %+v", *e)
265265
case <-ctx.Done():
266266
log.Warnf("EmitEventWithTimeout: context cancelled, event: %+v", *e)
267267
return ctx.Err()
@@ -271,7 +271,7 @@ func EmitEventWithTimeout(ctx context.Context, ch chan<- *Event,
271271

272272
select {
273273
case ch <- e:
274-
log.Debugf("EmitEventWithTimeout: event sent, event: %+v", *e)
274+
log.Tracef("EmitEventWithTimeout: event sent, event: %+v", *e)
275275
case <-ctx.Done():
276276
log.Warnf("EmitEventWithTimeout: context cancelled, event: %+v", *e)
277277
return ctx.Err()

log/level_test.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package log
1212
// ... existing code ...
1313

1414
import (
15+
"strings"
1516
"testing"
1617

1718
"go.uber.org/zap/zapcore"
@@ -42,26 +43,47 @@ func TestSetLevel(t *testing.T) {
4243
}
4344
}
4445

45-
// TestTracef makes sure Tracef forwards the call to the underlying
46-
// logger at the debug level. The test installs a stub logger that
47-
// records the last format string received and asserts that Tracef
48-
// prefixes the message with the required tag.
49-
func TestTracef(t *testing.T) {
50-
var recorded string
51-
stub := &stubLogger{debugf: func(format string, _ ...any) {
52-
recorded = format
53-
}}
46+
// TestTraceDisabledByDefault ensures trace logging starts disabled and Tracef is a no-op.
47+
func TestTraceDisabledByDefault(t *testing.T) {
48+
stub := &stubLogger{}
49+
oldDefault := Default
50+
oldTrace := traceEnabled
51+
Default = stub
52+
t.Cleanup(func() {
53+
Default = oldDefault
54+
traceEnabled = oldTrace
55+
})
56+
57+
if traceEnabled {
58+
t.Fatalf("traceEnabled should be false by default")
59+
}
60+
61+
Tracef("hello %s", "world")
5462

55-
// Replace Default logger with stub and restore afterwards.
56-
old := Default
63+
if stub.debugfCalls != 0 {
64+
t.Fatalf("Tracef should not log when trace is disabled; got %d calls", stub.debugfCalls)
65+
}
66+
}
67+
68+
// TestTracefEnabled makes sure Tracef forwards the call when trace is enabled.
69+
func TestTracefEnabled(t *testing.T) {
70+
stub := &stubLogger{}
71+
oldDefault := Default
72+
oldTrace := traceEnabled
5773
Default = stub
58-
defer func() { Default = old }()
74+
SetTraceEnabled(true)
75+
t.Cleanup(func() {
76+
Default = oldDefault
77+
traceEnabled = oldTrace
78+
})
5979

6080
Tracef("hello %s", "world")
6181

62-
wantPrefix := "[TRACE] "
63-
if recorded == "" || recorded[:len(wantPrefix)] != wantPrefix {
64-
t.Fatalf("Tracef did not prefix message with %q: got %q", wantPrefix, recorded)
82+
if stub.debugfCalls != 1 {
83+
t.Fatalf("Tracef should log once when trace is enabled; got %d calls", stub.debugfCalls)
84+
}
85+
if !strings.HasPrefix(stub.lastFormat, "[TRACE] ") {
86+
t.Fatalf("Tracef did not prefix message with \"[TRACE] \": got %q", stub.lastFormat)
6587
}
6688
}
6789

@@ -70,11 +92,15 @@ func TestTracef(t *testing.T) {
7092
// Only the methods required by the tests are implemented; the rest
7193
// are no-ops to satisfy the interface.
7294
type stubLogger struct {
73-
debugf func(format string, args ...any)
95+
lastFormat string
96+
debugfCalls int
7497
}
7598

76-
func (s *stubLogger) Debug(args ...any) {}
77-
func (s *stubLogger) Debugf(format string, args ...any) { s.debugf(format, args...) }
99+
func (s *stubLogger) Debug(args ...any) {}
100+
func (s *stubLogger) Debugf(format string, args ...any) {
101+
s.debugfCalls++
102+
s.lastFormat = format
103+
}
78104
func (s *stubLogger) Info(args ...any) {}
79105
func (s *stubLogger) Infof(format string, args ...any) {}
80106
func (s *stubLogger) Warn(args ...any) {}

log/log.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const (
3131
LevelFatal = "fatal"
3232
)
3333

34-
var zapLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
34+
var (
35+
zapLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
36+
traceEnabled = false
37+
)
3538

3639
// Default borrows logging utilities from zap.
3740
// You may replace it with whatever logger you like as long as it implements log.Logger interface.
@@ -155,7 +158,13 @@ func Fatalf(format string, args ...any) {
155158

156159
// Tracef logs a message at the trace level with formatting.
157160
func Tracef(format string, args ...any) {
158-
// Trace is more detailed than debug, so we'll log it at debug level
159-
// until we have proper trace level support in the underlying logger
161+
if !traceEnabled {
162+
return
163+
}
160164
Default.Debugf("[TRACE] "+format, args...)
161165
}
166+
167+
// SetTraceEnabled sets the trace enabled flag.
168+
func SetTraceEnabled(enabled bool) {
169+
traceEnabled = enabled
170+
}

0 commit comments

Comments
 (0)