Merge pull request #113728 from pawbana/add-scope-to-api-server-tracking

Added scope to api server tracing
This commit is contained in:
Kubernetes Prow Robot 2022-11-08 04:30:22 -08:00 committed by GitHub
commit 34ca18d1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -121,3 +121,21 @@ func (lazy *lazyResource) String() string {
return "unknown"
}
// lazyScope implements String() string and it will
// lazily get Scope from request info
type lazyScope struct {
req *http.Request
}
func (lazy *lazyScope) String() string {
if lazy.req != nil {
ctx := lazy.req.Context()
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
if ok {
return metrics.CleanScope(requestInfo)
}
}
return "unknown"
}

View File

@ -96,3 +96,15 @@ func TestLazyResource(t *testing.T) {
resourceWithReq := &lazyResource{req: req.WithContext(ctx)}
assert.Equal(t, "resource", fmt.Sprintf("%v", resourceWithReq))
}
func TestLazyScope(t *testing.T) {
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyScope{}))
scopeWithEmptyReq := &lazyScope{&http.Request{}}
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
req := &http.Request{}
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Namespace: "ns"})
scopeWithReq := &lazyScope{req: req.WithContext(ctx)}
assert.Equal(t, "namespace", fmt.Sprintf("%v", scopeWithReq))
}

View File

@ -29,6 +29,7 @@ func traceFields(req *http.Request) []attribute.KeyValue {
attribute.Stringer("client", &lazyClientIP{req: req}),
attribute.String("protocol", req.Proto),
attribute.Stringer("resource", &lazyResource{req: req}),
attribute.Stringer("scope", &lazyScope{req: req}),
attribute.String("url", req.URL.Path),
attribute.Stringer("user-agent", &lazyTruncatedUserAgent{req: req}),
attribute.Stringer("verb", &lazyVerb{req: req}),