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

Fix traces for get requests #44667

Merged
merged 1 commit into from
Apr 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions staging/src/k8s.io/apiserver/pkg/endpoints/handlers/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (scope *RequestScope) err(err error, w http.ResponseWriter, req *http.Reque

// getterFunc performs a get request with the given context and object name. The request
// may be used to deserialize an options object to pass to the getter.
type getterFunc func(ctx request.Context, name string, req *http.Request) (runtime.Object, error)
type getterFunc func(ctx request.Context, name string, req *http.Request, trace *utiltrace.Trace) (runtime.Object, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to block this PR on this, but traces need to becoming from the context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to switch to open tracing in general, but this was just a quick fix to what we currently have.


// MaxRetryWhenPatchConflicts is the maximum number of conflicts retry during a patch operation before returning failure
const MaxRetryWhenPatchConflicts = 5
Expand All @@ -86,6 +86,9 @@ const MaxRetryWhenPatchConflicts = 5
// passed-in getterFunc to perform the actual get.
func getResourceHandler(scope RequestScope, getter getterFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
trace := utiltrace.New("Get " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)

namespace, name, err := scope.Namer.Name(req)
if err != nil {
scope.err(err, w, req)
Expand All @@ -94,7 +97,7 @@ func getResourceHandler(scope RequestScope, getter getterFunc) http.HandlerFunc
ctx := scope.ContextFunc(req)
ctx = request.WithNamespace(ctx, namespace)

result, err := getter(ctx, name, req)
result, err := getter(ctx, name, req, trace)
if err != nil {
scope.err(err, w, req)
return
Expand All @@ -103,18 +106,15 @@ func getResourceHandler(scope RequestScope, getter getterFunc) http.HandlerFunc
scope.err(err, w, req)
return
}
trace.Step("About to write a response")
responsewriters.WriteObject(http.StatusOK, scope.Kind.GroupVersion(), scope.Serializer, result, w, req)
}
}

// GetResource returns a function that handles retrieving a single resource from a rest.Storage object.
func GetResource(r rest.Getter, e rest.Exporter, scope RequestScope) http.HandlerFunc {
return getResourceHandler(scope,
func(ctx request.Context, name string, req *http.Request) (runtime.Object, error) {
// For performance tracking purposes.
trace := utiltrace.New("Get " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)

func(ctx request.Context, name string, req *http.Request, trace *utiltrace.Trace) (runtime.Object, error) {
// check for export
options := metav1.GetOptions{}
if values := req.URL.Query(); len(values) > 0 {
Expand All @@ -132,19 +132,25 @@ func GetResource(r rest.Getter, e rest.Exporter, scope RequestScope) http.Handle
return nil, err
}
}

if trace != nil {
trace.Step("About to Get from storage")
}
return r.Get(ctx, name, &options)
})
}

// GetResourceWithOptions returns a function that handles retrieving a single resource from a rest.Storage object.
func GetResourceWithOptions(r rest.GetterWithOptions, scope RequestScope, isSubresource bool) http.HandlerFunc {
return getResourceHandler(scope,
func(ctx request.Context, name string, req *http.Request) (runtime.Object, error) {
func(ctx request.Context, name string, req *http.Request, trace *utiltrace.Trace) (runtime.Object, error) {
opts, subpath, subpathKey := r.NewGetOptions()
trace.Step("About to process Get options")
if err := getRequestOptions(req, scope, opts, subpath, subpathKey, isSubresource); err != nil {
return nil, err
}
if trace != nil {
trace.Step("About to Get from storage")
}
return r.Get(ctx, name, opts)
})
}
Expand Down