mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-18 08:09:58 +00:00
Added Verb and Resource to request trace attributes
This commit is contained in:
parent
b4156ea47b
commit
fad094cb70
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
utilnet "k8s.io/apimachinery/pkg/util/net"
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
||||||
"k8s.io/apiserver/pkg/audit"
|
"k8s.io/apiserver/pkg/audit"
|
||||||
|
apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -88,3 +89,39 @@ func (lazy *lazyAuditID) String() string {
|
|||||||
|
|
||||||
return "unknown"
|
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"
|
||||||
|
}
|
||||||
|
@ -18,9 +18,10 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLazyTruncatedUserAgent(t *testing.T) {
|
func TestLazyTruncatedUserAgent(t *testing.T) {
|
||||||
|
@ -24,11 +24,13 @@ import (
|
|||||||
|
|
||||||
func traceFields(req *http.Request) []attribute.KeyValue {
|
func traceFields(req *http.Request) []attribute.KeyValue {
|
||||||
return []attribute.KeyValue{
|
return []attribute.KeyValue{
|
||||||
attribute.String("url", req.URL.Path),
|
attribute.Stringer("accept", &lazyAccept{req: req}),
|
||||||
attribute.Stringer("user-agent", &lazyTruncatedUserAgent{req: req}),
|
|
||||||
attribute.Stringer("audit-id", &lazyAuditID{req: req}),
|
attribute.Stringer("audit-id", &lazyAuditID{req: req}),
|
||||||
attribute.Stringer("client", &lazyClientIP{req: req}),
|
attribute.Stringer("client", &lazyClientIP{req: req}),
|
||||||
attribute.Stringer("accept", &lazyAccept{req: req}),
|
|
||||||
attribute.String("protocol", req.Proto),
|
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}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user