mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #120388 from HirazawaUi/add-group-to-apiserver-tracing
Add group, version, namespace, name, and subresource to apiserver tracing
This commit is contained in:
commit
1e862b0fec
@ -77,6 +77,96 @@ func (lazy *lazyAccept) String() string {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// lazyAPIGroup implements String() string and it will
|
||||
// lazily get Group from request info.
|
||||
type lazyAPIGroup struct {
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
func (lazy *lazyAPIGroup) String() string {
|
||||
if lazy.req != nil {
|
||||
ctx := lazy.req.Context()
|
||||
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
||||
if ok {
|
||||
return requestInfo.APIGroup
|
||||
}
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// lazyAPIVersion implements String() string and it will
|
||||
// lazily get Group from request info.
|
||||
type lazyAPIVersion struct {
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
func (lazy *lazyAPIVersion) String() string {
|
||||
if lazy.req != nil {
|
||||
ctx := lazy.req.Context()
|
||||
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
||||
if ok {
|
||||
return requestInfo.APIVersion
|
||||
}
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// lazyName implements String() string and it will
|
||||
// lazily get Group from request info.
|
||||
type lazyName struct {
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
func (lazy *lazyName) String() string {
|
||||
if lazy.req != nil {
|
||||
ctx := lazy.req.Context()
|
||||
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
||||
if ok {
|
||||
return requestInfo.Name
|
||||
}
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// lazySubresource implements String() string and it will
|
||||
// lazily get Group from request info.
|
||||
type lazySubresource struct {
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
func (lazy *lazySubresource) String() string {
|
||||
if lazy.req != nil {
|
||||
ctx := lazy.req.Context()
|
||||
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
||||
if ok {
|
||||
return requestInfo.Subresource
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -85,6 +85,66 @@ func TestLazyVerb(t *testing.T) {
|
||||
assert.Equal(t, "WATCH", fmt.Sprintf("%v", verbWithReq))
|
||||
}
|
||||
|
||||
func TestLazyApiGroup(t *testing.T) {
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIGroup{}))
|
||||
|
||||
scopeWithEmptyReq := &lazyAPIGroup{&http.Request{}}
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
|
||||
|
||||
req := &http.Request{}
|
||||
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIGroup: "apps"})
|
||||
scopeWithReq := &lazyAPIGroup{req: req.WithContext(ctx)}
|
||||
assert.Equal(t, "apps", fmt.Sprintf("%v", scopeWithReq))
|
||||
}
|
||||
|
||||
func TestLazyApiVersion(t *testing.T) {
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyAPIVersion{}))
|
||||
|
||||
scopeWithEmptyReq := &lazyAPIVersion{&http.Request{}}
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
|
||||
|
||||
req := &http.Request{}
|
||||
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{APIVersion: "v1"})
|
||||
scopeWithReq := &lazyAPIVersion{req: req.WithContext(ctx)}
|
||||
assert.Equal(t, "v1", fmt.Sprintf("%v", scopeWithReq))
|
||||
}
|
||||
|
||||
func TestLazyName(t *testing.T) {
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazyName{}))
|
||||
|
||||
scopeWithEmptyReq := &lazyName{&http.Request{}}
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
|
||||
|
||||
req := &http.Request{}
|
||||
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Name: "jaeger-76d45d6876-vqp8t"})
|
||||
scopeWithReq := &lazyName{req: req.WithContext(ctx)}
|
||||
assert.Equal(t, "jaeger-76d45d6876-vqp8t", fmt.Sprintf("%v", scopeWithReq))
|
||||
}
|
||||
|
||||
func TestLazySubresource(t *testing.T) {
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", &lazySubresource{}))
|
||||
|
||||
scopeWithEmptyReq := &lazySubresource{&http.Request{}}
|
||||
assert.Equal(t, "unknown", fmt.Sprintf("%v", scopeWithEmptyReq))
|
||||
|
||||
req := &http.Request{}
|
||||
ctx := request.WithRequestInfo(context.TODO(), &request.RequestInfo{Subresource: "binding"})
|
||||
scopeWithReq := &lazySubresource{req: req.WithContext(ctx)}
|
||||
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{}))
|
||||
|
||||
|
@ -27,6 +27,11 @@ func traceFields(req *http.Request) []attribute.KeyValue {
|
||||
attribute.Stringer("accept", &lazyAccept{req: req}),
|
||||
attribute.Stringer("audit-id", &lazyAuditID{req: req}),
|
||||
attribute.Stringer("client", &lazyClientIP{req: req}),
|
||||
attribute.Stringer("api-group", &lazyAPIGroup{req: req}),
|
||||
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}),
|
||||
|
Loading…
Reference in New Issue
Block a user