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 a25e76db627..fe42370602d 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/helpers.go @@ -21,6 +21,7 @@ import ( utilnet "k8s.io/apimachinery/pkg/util/net" "k8s.io/apiserver/pkg/audit" + apirequest "k8s.io/apiserver/pkg/endpoints/request" ) const ( @@ -88,3 +89,39 @@ func (lazy *lazyAuditID) String() string { return "unknown" } + +// lazyVerb implements String() string and it will +// lazily get Verb from request info based on request context +type lazyVerb struct { + req *http.Request +} + +func (lazy *lazyVerb) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.Verb + } + } + + return "unknown" +} + +// lazyVerb implements String() string and it will +// lazily get Resource from request info based on request context +type lazyResource struct { + req *http.Request +} + +func (lazy *lazyResource) String() string { + if lazy.req != nil { + ctx := lazy.req.Context() + requestInfo, ok := apirequest.RequestInfoFrom(ctx) + if ok { + return requestInfo.Resource + } + } + + return "unknown" +} 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 3f7e3dc106e..e70480d9d04 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 @@ -18,9 +18,10 @@ package handlers import ( "fmt" - "github.com/stretchr/testify/assert" "net/http" "testing" + + "github.com/stretchr/testify/assert" ) func TestLazyTruncatedUserAgent(t *testing.T) { 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 c17401eec70..32790d1419f 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 @@ -24,11 +24,13 @@ import ( func traceFields(req *http.Request) []attribute.KeyValue { return []attribute.KeyValue{ - attribute.String("url", req.URL.Path), - attribute.Stringer("user-agent", &lazyTruncatedUserAgent{req: req}), + attribute.Stringer("accept", &lazyAccept{req: req}), attribute.Stringer("audit-id", &lazyAuditID{req: req}), attribute.Stringer("client", &lazyClientIP{req: req}), - attribute.Stringer("accept", &lazyAccept{req: req}), attribute.String("protocol", req.Proto), + attribute.Stringer("resource", &lazyResource{req: req}), + attribute.String("url", req.URL.Path), + attribute.Stringer("user-agent", &lazyTruncatedUserAgent{req: req}), + attribute.Stringer("verb", &lazyVerb{req: req}), } }