Description
It would be great if it would be possible to modify loggers for individual reconciles.
Some context:
- When using the standard controller
pkg/builder.Builder
today you can set a logger which is later adjusted:- during Builder.Build():
- "reconciler group" and "reconciler kind" k/v pairs are added
- "controller" and "<controller-name>" are added as logger name
- "controller": "<controller-name>" is added as k/v pair
- per reconcile in reconcileHandler:
- "name": "<object-name>", "namespace": "<object-namespace>" k/v pairs are added
- during Builder.Build():
The name and the k/v pair are added on top of either the logger provided in the builder or on top of mgr.GetLogger()
.
We would like to be able to customize the logger according to our requirements. Concrete this means we would like to add a "traceID" and "<kind>": "<namespace>/<name>" k/v pair to the logger (more background info below). We could do this during the Reconcile
call, but then the error logged by controller-runtime wouldn't have this context (this one).
As I'm pretty sure there are different opinions about what the name of a logger should look like and which k/v pairs should or shouldn't be added, I would propose to add a "hook" so that folks are able to customize the logger in the way they want.
Also e.g. dropping the current "name"/"namespace" k/v pairs would be a breaking change in controller-runtime.
Background info about "traceID" and "<kind>": "<namespace>/<name>"
- "traceID": we want to be able to correlate logs of an individual reconcile. Furthermore we want to be able to correlate them with traces and metrics in the future
- "<kind>": "<namespace>/<name>": We are trying to follow the upstream Kubernetes structured logging KEP. The KEP standardizes the "value" of the k/v pair to be "'<namespace>/<name>". It doesn't exactly standardize the key of it but in CAPI we will probably follow the examples there and use the lower case singular kind.
I just wanted to provide this as background information. I think the discussion if this is a good or bad idea is out-of-scope (and still TBD in the CAPI project). But I think it should illustrate that it's worth making the logger customizable, so it's possible to accommodate requirements like this.
I've opened the following PR to illustrate how a solution could look like: