from_stack.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package naming
  14. import (
  15. "fmt"
  16. "regexp"
  17. goruntime "runtime"
  18. "runtime/debug"
  19. "strconv"
  20. "strings"
  21. )
  22. // GetNameFromCallsite walks back through the call stack until we find a caller from outside of the ignoredPackages
  23. // it returns back a shortpath/filename:line to aid in identification of this reflector when it starts logging
  24. func GetNameFromCallsite(ignoredPackages ...string) string {
  25. name := "????"
  26. const maxStack = 10
  27. for i := 1; i < maxStack; i++ {
  28. _, file, line, ok := goruntime.Caller(i)
  29. if !ok {
  30. file, line, ok = extractStackCreator()
  31. if !ok {
  32. break
  33. }
  34. i += maxStack
  35. }
  36. if hasPackage(file, append(ignoredPackages, "/runtime/asm_")) {
  37. continue
  38. }
  39. file = trimPackagePrefix(file)
  40. name = fmt.Sprintf("%s:%d", file, line)
  41. break
  42. }
  43. return name
  44. }
  45. // hasPackage returns true if the file is in one of the ignored packages.
  46. func hasPackage(file string, ignoredPackages []string) bool {
  47. for _, ignoredPackage := range ignoredPackages {
  48. if strings.Contains(file, ignoredPackage) {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. // trimPackagePrefix reduces duplicate values off the front of a package name.
  55. func trimPackagePrefix(file string) string {
  56. if l := strings.LastIndex(file, "/vendor/"); l >= 0 {
  57. return file[l+len("/vendor/"):]
  58. }
  59. if l := strings.LastIndex(file, "/src/"); l >= 0 {
  60. return file[l+5:]
  61. }
  62. if l := strings.LastIndex(file, "/pkg/"); l >= 0 {
  63. return file[l+1:]
  64. }
  65. return file
  66. }
  67. var stackCreator = regexp.MustCompile(`(?m)^created by (.*)\n\s+(.*):(\d+) \+0x[[:xdigit:]]+$`)
  68. // extractStackCreator retrieves the goroutine file and line that launched this stack. Returns false
  69. // if the creator cannot be located.
  70. // TODO: Go does not expose this via runtime https://github.com/golang/go/issues/11440
  71. func extractStackCreator() (string, int, bool) {
  72. stack := debug.Stack()
  73. matches := stackCreator.FindStringSubmatch(string(stack))
  74. if len(matches) != 4 {
  75. return "", 0, false
  76. }
  77. line, err := strconv.Atoi(matches[3])
  78. if err != nil {
  79. return "", 0, false
  80. }
  81. return matches[2], line, true
  82. }