From 45b9b0df41fb67cf2c71263b3577f3b3daff4899 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Sun, 3 Sep 2023 21:50:47 +0800 Subject: [PATCH] Add namespace to apiserver tracing --- .../pkg/endpoints/handlers/helpers.go | 18 ++++++++++++++++++ .../pkg/endpoints/handlers/helpers_test.go | 12 ++++++++++++ .../pkg/endpoints/handlers/trace_util.go | 1 + 3 files changed, 31 insertions(+) diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go index b7c4f9f6512..2c2d3e4824b 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -149,6 +149,24 @@ func (lazy *lazySubresource) String() string { 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 // the audit ID associated with the request. type lazyAuditID struct { diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go index 8392f8e7745..81faefa7a07 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers_test.go @@ -133,6 +133,18 @@ func TestLazySubresource(t *testing.T) { 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) { assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyResource{})) diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go index 9fe032d812a..760c9bf40b8 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/trace_util.go @@ -31,6 +31,7 @@ func traceFields(req *http.Request) []attribute.KeyValue { attribute.Stringer("api-version", &lazyAPIVersion{req: req}), attribute.Stringer("name", &lazyName{req: req}), attribute.Stringer("subresource", &lazySubresource{req: req}), + attribute.Stringer("namespace", &lazyNamespace{req: req}), attribute.String("protocol", req.Proto), attribute.Stringer("resource", &lazyResource{req: req}), attribute.Stringer("scope", &lazyScope{req: req}),