-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkpoint for whakapapa #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,11 @@ import ( | |
"mime" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"reflect" | ||
goruntime "runtime" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
@@ -626,6 +629,46 @@ func (r *Request) Stream() (io.ReadCloser, error) { | |
} | ||
} | ||
|
||
// appends sacktrace of source and function calls via goruntime.CallersFrames | ||
func appendWhakapapa(currUserAgent string) string { | ||
var lines []string | ||
var callers []uintptr | ||
const SKIP_CALLERS = 3 // This is always NewRESTClient,Verb, and Do | ||
// https://github.com/cncf/apisnoop/issues/23#issuecomment-397106705 | ||
|
||
callers = make([]uintptr, 64) | ||
num_callers := goruntime.Callers(SKIP_CALLERS, callers) | ||
|
||
cwdir, err := os.Getwd() | ||
if err != nil { | ||
return fmt.Sprintf("%v",err) | ||
} | ||
|
||
if num_callers > 0 { | ||
frames := goruntime.CallersFrames(callers) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://golang.org/pkg/runtime/#CallersFrames Provides the frames for stacktrace |
||
|
||
for { | ||
frame, more := frames.Next() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Iterate through all the frames |
||
// TODO: maybe differentiate between $GOPATH and current directory | ||
relpath, err := filepath.Rel(cwdir, frame.File) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to see the file / linenumber! |
||
var line string | ||
if err != nil { | ||
line = fmt.Sprintf("%s():NOCWD-%s:%d:ERR-%v", frame.Function, frame.File, frame.Line, err) | ||
} else { | ||
line = fmt.Sprintf("%s():%s:%d", frame.Function, relpath, frame.Line) | ||
} | ||
lines = append(lines, line) | ||
|
||
// Exclude functions in backtrace that are higher than "main.main" | ||
if more == false || frame.Function == "main.main" { | ||
break | ||
} | ||
} | ||
} | ||
|
||
return fmt.Sprintf("%s stacktrace=[%s]", currUserAgent, strings.Join(lines, ";")) | ||
} | ||
|
||
// request connects to the server and invokes the provided function when a server response is | ||
// received. It handles retry behavior and up front validation of requests. It will invoke | ||
// fn at most once. It will return an error if a problem occurred prior to connecting to the | ||
|
@@ -655,6 +698,12 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error { | |
client = http.DefaultClient | ||
} | ||
|
||
// Add Whakapapa to the User-Agent | ||
if true { // TODO: Enable only when a VAR is set | ||
userAgent := DefaultKubernetesUserAgent() | ||
r.SetHeader("User-Agent", appendWhakapapa(userAgent)) | ||
} | ||
|
||
// Right now we make about ten retry attempts if we get a Retry-After response. | ||
maxRetries := 10 | ||
retries := 0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://golang.org/pkg/runtime/#Callers
Provides the number of callers within the stackframe