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

Share callstack b/w Caller and Stack #808

Closed
wants to merge 5 commits into from
Closed
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
35 changes: 32 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,44 @@ func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {

// Thread the error output through to the CheckedEntry.
ce.ErrorOutput = log.errorOutput

addStack := log.addStack.Enabled(ce.Entry.Level)
if !log.addCaller && !addStack {
return ce
}

// If a stack trace was requested, capture the entire stack.
stackLimit := 0

// For historical reasons, stack traces reported by Zap don't respect
// CallerSkip. See also #727.
stackSkip := callerSkipOffset

if !addStack {
stackLimit = 1
stackSkip += log.callerSkip
}

programCounters := newProgramCounters()
defer programCounters.Release()

pcs := programCounters.Callers(stackSkip, stackLimit)
if log.addCaller {
ce.Entry.Caller = zapcore.NewEntryCaller(runtime.Caller(log.callerSkip + callerSkipOffset))
if len(pcs) == 0 {
ce.Entry.Caller = zapcore.NewEntryCaller(0, "", 0, false)
} else {
frame, _ := runtime.CallersFrames(pcs).Next()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are adding caller and stack, then the stackSkip passed in is 0 (e.g., no caller skip), so the caller value used here would be incorrect right, since we wouldn't be respecting the log.callerSkip

ce.Entry.Caller = zapcore.NewEntryCaller(frame.PC, frame.File, frame.Line, true)
}

if !ce.Entry.Caller.Defined {
fmt.Fprintf(log.errorOutput, "%v Logger.check error: failed to get caller\n", time.Now().UTC())
log.errorOutput.Sync()
}
}
if log.addStack.Enabled(ce.Entry.Level) {
ce.Entry.Stack = Stack("").String

if addStack {
ce.Entry.Stack = makeStacktrace(pcs, true /* skip zap */)
}

return ce
Expand Down
35 changes: 35 additions & 0 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,41 @@ func BenchmarkAddCallerHook(b *testing.B) {
})
}

func BenchmarkAddStack(b *testing.B) {
logger := New(
zapcore.NewCore(
zapcore.NewJSONEncoder(NewProductionConfig().EncoderConfig),
&ztest.Discarder{},
InfoLevel,
),
AddStacktrace(InfoLevel),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("foo")
}
})
}

func BenchmarkAddCallerAndStack(b *testing.B) {
logger := New(
zapcore.NewCore(
zapcore.NewJSONEncoder(NewProductionConfig().EncoderConfig),
&ztest.Discarder{},
InfoLevel,
),
AddCaller(),
AddStacktrace(InfoLevel),
)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("foo")
}
})
}

