upgrade otel to v1.0.0 for compatibility

This commit is contained in:
John Guo
2022-03-25 11:06:23 +08:00
parent cc60bc9dab
commit a2823a501e
13 changed files with 143 additions and 137 deletions

View File

@ -8,7 +8,6 @@ package gdebug
import (
"fmt"
"github.com/gogf/gf/internal/utils"
"os"
"os/exec"
"path/filepath"
@ -34,7 +33,7 @@ func init() {
goRootForFilter = strings.Replace(goRootForFilter, "\\", "/", -1)
}
// Initialize internal package variable: selfPath.
selfPath, _ := exec.LookPath(os.Args[0])
selfPath, _ = exec.LookPath(os.Args[0])
if selfPath != "" {
selfPath, _ = filepath.Abs(selfPath)
}
@ -46,14 +45,14 @@ func init() {
// Caller returns the function name and the absolute file path along with its line
// number of the caller.
func Caller(skip ...int) (function string, path string, line int) {
return CallerWithFilter("", skip...)
return CallerWithFilter(nil, skip...)
}
// CallerWithFilter returns the function name and the absolute file path along with
// its line number of the caller.
//
// The parameter `filter` is used to filter the path of the caller.
func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) {
// The parameter `filters` is used to filter the path of the caller.
func CallerWithFilter(filters []string, skip ...int) (function string, path string, line int) {
var (
number = 0
ok = true
@ -61,14 +60,17 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string,
if len(skip) > 0 {
number = skip[0]
}
pc, file, line, start := callerFromIndex([]string{filter})
pc, file, line, start := callerFromIndex(filters)
if start != -1 {
for i := start + number; i < maxCallerDepth; i++ {
if i != start {
pc, file, line, ok = runtime.Caller(i)
}
if ok {
function := ""
if filterFileByFilters(file, filters) {
continue
}
function = ""
if fn := runtime.FuncForPC(pc); fn == nil {
function = "unknown"
} else {
@ -86,30 +88,14 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string,
// callerFromIndex returns the caller position and according information exclusive of the
// debug package.
//
// VERY NOTE THAT, the returned index value should be <index - 1> as the caller's start point.
// VERY NOTE THAT, the returned index value should be `index - 1` as the caller's start point.
func callerFromIndex(filters []string) (pc uintptr, file string, line int, index int) {
var filtered, ok bool
var ok bool
for index = 0; index < maxCallerDepth; index++ {
if pc, file, line, ok = runtime.Caller(index); ok {
filtered = false
for _, filter := range filters {
if filter != "" && strings.Contains(file, filter) {
filtered = true
break
}
}
if filtered {
if filterFileByFilters(file, filters) {
continue
}
if !utils.IsDebugEnabled() {
if strings.Contains(file, utils.StackFilterKeyForGoFrame) {
continue
}
} else {
if strings.Contains(file, stackFilterKey) {
continue
}
}
if index > 0 {
index--
}
@ -119,6 +105,27 @@ func callerFromIndex(filters []string) (pc uintptr, file string, line int, index
return 0, "", -1, -1
}
func filterFileByFilters(file string, filters []string) (filtered bool) {
// Filter empty file.
if file == "" {
return true
}
// Filter gdebug package callings.
if strings.Contains(file, stackFilterKey) {
return true
}
for _, filter := range filters {
if filter != "" && strings.Contains(file, filter) {
return true
}
}
// GOROOT filter.
if goRootForFilter != "" && len(file) >= len(goRootForFilter) && file[0:len(goRootForFilter)] == goRootForFilter {
return true
}
return false
}
// CallerPackage returns the package name of the caller.
func CallerPackage() string {
function, _, _ := Caller()

View File

@ -9,9 +9,7 @@ package gdebug
import (
"bytes"
"fmt"
"github.com/gogf/gf/internal/utils"
"runtime"
"strings"
)
// PrintStack prints to standard error the stack trace returned by runtime.Stack.
@ -22,15 +20,15 @@ func PrintStack(skip ...int) {
// Stack returns a formatted stack trace of the goroutine that calls it.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
func Stack(skip ...int) string {
return StackWithFilter("", skip...)
return StackWithFilter(nil, skip...)
}
// StackWithFilter returns a formatted stack trace of the goroutine that calls it.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
//
// The parameter `filter` is used to filter the path of the caller.
func StackWithFilter(filter string, skip ...int) string {
return StackWithFilters([]string{filter}, skip...)
func StackWithFilter(filters []string, skip ...int) string {
return StackWithFilters(filters, skip...)
}
// StackWithFilters returns a formatted stack trace of the goroutine that calls it.
@ -50,7 +48,6 @@ func StackWithFilters(filters []string, skip ...int) string {
space = " "
index = 1
buffer = bytes.NewBuffer(nil)
filtered = false
ok = true
pc, file, line, start = callerFromIndex(filters)
)
@ -59,38 +56,9 @@ func StackWithFilters(filters []string, skip ...int) string {
pc, file, line, ok = runtime.Caller(i)
}
if ok {
// Filter empty file.
if file == "" {
if filterFileByFilters(file, filters) {
continue
}
// GOROOT filter.
if goRootForFilter != "" &&
len(file) >= len(goRootForFilter) &&
file[0:len(goRootForFilter)] == goRootForFilter {
continue
}
// Custom filtering.
filtered = false
for _, filter := range filters {
if filter != "" && strings.Contains(file, filter) {
filtered = true
break
}
}
if filtered {
continue
}
if !utils.IsDebugEnabled() {
if strings.Contains(file, utils.StackFilterKeyForGoFrame) {
continue
}
} else {
if strings.Contains(file, stackFilterKey) {
continue
}
}
if fn := runtime.FuncForPC(pc); fn == nil {
name = "unknown"
} else {