Skip to content

Commit

Permalink
Change takeStacktrace skip parameter to a value
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner committed Jul 25, 2020
1 parent 017057e commit 8e25507
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions field.go
Expand Up @@ -368,7 +368,7 @@ func Stack(key string) Field {
// from expanding the zapcore.Field union struct to include a byte slice. Since
// taking a stacktrace is already so expensive (~10us), the extra allocation
// is okay.
return String(key, takeStacktrace(nil))
return String(key, takeStacktrace(0))
}

// StackSkip constructs a field that stores a stacktrace of the current
Expand All @@ -381,7 +381,7 @@ func StackSkip(key string, skip int) Field {
// from expanding the zapcore.Field union struct to include a byte slice. Since
// taking a stacktrace is already so expensive (~10us), the extra allocation
// is okay.
return String(key, takeStacktrace(&skip))
return String(key, takeStacktrace(skip))
}

// Duration constructs a field with the given key and value. The encoder
Expand Down
2 changes: 1 addition & 1 deletion field_test.go
Expand Up @@ -259,6 +259,6 @@ func TestStackField(t *testing.T) {
f := Stack("stacktrace")
assert.Equal(t, "stacktrace", f.Key, "Unexpected field key.")
assert.Equal(t, zapcore.StringType, f.Type, "Unexpected field type.")
assert.Equal(t, takeStacktrace(nil), f.String, "Unexpected stack trace")
assert.Equal(t, takeStacktrace(0), f.String, "Unexpected stack trace")
assertCanBeReused(t, f)
}
10 changes: 3 additions & 7 deletions stacktrace.go
Expand Up @@ -43,7 +43,7 @@ var (
_zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...)
)

func takeStacktrace(skip *int) string {
func takeStacktrace(skip int) string {
buffer := bufferpool.Get()
defer buffer.Free()
programCounters := _stacktracePool.Get().(*programCounters)
Expand All @@ -69,19 +69,15 @@ func takeStacktrace(skip *int) string {
// 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.
toSkip := 0
if skip != nil {
toSkip = *skip
}
for frame, more := frames.Next(); more; frame, more = frames.Next() {
if skipZapFrames && isZapFrame(frame.Function) {
continue
} else {
skipZapFrames = false
}

if toSkip > 0 {
toSkip--
if skip > 0 {
skip--
continue
}

Expand Down
4 changes: 2 additions & 2 deletions stacktrace_test.go
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestTakeStacktrace(t *testing.T) {
trace := takeStacktrace(nil)
trace := takeStacktrace(0)
lines := strings.Split(trace, "\n")
require.True(t, len(lines) > 0, "Expected stacktrace to have at least one frame.")
assert.Contains(
Expand Down Expand Up @@ -70,6 +70,6 @@ func TestIsZapFrame(t *testing.T) {

func BenchmarkTakeStacktrace(b *testing.B) {
for i := 0; i < b.N; i++ {
takeStacktrace(nil)
takeStacktrace(0)
}
}

0 comments on commit 8e25507

Please sign in to comment.