Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: Remove log.Debug from hot path (-2 allocs; -5% CPU) #1186

Merged
merged 4 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion ddtrace/tracer/tracer.go
Expand Up @@ -434,7 +434,16 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
if t.config.profilerHotspots || t.config.profilerEndpoints {
t.applyPPROFLabels(pprofContext, span)
}
log.Debug("Started Span: %v, Operation: %s, Resource: %s, Tags: %v, %v", span, span.Name, span.Resource, span.Meta, span.Metrics)
if t.config.serviceMappings != nil {
if newSvc, ok := t.config.serviceMappings[span.Service]; ok {
span.Service = newSvc
}
}
if log.DebugEnabled() {
// avoid allocating the ...interface{} argument if debug logging is disabled
log.Debug("Started Span: %v, Operation: %s, Resource: %s, Tags: %v, %v",
span, span.Name, span.Resource, span.Meta, span.Metrics)
}
return span
}

Expand Down
12 changes: 9 additions & 3 deletions internal/log/log.go
Expand Up @@ -56,12 +56,18 @@ func SetLevel(lvl Level) {
level = lvl
}

// Debug prints the given message if the level is LevelDebug.
func Debug(fmt string, a ...interface{}) {
// DebugEnabled returns true if debug log messages are enabled. This can be used in extremely
// hot code paths to avoid allocating the ...interface{} argument.
func DebugEnabled() bool {
mu.RLock()
lvl := level
mu.RUnlock()
if lvl != LevelDebug {
return lvl == LevelDebug
}

// Debug prints the given message if the level is LevelDebug.
func Debug(fmt string, a ...interface{}) {
if !DebugEnabled() {
return
}
printMsg("DEBUG", fmt, a...)
Expand Down
2 changes: 2 additions & 0 deletions internal/log/log_test.go
Expand Up @@ -60,13 +60,15 @@ func TestLog(t *testing.T) {
tp.Reset()
defer func(old Level) { level = old }(level)
SetLevel(LevelDebug)
assert.True(t, DebugEnabled())

Debug("message %d", 3)
assert.Equal(t, msg("DEBUG", "message 3"), tp.Lines()[0])
})

t.Run("off", func(t *testing.T) {
tp.Reset()
assert.False(t, DebugEnabled())
Debug("message %d", 2)
assert.Len(t, tp.Lines(), 0)
})
Expand Down