Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Contextual logging in the apiserver POC #114198

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/types"
auditinternal "k8s.io/apiserver/pkg/apis/audit"
"k8s.io/apiserver/pkg/audit"
"k8s.io/klog/v2"

"github.com/google/uuid"
)
Expand All @@ -39,7 +40,6 @@ func WithAuditInit(handler http.Handler) http.Handler {
func withAuditInit(handler http.Handler, newAuditIDFunc func() string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := audit.WithAuditContext(r.Context())
r = r.WithContext(ctx)

auditID := r.Header.Get(auditinternal.HeaderAuditID)
if len(auditID) == 0 {
Expand All @@ -49,6 +49,10 @@ func withAuditInit(handler http.Handler, newAuditIDFunc func() string) http.Hand
// Note: we save the user specified value of the Audit-ID header as is, no truncation is performed.
audit.WithAuditID(ctx, types.UID(auditID))

logger := klog.FromContext(ctx).WithValues("auditID", auditID)
ctx = klog.NewContext(ctx, logger)
r = r.WithContext(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on making this step unnecessary in go-logr/logr#158

The exact solution is still unknown, but it should become faster than modifying the logger and adding it to the context.

Do you have benchmarks that could be run to measure the performance impact of these changes?


// We echo the Audit-ID in to the response header.
// It's not guaranteed Audit-ID http header is sent for all requests.
// For example, when user run "kubectl exec", apiserver uses a proxy handler
Expand Down