feat: emit MDCUtils.NO_NAMESPACE value when namespace is null#3186
feat: emit MDCUtils.NO_NAMESPACE value when namespace is null#3186
Conversation
Fixes #3184 Signed-off-by: Chris Laprun <metacosm@gmail.com>
There was a problem hiding this comment.
Pull request overview
Unifies MDC namespace handling by ensuring a consistent placeholder is logged when a resource has no namespace, improving log filtering for cluster-scoped resources (Fixes #3184).
Changes:
- Expose
MDCUtils.NO_NAMESPACEand use it when.metadata.namespaceis null inaddResourceInfo. - Update observability docs to explain the namespace placeholder behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java | Always emits a namespace value (falls back to NO_NAMESPACE) when adding resource info to MDC. |
| docs/content/en/docs/documentation/observability.md | Documents MDC null/namespace behavior and the NO_NAMESPACE placeholder. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (enabled) { | ||
| addResourceInfo(resource, true); | ||
| MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name()); | ||
| MDC.put(EVENT_ACTION, action.name()); |
There was a problem hiding this comment.
addInformerEventInfo previously handled a null action (falling back to an "unknown" value). With the new MDC.put(EVENT_ACTION, action.name()), passing a null action will now throw an NPE before MDC is populated. Consider restoring the null handling (or enforcing non-null via validation/annotations) to avoid a behavioral regression in this public utility method.
| MDC.put(EVENT_ACTION, action.name()); | |
| MDC.put(EVENT_ACTION, action != null ? action.name() : "unknown"); |
| If a resource doesn't provide values for one of the specified key, the key will be omitted and not added to the MDC | ||
| context. There is, however, one notable exception: the resources' namespace, where, instead of omitting the key, we emit | ||
| the `MDCUtils.NO_NAMESPACE` value instead. This allows searching for resources without namespace (notably, clustered | ||
| resources) in the logs more easily. |
There was a problem hiding this comment.
This new section says that if a resource doesn't provide a value, the MDC key will be omitted. However, MDCUtils.addResourceInfo still calls MDC.put(...) for several fields (e.g., apiVersion/kind/resourceVersion/uid) without null checks, so omission depends on the MDC implementation (and can even be an NPE in some adapters). Either adjust the wording to match actual behavior or update MDCUtils to avoid calling MDC.put with null values (e.g., remove the key explicitly when the value is null).
| If a resource doesn't provide values for one of the specified key, the key will be omitted and not added to the MDC | |
| context. There is, however, one notable exception: the resources' namespace, where, instead of omitting the key, we emit | |
| the `MDCUtils.NO_NAMESPACE` value instead. This allows searching for resources without namespace (notably, clustered | |
| resources) in the logs more easily. | |
| If a resource doesn't provide values for one of the specified keys, the SDK may still attempt to add the corresponding | |
| MDC entry with a `null` value. The exact behavior (omitting the key, storing a `null` value, or even raising an error) | |
| depends on the underlying MDC implementation and any logging adapters in use. There is, however, one notable exception: | |
| the resource's namespace, where, instead of using `null`, we emit the `MDCUtils.NO_NAMESPACE` value. This allows | |
| searching for resources without namespace (notably, cluster-scoped resources) in the logs more easily. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
The CI fails is not related, but cold you restart it. In case you have the bandwidth would be great to check why is that test flaky. |
* feat: emit MDCUtils.NO_NAMESPACE value when namespace is null Fixes #3184 Signed-off-by: Chris Laprun <metacosm@gmail.com> * fix: improve wording Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: Chris Laprun <metacosm@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Fixes #3184
Signed-off-by: Chris Laprun metacosm@gmail.com