Skip to content
Permalink
Browse files

Log http request and count number of traces before logging

Change-Id: I421afd7c0ef3ce7b4cd37c34b60462ce18c00fe7
  • Loading branch information...
dims committed Jan 11, 2019
1 parent 40de2ee commit 2e03fbfda9454d6d42657bd4f093cc032df4e197
@@ -38,13 +38,18 @@ import (
"k8s.io/apiserver/pkg/util/dryrun"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utiltrace "k8s.io/apiserver/pkg/util/trace"

"k8s.io/klog"
)

func createHandler(r rest.NamedCreater, scope RequestScope, admit admission.Interface, includeName bool) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
// For performance tracking purposes.
trace := utiltrace.New("Create " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)
defer trace.LogIfStepsLessThan(4, func() {
klog.V(4).Infof(">>>> createHandler bad count : %#v", req)
})

if isDryRun(req.URL) && !utilfeature.DefaultFeatureGate.Enabled(features.DryRun) {
scope.err(errors.NewBadRequest("the dryRun alpha feature is disabled"), w, req)
@@ -45,6 +45,8 @@ import (
"k8s.io/apiserver/pkg/util/dryrun"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utiltrace "k8s.io/apiserver/pkg/util/trace"

"k8s.io/klog"
)

// PatchResource returns a function that will handle a resource patch.
@@ -53,7 +55,9 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
// For performance tracking purposes.
trace := utiltrace.New("Patch " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)

defer trace.LogIfStepsLessThan(3, func() {
klog.V(4).Infof(">>>> PatchResource bad count : %#v", req)
})
if isDryRun(req.URL) && !utilfeature.DefaultFeatureGate.Enabled(features.DryRun) {
scope.err(errors.NewBadRequest("the dryRun alpha feature is disabled"), w, req)
return
@@ -35,20 +35,22 @@ import (
// transformResponseObject takes an object loaded from storage and performs any necessary transformations.
// Will write the complete response object.
func transformResponseObject(ctx context.Context, scope RequestScope, req *http.Request, w http.ResponseWriter, statusCode int, mediaType negotiation.MediaTypeOptions, result runtime.Object) {
trace := scope.Trace

// status objects are ignored for transformation
if _, ok := result.(*metav1.Status); ok {
trace.Step("writing metav1.Status")
responsewriters.WriteObject(statusCode, scope.Kind.GroupVersion(), scope.Serializer, result, w, req)
return
}

// ensure the self link and empty list array are set
if err := setObjectSelfLink(ctx, result, req, scope.Namer); err != nil {
trace.Step("setObjectSelfLink failed")
scope.err(err, w, req)
return
}

trace := scope.Trace

// If conversion was allowed by the scope, perform it before writing the response
switch target := mediaType.Convert; {

@@ -57,6 +59,7 @@ func transformResponseObject(ctx context.Context, scope RequestScope, req *http.
responsewriters.WriteObject(statusCode, scope.Kind.GroupVersion(), scope.Serializer, result, w, req)

case target.Kind == "PartialObjectMetadata" && target.GroupVersion() == metav1beta1.SchemeGroupVersion:
trace.Step("writing PartialObjectMetadata")
partial, err := asV1Beta1PartialObjectMetadata(result)
if err != nil {
scope.err(err, w, req)
@@ -101,6 +104,7 @@ func transformResponseObject(ctx context.Context, scope RequestScope, req *http.
}

default:
trace.Step("writing default")
// this block should only be hit if scope AllowsConversion is incorrect
accepted, _ := negotiation.MediaTypesForSerializer(metainternalversion.Codecs)
err := negotiation.NewNotAcceptableError(accepted)
@@ -39,6 +39,8 @@ import (
"k8s.io/apiserver/pkg/util/dryrun"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utiltrace "k8s.io/apiserver/pkg/util/trace"

"k8s.io/klog"
)

// UpdateResource returns a function that will handle a resource update
@@ -47,6 +49,9 @@ func UpdateResource(r rest.Updater, scope RequestScope, admit admission.Interfac
// For performance tracking purposes.
trace := utiltrace.New("Update " + req.URL.Path)
defer trace.LogIfLong(500 * time.Millisecond)
defer trace.LogIfStepsLessThan(5, func() {
klog.V(4).Infof(">>>> UpdateResource bad count : %#v", req)
})

if isDryRun(req.URL) && !utilfeature.DefaultFeatureGate.Enabled(features.DryRun) {
scope.err(errors.NewBadRequest("the dryRun alpha feature is disabled"), w, req)
@@ -53,6 +53,14 @@ func (t *Trace) Log() {
t.logWithStepThreshold(0)
}

func (t *Trace) LogIfStepsLessThan(count int, f func()) {
if len(t.steps) < count {
// an explicit logging request should dump all the steps out at the higher level
t.logWithStepThreshold(0)
f()
}
}

func (t *Trace) logWithStepThreshold(stepThreshold time.Duration) {
var buffer bytes.Buffer
tracenum := rand.Int31()

0 comments on commit 2e03fbf

Please sign in to comment.
You can’t perform that action at this time.