Add namespace to apiserver tracing

This commit is contained in:
HirazawaUi 2023-09-03 21:50:47 +08:00
parent c83eb6dcaa
commit 45b9b0df41
3 changed files with 31 additions and 0 deletions

View File

@ -149,6 +149,24 @@ func (lazy *lazySubresource) String() string {
return "unknown" return "unknown"
} }
// lazyNamespace implements String() string and it will
// lazily get Group from request info.
type lazyNamespace struct {
req *http.Request
}
func (lazy *lazyNamespace) String() string {
if lazy.req != nil {
ctx := lazy.req.Context()
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
if ok {
return requestInfo.Namespace
}
}
return "unknown"
}
// lazyAuditID implements Stringer interface to lazily retrieve // lazyAuditID implements Stringer interface to lazily retrieve
// the audit ID associated with the request. // the audit ID associated with the request.
type lazyAuditID struct { type lazyAuditID struct {

View File

@ -133,6 +133,18 @@ func TestLazySubresource(t *testing.T) {
assert.Equal(t, "binding", fmt.Sprintf("%v", scopeWithReq)) assert.Equal(t, "binding", fmt.Sprintf("%v", scopeWithReq))
} }
func TestLazyNamespace(t *testing.T) {
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyNamespace{}))
scopeWithEmptyReq := &lazyNamespace{&http.Request{}}
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
req := &http.Request{}
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Namespace: "jaeger"})
scopeWithReq := &lazyNamespace{req: req.WithContext(ctx)}
assert.Equal(t, "jaeger", fmt.Sprintf("%v", scopeWithReq))
}
func TestLazyResource(t *testing.T) { func TestLazyResource(t *testing.T) {
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{})) assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{}))

View File

@ -31,6 +31,7 @@ func traceFields(req *http.Request) []attribute.KeyValue {
attribute.Stringer("api-version", &lazyAPIVersion{req: req}), attribute.Stringer("api-version", &lazyAPIVersion{req: req}),
attribute.Stringer("name", &lazyName{req: req}), attribute.Stringer("name", &lazyName{req: req}),
attribute.Stringer("subresource", &lazySubresource{req: req}), attribute.Stringer("subresource", &lazySubresource{req: req}),
attribute.Stringer("namespace", &lazyNamespace{req: req}),
attribute.String("protocol", req.Proto), attribute.String("protocol", req.Proto),
attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("resource", &lazyResource{req: req}),
attribute.Stringer("scope", &lazyScope{req: req}), attribute.Stringer("scope", &lazyScope{req: req}),