controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

✨ logging: allow override default logger for compatibility

Open timonwong opened this issue 2 years ago • 6 comments

#1827 introduces WithLogConstructor and removed WithLogger but in practical it's very hard to use WithLogConstructor to inject fields like:

  • "controller", controllerName
  • "controllerGroup": gvk.Group
  • "controllerKind", gvk.Kind

This PR proposes we can keep WithLogger for backward-compat.

timonwong avatar Aug 09 '22 07:08 timonwong

Welcome @timonwong!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. :smiley:

k8s-ci-robot avatar Aug 09 '22 07:08 k8s-ci-robot

Hi @timonwong. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Aug 09 '22 07:08 k8s-ci-robot

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: timonwong Once this PR has been reviewed and has the lgtm label, please assign pwittrock for approval by writing /assign @pwittrock in a comment. For more information see:The Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

k8s-ci-robot avatar Aug 09 '22 07:08 k8s-ci-robot

introduces WithLogConstructor and removed WithLogger but in practical it's very hard to use WithLogConstructor to inject fields like:

Can you elaborate why that is hard and easier with this? From what I can tell the only thing it does is putting a logger into a closure

alvaroaleman avatar Aug 12 '22 13:08 alvaroaleman

Can you elaborate why that is hard and easier with this? From what I can tell the only thing it does is putting a logger into a closure

@alvaroaleman

Previously, we just construct multiple loggers (our logging facility is similar to istio's, which registers each business "scope", and their levels can be changed at runtime separately), pseudo-code :

var genericControlPlaneLogger = logging.RegisterScope("control-plane")
var istioUpdateLogger = logging.RegisterScope("istio-update")
var domainLogger = logging.RegisterScope("domain")
var wasmLogger =  logging.RegisterScope("wasm")
// etc, etc

// and there are multiple controllers, each controller setup with the builder:

ctrl1.WithLogger(genericControlPlaneLogger).Build()
ctrl2.WithLogger(istioUpdateLogger).Build()
ctrl3.WithLogger(domainLogger).Build()
ctrl4.WithLogger(wasmLogger).Build()
// etc, etc

And previously, logger keys "controller", "controllerGroup", "controllerKind" are auto-generated (after #1827, when customized logger with WithLogConstructor, you can only set them manually):

var genericControlPlaneLogger = logging.RegisterScope("control-plane")
var istioUpdateLogger = logging.RegisterScope("istio-update")
var domainLogger = logging.RegisterScope("domain")
var wasmLogger =  logging.RegisterScope("wasm")
// etc, etc

func logConstructorFunc(logger logr.Logger, controllerName string, gvk schema.GroupVersionKind) func(*reconcile.Request) logr.Logger {
  return func(req *reconcile.Request) logr.Logger {
    logger = logger.WithValues("controller", controller, "controllerGroup", gvk.Group, "controllerKind", gvk.Kind)
    if req != nil {
      logger = logger.WithValues(gvk.Kind, klog.KRef(req.Namespace, req.Name), "namespace", req.Namespace, "name", req.Name)
    }
    return logger
  }
}

ctrl1.WithLogConstructor(logConstructorFunc(genericControlPlaneLogger, ...)).Build()
ctrl2.WithLogConstructor(logConstructorFunc(istioUpdateLogger, ...)).Build()
ctrl3.WithLogConstructor(logConstructorFunc(domainLogger, ...)).Build()
ctrl4.WithLogConstructor(logConstructorFunc(wasmLogger, ...)).Build()

You can see it (func logConstructorFunc) requires more work to archive same functionality as WithLogger

timonwong avatar Aug 14 '22 08:08 timonwong

@timonwong how about instead just adding the fields unconditionally, regardless of logConstructor being set or not?

The new api this introduces is IMHO not great, from a users POV it is very difficult to understand what the difference between LogConstructor and GetDefaultLogger is. Also I can not think of a reason for not wanting the controller etc fields on the logger. That will make it a breaking change though.

alvaroaleman avatar Aug 15 '22 13:08 alvaroaleman

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Nov 13 '22 14:11 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Dec 13 '22 14:12 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

k8s-triage-robot avatar Jan 12 '23 15:01 k8s-triage-robot

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Jan 12 '23 15:01 k8s-ci-robot