Description
I would like to propose to adjust CR logging.
Today, controller-runtime logs have the following format: (when JSON logging is used)
{
"ts": 1646398972336.9634,
"caller": "cluster/cluster_controller.go:105",
"controller": "cluster",
"reconciler group": "cluster.x-k8s.io",
"reconciler kind": "Cluster",
"msg": "controller/cluster: Waiting for DockerCluster",
"v": 0,
"name": "capi-quickstart",
"namespace": "default"
}
I would like to propose the following format instead:
{
"ts": 1646398972336.9634,
"caller": "cluster/cluster_controller.go:105",
"controller": "cluster",
"controllerGroup": "cluster.x-k8s.io",
"controllerKind": "Cluster",
"msg": "Waiting for DockerCluster",
"v": 0,
"name": "capi-quickstart",
"namespace": "default"
"cluster": {
"name": "capi-quickstart",
"namespace": "default"
},
"reconcileID": "56e044eb38fb76741ffe41cd83e7bed5"
}
The delta is:
- "msg": "controller/cluster: Waiting for DockerCluster",
+ "msg": "Waiting for DockerCluster",
+ "cluster": {
+ "name": "capi-quickstart",
+ "namespace": "default"
+ },
- "reconciler group": "cluster.x-k8s.io",
- "reconciler kind": "Cluster",
+ "controllerGroup": "cluster.x-k8s.io",
+ "controllerKind": "Cluster",
+ "reconcileID": "56e044eb38fb76741ffe41cd83e7bed5",
The ideas behind that are:
Drop the message prefix
- I think the message prefix in every message is not necessary and just makes the logs unnecessarily verbose.
- With structured logging it's easily possible to just filter on / show the controller k/v pair instead.
Log "<kind>": "<namespace>/<name>"
instead of "namespace"
and "name"
- Let's align to the structured logging KEP. Specifically to: https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/1602-structured-logging#object-reference-format-in-logs
- Note: the value is formatted differently for JSON/text format.
- This produces logs consistent with the logs in upstream Kubernetes
- It is a lot easier to correlate logs for a specific object across controllers with this format
Add a reconcileID
With a reconcileID it is very easy to identify logs belong to the same reconcile operation.
Change reconciler kind/ group to controllerKind and controllerGroup
Given that Reconciler only seems to be the "inner" reconciler provided by the user, I think we should rather use controller (e.g. we're setting the name and object on the NewControllerManagedBy
Builder )
Also to align with "casing" best practices outlined here: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments
I've opened a PR to show how a potential solution would look like: ⚠ logging: align to Kubernetes structured logging, add reconcileID
P.S.
- This is an alternative to: Enable per-reconcile logger customization #1811 where we considered making logging customizable.
- I know I made a related change recently to just add the controller k/v pair. We discussed this a bit and now want to propose to align CR logging overall.