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

logging: move clusterworkspaceshard reconciler to structured logging #1614

Conversation

stevekuznetsov
Copy link
Contributor

@stevekuznetsov stevekuznetsov commented Jul 25, 2022

Signed-off-by: Steve Kuznetsov skuznets@redhat.com

Example output:

clusterworkspaceshard_controller.go:99] "Starting controller" reconciler="clusterworkspaceshard"
clusterworkspaceshard_controller.go:88] "queueing workspace shard" reconciler="clusterworkspaceshard" key="root|root"
clusterworkspaceshard_controller.go:125] "processing key" reconciler="clusterworkspaceshard" key="root|root"
clusterworkspaceshard_controller.go:197] "processed ClusterWorkspaceShard" reconciler="clusterworkspaceshard" key="root|root" workspace="root" namespace="" name="root"
clusterworkspaceshard_controller.go:88] "queueing workspace shard" reconciler="clusterworkspaceshard" key="root|root"
clusterworkspaceshard_controller.go:125] "processing key" reconciler="clusterworkspaceshard" key="root|root"
clusterworkspaceshard_controller.go:197] "processed ClusterWorkspaceShard" reconciler="clusterworkspaceshard" key="root|root" workspace="root" namespace="" name="root"
clusterworkspaceshard_controller.go:107] "Shutting down controller" reconciler="clusterworkspaceshard"

/cc @ncdc @sttts

@openshift-ci openshift-ci bot requested review from ncdc and sttts July 25, 2022 14:31
@stevekuznetsov stevekuznetsov force-pushed the skuznets/structured-logging-clusterworkspaceshard-controller branch 2 times, most recently from ac7e356 to 5862781 Compare July 25, 2022 15:00
@@ -83,16 +85,19 @@ func (c *Controller) enqueue(obj interface{}) {
runtime.HandleError(err)
return
}
klog.Infof("queueing workspace shard %q", key)
klog.Background().WithValues(logging.ReconcilerKey, controllerName, logging.KeyKey, key).Info("queueing workspace shard")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unclear if we ever really need logs here (that we're enqueuing) given that we should be safe to assume that we de-queue at some point.... but in any case there's no context provided to the controller's constructor, so we can't share the logger.

Copy link
Member

Choose a reason for hiding this comment

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

We have had cases where we missed events from the informers. And we had cases with backoffs. Having these has had value.

@stevekuznetsov stevekuznetsov force-pushed the skuznets/structured-logging-clusterworkspaceshard-controller branch from 5862781 to 8e450e8 Compare July 25, 2022 15:03
@stevekuznetsov stevekuznetsov force-pushed the skuznets/structured-logging-clusterworkspaceshard-controller branch from 8e450e8 to d79e1f9 Compare July 25, 2022 15:45
@stevekuznetsov stevekuznetsov force-pushed the skuznets/structured-logging-clusterworkspaceshard-controller branch from d79e1f9 to defdfe0 Compare July 25, 2022 15:48
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
@stevekuznetsov stevekuznetsov force-pushed the skuznets/structured-logging-clusterworkspaceshard-controller branch from defdfe0 to c3231e0 Compare July 25, 2022 15:55
@@ -83,16 +85,19 @@ func (c *Controller) enqueue(obj interface{}) {
runtime.HandleError(err)
return
}
klog.Infof("queueing workspace shard %q", key)
logger := logging.WithQueueKey(logging.WithReconciler(klog.Background(), controllerName), key)
Copy link
Member

Choose a reason for hiding this comment

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

👍

klog.Infof("processing key %q", key)
logger := logging.WithQueueKey(klog.FromContext(ctx), key)
ctx = klog.NewContext(ctx, logger)
logger.Info("processing key")
Copy link
Member

Choose a reason for hiding this comment

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

nit: lower or upper case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lower probably best?

Copy link
Member

Choose a reason for hiding this comment

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

I tend to prefer lower

@@ -182,6 +193,7 @@ func (c *Controller) process(ctx context.Context, key string) error {
return uerr
}

logger.V(6).Info("processed ClusterWorkspaceShard")
Copy link
Member

Choose a reason for hiding this comment

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

do these V levels map directly to klog levels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

return nil // object deleted before we handled it
}
return err
}
previous := obj
obj = obj.DeepCopy()

logger = logging.WithObject(logger, obj)
ctx = klog.NewContext(ctx, logger)
Copy link
Member

Choose a reason for hiding this comment

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

everytime I see these I wished klog.NewContext would return the logger in order to write

ctx, logger := klog.NewContext(ctx, logging.WithObject(logger, obj))

Copy link
Member

Choose a reason for hiding this comment

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

We'll probably want a helper

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, we can do that too - it's not every case where you want to decorate the context and the logger, but can't hurt to over-helper it :)

Copy link
Member

@sttts sttts Jul 25, 2022

Choose a reason for hiding this comment

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

I would argue that 95% of cases where you forget to augment the context is a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we'll find that 95% of our logging code is in terminal/leaf calls where you're not passing the thing on to anyone else but yes, maybe better just to be explicit about it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for _, item := range collection {
    logger = logger.WithValue("item", item)
    // use it
}

@ncdc
Copy link
Member

ncdc commented Jul 25, 2022

LGTM. We can always evolve our patterns and styles over time.

Let's also add some logging conventions to a markdown doc somewhere (can be another PR)?

@sttts
Copy link
Member

sttts commented Jul 26, 2022

/approve
Lgtm.

Anything left open to do?

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 26, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sttts

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 26, 2022
@sttts
Copy link
Member

sttts commented Jul 27, 2022

/lgtm

We will iterate.

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 27, 2022
@sttts
Copy link
Member

sttts commented Jul 27, 2022

/retest

@ncdc
Copy link
Member

ncdc commented Jul 27, 2022

Hit flake #1626

@ncdc
Copy link
Member

ncdc commented Jul 27, 2022

/retest

@openshift-merge-robot openshift-merge-robot merged commit 4a52d62 into kcp-dev:main Jul 27, 2022
@stevekuznetsov
Copy link
Contributor Author

#1694

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants