Added scope as tracing attribute

Kubernetes-commit: e4169d7f4381595152f6fa862af2227950f3fc76
This commit is contained in:
Paweł Banaszewski 2022-11-08 11:22:57 +00:00 committed by Kubernetes Publisher
parent 20c697acfa
commit ebb0a2b606
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}),