func Benchmark10Fields(b *testing.B) {
withBenchedLogger(b, func(log *Logger) {
log.Info("Ten fields, passed at the log site.",
Expand Down
86 changes: 61 additions & 25 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,34 @@ import (
const _zapPackage = "go.uber.org/zap"

var (
_stacktracePool = sync.Pool{
New: func() interface{} {
return newProgramCounters(64)
},
}

// We add "." and "/" suffixes to the package name to ensure we only match
// the exact package and not any package with the same prefix.
_zapStacktracePrefixes = addPrefix(_zapPackage, ".", "/")
_zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...)
)

func takeStacktrace() string {
pcs := newProgramCounters()
defer pcs.Release()

return makeStacktrace(
pcs.Callers(1 /* skip */, 0 /* limit */),
true, // skip zap
)
}

func makeStacktrace(pcs []uintptr, skipZapFrames bool) string {
buffer := bufferpool.Get()
defer buffer.Free()
programCounters := _stacktracePool.Get().(*programCounters)
defer _stacktracePool.Put(programCounters)

var numFrames int
for {
// Skip the call to runtime.Counters and takeStacktrace so that the
// program counters start at the caller of takeStacktrace.
numFrames = runtime.Callers(2, programCounters.pcs)
if numFrames < len(programCounters.pcs) {
break
}
// Don't put the too-short counter slice back into the pool; this lets
// the pool adjust if we consistently take deep stacktraces.
programCounters = newProgramCounters(len(programCounters.pcs) * 2)
}

i := 0
skipZapFrames := true // skip all consecutive zap frames at the beginning.
frames := runtime.CallersFrames(programCounters.pcs[:numFrames])
frames := runtime.CallersFrames(pcs)

// Note: On the last iteration, frames.Next() returns false, with a valid
// frame, but we ignore this frame. The last frame is a a runtime frame which
// adds noise, since it's only either runtime.main or runtime.goexit.
for frame, more := frames.Next(); more; frame, more = frames.Next() {
// skip all consecutive zap frames at the beginning.
if skipZapFrames && isZapFrame(frame.Function) {
continue
} else {
Expand Down Expand Up @@ -113,8 +102,55 @@ type programCounters struct {
pcs []uintptr
}

func newProgramCounters(size int) *programCounters {
return &programCounters{make([]uintptr, size)}
var _stacktracePool = sync.Pool{
New: func() interface{} {
return &programCounters{make([]uintptr, 64)}
},
}

func newProgramCounters() *programCounters {
return _stacktracePool.Get().(*programCounters)
}

func (pcs *programCounters) Release() {
_stacktracePool.Put(pcs)
}

// Callers captures program counters of callers of the current function.
//
// skip specifies the number of functions to skip.
// limit specifies how deep into the stack we go, zero meaning unlimited.
func (pcs *programCounters) Callers(skip int, limit int) []uintptr {
pc := pcs.pcs
if limit > 0 {
// The shortened slice will never be placed back into the
// pool, but the longer one will.
if limit < len(pc) {
pc = pc[:limit]
} else {
pc = make([]uintptr, limit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case never gets hit in prod right? anyway we can refactor the change to avoid adding a case that's not necessary in prod?

pcs.pcs = pc
}
}

var numFrames int
for {
// Skip the call to runtime.Callers and programCounter.Callers
// so that the program counters start at our caller.
numFrames = runtime.Callers(skip+2, pc)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do numFrames := since we don't use the value outside of the for loop and always replace it


// All required frames have been captured if the limit was
// non-zero or if the stack depth is smaller than the size of
// the slice.
if limit > 0 || numFrames < len(pc) {
return pc[:numFrames]
}

// Don't put the too-short counter slice back into the pool; this lets
// the pool adjust if we consistently take deep stacktraces.
pc = make([]uintptr, len(pcs.pcs)*2)
pcs.pcs = pc
}
}

func addPrefix(prefix string, ss ...string) []string {
Expand Down
53 changes: 53 additions & 0 deletions stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,56 @@ func BenchmarkTakeStacktrace(b *testing.B) {
takeStacktrace()
}
}

func TestProgramCountersCallersDeepStack(t *testing.T) {
tests := []struct {
desc string
stackDepth int
limit int
wantOccurrences int
}{
{
desc: "large limit",
stackDepth: 128,
limit: 128,
wantOccurrences: 127,
// one of the slots is taken by the closure we pass to
// withStackDepth.
},
{
desc: "unlimited",
stackDepth: 500,
wantOccurrences: 500,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
const withStackDepthName = "go.uber.org/zap.withStackDepth"

var trace string
withStackDepth(tt.stackDepth, func() {
pcs := newProgramCounters().Callers(1, tt.limit)
trace = makeStacktrace(pcs, false /* don't skip zap */)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to avoid this boolean arg (as it's a test-only code path, and generally boolean args are not great)
presumably you need a fair bit of stack depth,, is it possible to use something outside of zap that takes in a callback, e.g.,
https://golang.org/pkg/bytes/#Map
hacky PoC: https://play.golang.org/p/bKrUH8kEreE

And keep calling that function recursively till we hit the limit we want, and that should add non-zap stack frames?

})

for found := 0; found < tt.wantOccurrences; found++ {
i := strings.Index(trace, withStackDepthName)
if i < 0 {
t.Fatalf(`expected %d occurrences of %q, found %d`, tt.wantOccurrences, withStackDepthName, found)
}

trace = trace[i+len(withStackDepthName):]
}
})
}

}

func withStackDepth(depth int, f func()) {
if depth > 0 {
withStackDepth(depth-1, f)
} else {
f()
}
}