Skip to content

feat: emit MDCUtils.NO_NAMESPACE value when namespace is null#3186

Merged
metacosm merged 2 commits intonextfrom
fix-mdc-ns
Feb 26, 2026
Merged

feat: emit MDCUtils.NO_NAMESPACE value when namespace is null#3186
metacosm merged 2 commits intonextfrom
fix-mdc-ns

Conversation

@metacosm
Copy link
Collaborator

Fixes #3184

Signed-off-by: Chris Laprun metacosm@gmail.com

Fixes #3184

Signed-off-by: Chris Laprun <metacosm@gmail.com>
Copilot AI review requested due to automatic review settings February 25, 2026 16:30
@metacosm metacosm self-assigned this Feb 25, 2026
@metacosm metacosm requested review from csviri and xstefank February 25, 2026 16:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_NAMESPACE and use it when .metadata.namespace is null in addResourceInfo.
  • 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());
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
MDC.put(EVENT_ACTION, action.name());
MDC.put(EVENT_ACTION, action != null ? action.name() : "unknown");

Copilot uses AI. Check for mistakes.
Comment on lines 55 to 58
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.
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

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).

Suggested change
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.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

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

LGTM

@csviri
Copy link
Collaborator

csviri commented Feb 26, 2026

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.

@metacosm metacosm merged commit ecd076f into next Feb 26, 2026
12 of 25 checks passed
@metacosm metacosm deleted the fix-mdc-ns branch February 26, 2026 10:58
@csviri csviri linked an issue Feb 26, 2026 that may be closed by this pull request
csviri pushed a commit that referenced this pull request Feb 28, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MDC: inconsistent handling of absence of namespace

4 participants