Use runtime.CallersFrames to get more detailed stack information (#8868)

Signed-off-by: dragondriver <jiquan.long@zilliz.com>
This commit is contained in:
dragondriver 2021-09-29 22:02:00 +08:00 committed by GitHub
parent 7094722fb0
commit 3636ade689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 18 deletions

View File

@ -88,8 +88,9 @@ func StartSpanFromContextWithSkip(ctx context.Context, skip int, opts ...opentra
span.LogFields(log.Error(errors.New("runtime.Callers failed"))) span.LogFields(log.Error(errors.New("runtime.Callers failed")))
return span, ctx return span, ctx
} }
fn := runtime.FuncForPC(pcs[0]) frames := runtime.CallersFrames(pcs[:])
name := fn.Name() frame, _ := frames.Next()
name := frame.Function
if lastSlash := strings.LastIndexByte(name, '/'); lastSlash > 0 { if lastSlash := strings.LastIndexByte(name, '/'); lastSlash > 0 {
name = name[lastSlash+1:] name = name[lastSlash+1:]
} }
@ -99,7 +100,7 @@ func StartSpanFromContextWithSkip(ctx context.Context, skip int, opts ...opentra
} }
span := opentracing.StartSpan(name, opts...) span := opentracing.StartSpan(name, opts...)
file, line := fn.FileLine(pcs[0]) file, line := frame.File, frame.Line
span.LogFields(log.String("filename", file), log.Int("line", line)) span.LogFields(log.String("filename", file), log.Int("line", line))
return span, opentracing.ContextWithSpan(ctx, span) return span, opentracing.ContextWithSpan(ctx, span)
@ -125,7 +126,9 @@ func StartSpanFromContextWithOperationNameWithSkip(ctx context.Context, operatio
span.LogFields(log.Error(errors.New("runtime.Callers failed"))) span.LogFields(log.Error(errors.New("runtime.Callers failed")))
return span, ctx return span, ctx
} }
file, line := runtime.FuncForPC(pcs[0]).FileLine(pcs[0]) frames := runtime.CallersFrames(pcs[:])
frame, _ := frames.Next()
file, line := frame.File, frame.Line
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil { if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context())) opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
@ -153,7 +156,9 @@ func LogError(span opentracing.Span, err error) error {
return err return err
} }
file, line := runtime.FuncForPC(pcs[0]).FileLine(pcs[0]) frames := runtime.CallersFrames(pcs[:])
frame, _ := frames.Next()
file, line := frame.File, frame.Line
span.LogFields(log.String("filename", file), log.Int("line", line), log.Error(err)) span.LogFields(log.String("filename", file), log.Int("line", line), log.Error(err))
return err return err

View File

@ -14,6 +14,7 @@ package trace
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"testing" "testing"
"errors" "errors"
@ -28,16 +29,18 @@ type simpleStruct struct {
value string value string
} }
func TestMain(m *testing.M) {
closer := InitTracing("test")
defer closer.Close()
os.Exit(m.Run())
}
func TestInit(t *testing.T) { func TestInit(t *testing.T) {
cfg := initFromEnv("test") cfg := initFromEnv("test")
assert.NotNil(t, cfg) assert.NotNil(t, cfg)
} }
func TestTracing(t *testing.T) { func TestTracing(t *testing.T) {
//Already Init in each framework, this can be ignored in debug
closer := InitTracing("test")
defer closer.Close()
// context normally can be propagated through func params // context normally can be propagated through func params
ctx := context.Background() ctx := context.Background()
@ -88,10 +91,6 @@ func caller(ctx context.Context) error {
} }
func TestInject(t *testing.T) { func TestInject(t *testing.T) {
//Already Init in each framework, this can be ignored in debug
closer := InitTracing("test")
defer closer.Close()
// context normally can be propagated through func params // context normally can be propagated through func params
ctx := context.Background() ctx := context.Background()
@ -109,10 +108,6 @@ func TestInject(t *testing.T) {
} }
func TestTraceError(t *testing.T) { func TestTraceError(t *testing.T) {
//Already Init in each framework, this can be ignored in debug
closer := InitTracing("test")
defer closer.Close()
// context normally can be propagated through func params // context normally can be propagated through func params
sp, ctx := StartSpanFromContext(nil) sp, ctx := StartSpanFromContext(nil)
assert.Nil(t, ctx) assert.Nil(t, ctx)
@ -137,5 +132,4 @@ func TestTraceError(t *testing.T) {
assert.Equal(t, id, "") assert.Equal(t, id, "")
assert.Equal(t, sampled, false) assert.Equal(t, sampled, false)
assert.Equal(t, found, false) assert.Equal(t, found, false)
} }