From 71fa7d7d64785c98d3814e53b3544fd8cccff897 Mon Sep 17 00:00:00 2001 From: Chris Hines Date: Thu, 16 Sep 2021 22:55:19 -0400 Subject: [PATCH] Simplify stack helpers and remove internal/stack package (#11) --- internal/stack/stack.go | 38 -------------------------------------- log_test.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 42 deletions(-) delete mode 100644 internal/stack/stack.go diff --git a/internal/stack/stack.go b/internal/stack/stack.go deleted file mode 100644 index b0acb15..0000000 --- a/internal/stack/stack.go +++ /dev/null @@ -1,38 +0,0 @@ -package stack - -import "runtime" - -// Caller returns a frame from the stack of the current goroutine. The argument -// skip is the number of frames to ascend, with 0 identifying the -// calling function. -func Caller(skip int) runtime.Frame { - var pcs [3]uintptr - - n := runtime.Callers(skip+1, pcs[:]) - frames := runtime.CallersFrames(pcs[:n]) - frame, _ := frames.Next() - frame, _ = frames.Next() - - return frame -} - -// Trace returns a call stack for the current goroutine with element 0 -// identifying the calling function. -func Trace() []runtime.Frame { - var pcs [512]uintptr - n := runtime.Callers(1, pcs[:]) - - frames := runtime.CallersFrames(pcs[:n]) - cs := make([]runtime.Frame, 0, n) - - // Skip extra frame retrieved just to make sure the runtime.sigpanic - // special case is handled. - frame, more := frames.Next() - - for more { - frame, more = frames.Next() - cs = append(cs, frame) - } - - return cs -} diff --git a/log_test.go b/log_test.go index 76a4582..8a0e15c 100644 --- a/log_test.go +++ b/log_test.go @@ -2,11 +2,11 @@ package log_test import ( "bytes" + "runtime" "sync" "testing" "github.com/go-kit/log" - "github.com/go-kit/log/internal/stack" ) func TestContext(t *testing.T) { @@ -108,7 +108,7 @@ func TestWithPrefixAndSuffix(t *testing.T) { // Valuers, regardless of how many times With has been called. func TestContextStackDepth(t *testing.T) { t.Parallel() - fn := stack.Caller(0).Function + fn := callingFunctions()[0] var output []interface{} @@ -118,8 +118,8 @@ func TestContextStackDepth(t *testing.T) { })) stackValuer := log.Valuer(func() interface{} { - for i, f := range stack.Trace() { - if f.Function == fn { + for i, f := range callingFunctions() { + if f == fn { return i } } @@ -149,6 +149,29 @@ func TestContextStackDepth(t *testing.T) { } } +// callingFunctions returns the names of the functions on the call stack for the +// current goroutine with element 0 identifying the calling function. +func callingFunctions() []string { + pcs := make([]uintptr, 10) + n := runtime.Callers(2, pcs) + if n == 0 { + return nil + } + + frames := runtime.CallersFrames(pcs[:n]) + funcs := make([]string, 0, n) + + for { + frame, more := frames.Next() + funcs = append(funcs, frame.Function) + if !more { + break + } + } + + return funcs +} + // Test that With returns a Logger safe for concurrent use. This test // validates that the stored logging context does not get corrupted when // multiple clients concurrently log additional keyvals.