Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiserver/pkg/audit/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

const (
maxUserAgentLength = 1024
maxUserAgentLength = 16384
userAgentTruncateSuffix = "...TRUNCATED"
)

Expand Down
49 changes: 49 additions & 0 deletions staging/src/k8s.io/client-go/rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (
"mime"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
goruntime "runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
Copy link
Member Author

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


cwdir, err := os.Getwd()
if err != nil {
return fmt.Sprintf("%v",err)
}

if num_callers > 0 {
frames := goruntime.CallersFrames(callers)
Copy link
Member Author

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/#CallersFrames

Provides the frames for stacktrace


for {
frame, more := frames.Next()
Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Expand Down