Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/en/docs/documentation/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ For `InformerEventSource` and `ControllerEventSource` the following information
| `eventsource.event.action` | action name (e.g. `ADDED`, `UPDATED`, `DELETED`) |
| `eventsource.name` | name of the event source |

### Note on null values

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
Comment thread
metacosm marked this conversation as resolved.
Outdated
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.

### Disabling MDC support

MDC support is enabled by default. If you want to disable it, you can set the `JAVA_OPERATOR_SDK_USE_MDC` environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.javaoperatorsdk.operator.processing.event.source.ResourceAction;

public class MDCUtils {
public static final String NO_NAMESPACE = "no namespace";

private static final String NAME = "resource.name";
private static final String NAMESPACE = "resource.namespace";
Expand All @@ -31,20 +32,18 @@ public class MDCUtils {
private static final String RESOURCE_VERSION = "resource.resourceVersion";
private static final String GENERATION = "resource.generation";
private static final String UID = "resource.uid";
private static final String NO_NAMESPACE = "no namespace";
private static final boolean enabled =
Utils.getBooleanFromSystemPropsOrDefault(Utils.USE_MDC_ENV_KEY, true);

private static final String EVENT_SOURCE_PREFIX = "eventsource.event.";
private static final String EVENT_ACTION = EVENT_SOURCE_PREFIX + "action";
private static final String EVENT_SOURCE_NAME = "eventsource.name";
private static final String UNKNOWN_ACTION = "unknown action";

public static void addInformerEventInfo(
HasMetadata resource, ResourceAction action, String eventSourceName) {
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.
MDC.put(EVENT_SOURCE_NAME, eventSourceName);
}
}
Expand Down Expand Up @@ -92,9 +91,10 @@ public static void addResourceInfo(HasMetadata resource, boolean forEventSource)
final var metadata = resource.getMetadata();
if (metadata != null) {
MDC.put(key(NAME, forEventSource), metadata.getName());
if (metadata.getNamespace() != null) {
MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace());
}

final var namespace = metadata.getNamespace();
MDC.put(key(NAMESPACE, forEventSource), namespace != null ? namespace : NO_NAMESPACE);

MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion());
if (metadata.getGeneration() != null) {
MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString());
Expand Down
Loading